Skip to content

Commit

Permalink
Merge pull request #56 from skycocker/master
Browse files Browse the repository at this point in the history
Stop overriding callback methods if they're present
  • Loading branch information
chevinbrown authored Dec 8, 2018
2 parents 7cf6ba7 + 06767a1 commit 3c9798c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,26 @@ Instances with accepted Friendship.
@dee.friends # => [@mac]
```

### Custom validations

You can provide custom validations for the friendship
by implementing `friendship_errors` method on your Friendable model.

Returning an array with any elements will result in the friendship not being established.

```ruby
def friendship_errors(wannabe_friend)
return if can_become_friends_with?(wannabe_friend)

[
"Cannot become friends with #{wannabe_friend.email}",
]
end
```

### Callbacks

To use callbacks you can add methods described below, to your Friendable model.
To use callbacks you can add methods described below to your Friendable model.

```ruby
def on_friendship_created(friendship)
Expand All @@ -154,6 +171,10 @@ end
def on_friendship_blocked(friendship)
...
end

def on_friendship_destroyed(friendship)
...
end
```

## Roadmap
Expand Down
17 changes: 12 additions & 5 deletions lib/has_friendship/friendable.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module HasFriendship
module Friendable

def friendable?
false
end
Expand Down Expand Up @@ -39,6 +38,18 @@ def self.friendable?
end

module InstanceMethods
CALLBACK_METHOD_NAMES = %i(
on_friendship_created
on_friendship_accepted
on_friendship_blocked
on_friendship_destroyed
).freeze

CALLBACK_METHOD_NAMES.each do |method_name|
define_method(method_name) do |*args|
super(*args) if defined?(super)
end
end

def friend_request(friend)
unless self == friend || HasFriendship::Friendship.exist?(self, friend)
Expand Down Expand Up @@ -88,10 +99,6 @@ 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
19 changes: 17 additions & 2 deletions lib/has_friendship/friendship.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
module HasFriendship
class Friendship < ActiveRecord::Base
validate :satisfy_custom_conditions

after_create do
friend.on_friendship_created(self)
after_create do |record|
friend.on_friendship_created(record)
end

after_destroy do |record|
friend.try(:on_friendship_destroyed, record)
end

enum status: { pending: 0, requested: 1, accepted: 2, blocked: 3 } do
Expand Down Expand Up @@ -65,5 +70,15 @@ def self.find_blocked_friendship(friendable, friend)
def self.find_one_side(one, other)
find_by(relation_attributes(one, other))
end

private

def satisfy_custom_conditions
return unless friend.respond_to?(:friendship_errors)

friend.friendship_errors(friendable).to_a.each do |friendship_error|
errors.add(:base, friendship_error)
end
end
end
end

0 comments on commit 3c9798c

Please sign in to comment.