has_friendship 1.1.5 → 1.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e7405f12ea84e279cc43435c42a147f26564beeed2d42491b9ed46d6b1c0251
4
- data.tar.gz: 9f4519f3ef0253f739a46f2fa2efc0a806ceb320de89eb654a739eb3139c839c
3
+ metadata.gz: 57947ab4c8d036df58994c27b253d0f301b7530da79c95a56d957dfe50ca2c94
4
+ data.tar.gz: 340377d9412274cc00905e2f469b2e1fff293a89c172b165bd1a92ae64ef7407
5
5
  SHA512:
6
- metadata.gz: 7d04acf4d3f0f4a9450a40b6edcefa2b89267e275a2da11ed94c5c39a0743bd98fceb4dd28afab0fa915d74a00f671a9b3570cf21b9bd45caca998bd70014a92
7
- data.tar.gz: 7da05c876ed87d065940447da83e84810db4e5f7ce904ff0548575e34c483e607b3f23b0d2dbd855e576d0427735a9be804de0a4c3f043898d2ac3e2759b1a5c
6
+ metadata.gz: 34ca1faf29f2b1f70a88951f13b5beb9893948e140d312c798c179a7e1536518eec9a29ca7a76c17b926be9c1591e2baaf43e6750415483fe2e59906bd1c4535
7
+ data.tar.gz: c5a960c9adf8e360fd893971f650d653f7dda143f316771a5079d6ac9ce85d8bcb1dc2b011a9ff0cf4c5cb2481329f6d4905dc9f3f733cf15014c91fc67ae711
@@ -14,6 +14,8 @@ AddUniqueIndexToFriendships.class_eval do
14
14
  end
15
15
 
16
16
  def self.down
17
+ return unless index_exists?(:friendships, [:friendable_id, :friend_id])
18
+
17
19
  remove_index :friendships, [:friendable_id, :friend_id]
18
20
  end
19
21
  end
@@ -1,6 +1,5 @@
1
1
  module HasFriendship
2
2
  module Friendable
3
-
4
3
  def friendable?
5
4
  false
6
5
  end
@@ -39,6 +38,18 @@ module HasFriendship
39
38
  end
40
39
 
41
40
  module InstanceMethods
41
+ CALLBACK_METHOD_NAMES = %i(
42
+ on_friendship_created
43
+ on_friendship_accepted
44
+ on_friendship_blocked
45
+ on_friendship_destroyed
46
+ ).freeze
47
+
48
+ CALLBACK_METHOD_NAMES.each do |method_name|
49
+ define_method(method_name) do |*args|
50
+ super(*args) if defined?(super)
51
+ end
52
+ end
42
53
 
43
54
  def friend_request(friend)
44
55
  unless self == friend || HasFriendship::Friendship.exist?(self, friend)
@@ -88,10 +99,6 @@ module HasFriendship
88
99
  HasFriendship::Friendship.find_relation(self, friend, status: 2).any?
89
100
  end
90
101
 
91
- def on_friendship_created(*args); end
92
- def on_friendship_accepted(*args); end
93
- def on_friendship_blocked(*args); end
94
-
95
102
  private
96
103
 
97
104
  def has_blocked(friend)
@@ -1,8 +1,13 @@
1
1
  module HasFriendship
2
2
  class Friendship < ActiveRecord::Base
3
+ validate :satisfy_custom_conditions
3
4
 
4
- after_create do
5
- friend.on_friendship_created(self)
5
+ after_create do |record|
6
+ friend.on_friendship_created(record)
7
+ end
8
+
9
+ after_destroy do |record|
10
+ friendable.try(:on_friendship_destroyed, record)
6
11
  end
7
12
 
8
13
  enum status: { pending: 0, requested: 1, accepted: 2, blocked: 3 } do
@@ -65,5 +70,15 @@ module HasFriendship
65
70
  def self.find_one_side(one, other)
66
71
  find_by(relation_attributes(one, other))
67
72
  end
73
+
74
+ private
75
+
76
+ def satisfy_custom_conditions
77
+ return unless friend.respond_to?(:friendship_errors)
78
+
79
+ friend.friendship_errors(friendable).to_a.each do |friendship_error|
80
+ errors.add(:base, friendship_error)
81
+ end
82
+ end
68
83
  end
69
84
  end
@@ -1,3 +1,3 @@
1
1
  module HasFriendship
2
- VERSION = "1.1.5"
2
+ VERSION = "1.1.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_friendship
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sung Won Cho
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-01 00:00:00.000000000 Z
11
+ date: 2018-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails