Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run callbacks on state change #41

Merged
merged 1 commit into from
May 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/has_friendship/friendable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions lib/has_friendship/friendship.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
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
before do
self.blocker_id = self.friendable.id
end

after do
friendable.on_friendship_blocked(self)
end
transition all - [:blocked] => :blocked
end
end
Expand Down
25 changes: 25 additions & 0 deletions spec/has_friendship/friendable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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