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

Stop overriding callback methods if they're present #56

Merged
merged 4 commits into from
Dec 8, 2018
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
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,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 @@ -165,6 +182,10 @@ end
def on_friendship_blocked(friendship)
...
end

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

## Roadmap
Expand Down
2 changes: 1 addition & 1 deletion lib/has_friendship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ module HasFriendship

ActiveSupport.on_load(:active_record) do
extend HasFriendship::Friendable
end
end
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