From 7afc5f118c6ac2bc8b720d2c0001e34664bf62a4 Mon Sep 17 00:00:00 2001 From: Sebastian Badura Date: Sat, 6 May 2017 14:19:16 +0200 Subject: [PATCH] Run callbacks on state change --- README.md | 18 ++++++++++++++++++ lib/has_friendship/friendable.rb | 4 ++++ lib/has_friendship/friendship.rb | 11 +++++++++++ spec/has_friendship/friendable_spec.rb | 25 +++++++++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/README.md b/README.md index 724be6d..7b67a09 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,24 @@ Instances with accepted Friendship. @dee.friends # => [@mac] ``` +### Callbacks + +To use callbacks you can add methods described below, to your Friendable model. + +```ruby +def on_friendship_created(friendship) + ... +end + +def on_friendship_accepted(friendship) + ... +end + +def on_friendship_blocked(friendship) + ... +end +``` + ## Roadmap Thanks for all the contributors. Pull requests are encouraged for the following diff --git a/lib/has_friendship/friendable.rb b/lib/has_friendship/friendable.rb index 4508fc7..bc0035f 100644 --- a/lib/has_friendship/friendable.rb +++ b/lib/has_friendship/friendable.rb @@ -88,6 +88,10 @@ def friends_with?(friend) HasFriendship::Friendship.find_relation(self, friend, status: 2).any? end + def on_friendship_created(*args); end + def on_friendship_accepted(*args); end + def on_friendship_blocked(*args); end + private def has_blocked(friend) diff --git a/lib/has_friendship/friendship.rb b/lib/has_friendship/friendship.rb index b45874f..2f717b5 100644 --- a/lib/has_friendship/friendship.rb +++ b/lib/has_friendship/friendship.rb @@ -1,9 +1,17 @@ module HasFriendship class Friendship < ActiveRecord::Base + after_create do + friend.on_friendship_created(self) + end + enum status: { pending: 0, requested: 1, accepted: 2, blocked: 3 } do event :accept do transition [:pending, :requested] => :accepted + + after do + friendable.on_friendship_accepted(self) + end end event :block do @@ -11,6 +19,9 @@ class Friendship < ActiveRecord::Base self.blocker_id = self.friendable.id end + after do + friendable.on_friendship_blocked(self) + end transition all - [:blocked] => :blocked end end diff --git a/spec/has_friendship/friendable_spec.rb b/spec/has_friendship/friendable_spec.rb index 4e2eed4..513dbc4 100644 --- a/spec/has_friendship/friendable_spec.rb +++ b/spec/has_friendship/friendable_spec.rb @@ -272,5 +272,30 @@ end end end + + describe '#on_friendship_accepted' do + context 'when friendship is accepted' do + it 'should be called' do + user.friend_request(friend) + friendship = find_friendship_record(user, friend) + + expect(friendship.friendable).to receive(:on_friendship_accepted) + friendship.accept! + end + end + end + + describe '#on_friendship_blocked' do + context 'when friendship is blocked' do + it 'should be called' do + user.friend_request(friend) + friendship = find_friendship_record(user, friend) + + expect(friendship.friendable).to receive(:on_friendship_blocked) + friendship.block! + end + end + end + end end