popular 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MDBkZDk2OTY1OTczYjYzMzkzOTVmN2QyZDMyMjYwNmE1ZjBjOWYxOA==
4
+ NzNkYjliYTc5YjZhMTQ0YzYzZDQ3NGY0ZGI1M2VkMjRhMjBlZDcyZQ==
5
5
  data.tar.gz: !binary |-
6
- NTA3NTI1MmUwY2UzMzk1MWY1MTMxM2FmZGQxNzE4NTZmMGVjY2ZhZA==
6
+ MzdlYjQzM2UwNWJkZGI3NTVmMDVlMDgzNTg3YWMzMmM4NzUxMDY5Zg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZDc4MDI4NjI3OWNlNmM4MzRmNzVmMzYxZDBjYWRhYzFjNDgxM2E2NWExMDRk
10
- ZjgwNjZmMDAyYzQzOGE3NzkxMDZjNjA3MWVlZTVkMzIyZDk4NjllMjQ4YzRj
11
- NDU5ZjZhMjMxZGE4OGM2Njk2NGM0NGE5ZGRjNGU3ZDQ4MTMxMWU=
9
+ N2YyOGQ2ZmUwNDczNzVhNjhlYjA3N2M2N2MzNjJkODQ4ODMzNWU0YjJkODcz
10
+ ZWM0Yzc0OGU3OWU5OTBiMTAzMGI2YWEwMTZkNDM0MDNhZDVkOGQ2NGU0MTYy
11
+ NmFhOGIwMTE5NjdjZDU2YTQ3ZGE1NjY1YWZmMWViMWNiOTU3MmY=
12
12
  data.tar.gz: !binary |-
13
- OThiOGJlNDljMmUzMTNmOTI4NGNkYmFmYzliOWJiNzgxODAwYzEzODljZGUx
14
- N2YzNjg3NTVlODFjN2NhZjUyMDc1MzgzNTIyOGFhMTIxYTM0NGE5OTYyZjA4
15
- MDI3Y2E5YmI1MmVkOTJhYjZhZDlmMTlmMWZlNjEwMjFiNzM3OTA=
13
+ Y2YwNTRhZDI5YWNkMzc5NjNjYjg3ZDRmZDQzNWQ0NmEzMjYwMjU1N2IzODk5
14
+ ZjUxOTNiOWM5NWM0ZjZhNmIwZjY5MzU3OWFmYTc3ZjM2ZDFhZGY3ODRhYjc2
15
+ NzQxZDM5ZWYyZDJjMjhmN2JhMmRkZTQwYWIxZDJmYjI2NzY1NjQ=
data/README.md CHANGED
@@ -59,22 +59,30 @@ end
59
59
  Popular provides callbacks that are fired around friendship creation. Available callbacks are:
60
60
  - after_befriend
61
61
  - before_befriend
62
+ - after_unfriend
63
+ - before_unfriend
62
64
 
63
65
 
64
66
  ```ruby
65
67
  class User < ActiveRecord::Base
66
68
  popular
67
- after_befriend :notify
69
+ after_befriend :notify_friendship_created
70
+ after_unfriend :notify_unfriended
68
71
 
69
- def notify
72
+ def notify_friendship_created
70
73
  puts "Friendship created successfully"
71
74
  end
75
+
76
+ def notify_unfriended
77
+ puts ":("
78
+ end
72
79
  end
73
80
 
74
81
  @justin = User.create name: "Justin"
75
82
  @jenny = User.create name: "Jenny"
76
83
 
77
84
  @justin.befriend @jenny #=> "Friendship created successfully"
85
+ @justin.unfriend @jenny #=> ":("
78
86
  ```
79
87
 
80
88
  ## Contributing
@@ -9,9 +9,9 @@ module Popular
9
9
  has_many :friends, through: :friendships, source_type: base
10
10
 
11
11
  include ActiveSupport::Callbacks
12
- define_callbacks :befriend
12
+ define_callbacks :befriend, :unfriend
13
13
 
14
- [:before_befriend, :after_befriend].each do |callback|
14
+ [:before_unfriend, :after_unfriend, :before_befriend, :after_befriend].each do |callback|
15
15
  send callback
16
16
  end
17
17
  end
@@ -61,7 +61,9 @@ module Popular
61
61
  #
62
62
  # user.friends_with? other_user # => false
63
63
  def unfriend friend
64
- friendships.where( friend: friend ).first.destroy
64
+ run_callbacks :unfriend do
65
+ friendships.where( friend: friend ).first.destroy
66
+ end
65
67
  end
66
68
 
67
69
  # Helper method for finding whether or not the instance is friends with
@@ -86,6 +88,52 @@ module Popular
86
88
  # ClassMethods included in popular models
87
89
  module ClassMethods
88
90
 
91
+ # after_unfriend callback convenience class method
92
+ #
93
+ # @since 0.4.0
94
+ #
95
+ # @example
96
+ #
97
+ # class User < ActiveRecord::Base
98
+ # after_unfriend :do_something_amazing
99
+ #
100
+ # def do_something_amazing
101
+ # puts name
102
+ # end
103
+ # end
104
+ #
105
+ # user = User.create name: "Justin"
106
+ # another_user = User.create name: "Jenny"
107
+ #
108
+ # user.befriend another_user
109
+ # user.unfriend another_user #=> "Justin"
110
+ def after_unfriend *args, &blk
111
+ set_callback :unfriend, :after, *args, &blk
112
+ end
113
+
114
+ # before_unfriend callback convenience class method
115
+ #
116
+ # @since 0.4.0
117
+ #
118
+ # @example
119
+ #
120
+ # class User < ActiveRecord::Base
121
+ # before_unfriend :do_something_amazing
122
+ #
123
+ # def do_something_amazing
124
+ # puts name
125
+ # end
126
+ # end
127
+ #
128
+ # user = User.create name: "Justin"
129
+ # another_user = User.create name: "Jenny"
130
+ #
131
+ # user.befriend another_user
132
+ # user.unfriend another_user #=> "Justin"
133
+ def before_unfriend *args, &blk
134
+ set_callback :unfriend, :before, *args, &blk
135
+ end
136
+
89
137
  # before_befriend callback convenience class method
90
138
  #
91
139
  # @since 0.3.0
@@ -1,3 +1,3 @@
1
1
  module Popular
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -8,22 +8,16 @@ shared_examples "a popular model" do
8
8
  end
9
9
 
10
10
  it 'triggers before_befriend callback' do
11
- popular_model_with_before_befriend_callback
12
- .should_receive( :callback_worked )
13
- .once
14
- .and_return true
11
+ ensure_callback_fired_on popular_model_with_callbacks, :before_befriend_callback
15
12
 
16
- popular_model_with_before_befriend_callback.befriend! another_popular_model
13
+ popular_model_with_callbacks.befriend! another_popular_model
17
14
  end
18
15
 
19
16
  context 'successful' do
20
17
  it 'triggers after_befriend callback' do
21
- popular_model_with_after_befriend_callback
22
- .should_receive( :callback_worked )
23
- .once
24
- .and_return true
18
+ ensure_callback_fired_on popular_model_with_callbacks, :after_befriend_callback
25
19
 
26
- popular_model_with_after_befriend_callback.befriend! another_popular_model
20
+ popular_model_with_callbacks.befriend! another_popular_model
27
21
  end
28
22
  end
29
23
  end
@@ -31,13 +25,16 @@ shared_examples "a popular model" do
31
25
  describe '.befriend' do
32
26
 
33
27
  context 'successful' do
28
+ it 'triggers before_befriend callback' do
29
+ ensure_callback_fired_on popular_model_with_callbacks, :before_befriend_callback
30
+
31
+ popular_model_with_callbacks.befriend another_popular_model
32
+ end
33
+
34
34
  it 'triggers after_befriend callback' do
35
- popular_model_with_after_befriend_callback
36
- .should_receive( :callback_worked )
37
- .once
38
- .and_return true
35
+ ensure_callback_fired_on popular_model_with_callbacks, :after_befriend_callback
39
36
 
40
- popular_model_with_after_befriend_callback.befriend another_popular_model
37
+ popular_model_with_callbacks.befriend another_popular_model
41
38
  end
42
39
 
43
40
  it 'creates a one way friendship' do
@@ -53,6 +50,7 @@ shared_examples "a popular model" do
53
50
  it 'returns true if a popular_model is friends with a given popular_model' do
54
51
  expect( popular_model ).to_not be_friends_with another_popular_model
55
52
  end
53
+
56
54
  it 'returns true if a popular_model is friends with a given popular_model' do
57
55
  popular_model.befriend another_popular_model
58
56
 
@@ -68,6 +66,25 @@ shared_examples "a popular model" do
68
66
 
69
67
  expect( popular_model).to_not be_friends_with another_popular_model
70
68
  end
69
+
70
+ it 'triggers before_unfriend callback' do
71
+ ensure_callback_fired_on popular_model_with_callbacks, :before_unfriend_callback
72
+
73
+ [:befriend, :unfriend].each do |method|
74
+ popular_model_with_callbacks.send method, another_popular_model
75
+ end
76
+ end
77
+
78
+ it 'triggers after_unfriend callback' do
79
+ ensure_callback_fired_on popular_model_with_callbacks, :after_unfriend_callback
80
+
81
+ [:befriend, :unfriend].each do |method|
82
+ popular_model_with_callbacks.send method, another_popular_model
83
+ end
84
+ end
71
85
  end
72
86
 
87
+ def ensure_callback_fired_on model, method
88
+ model.send( :should_receive, method ).once.and_return true
89
+ end
73
90
  end
@@ -18,8 +18,7 @@ describe Popular::Popular do
18
18
  let ( :random_object ) { RandomClass.new }
19
19
  let ( :popular_model ) { PopularModel.create }
20
20
  let ( :another_popular_model ) { PopularModel.create }
21
- let ( :popular_model_with_after_befriend_callback ) { PopularModelWithAfterBefriendCallback.create }
22
- let ( :popular_model_with_before_befriend_callback ) { PopularModelWithBeforeBefriendCallback.create }
21
+ let ( :popular_model_with_callbacks ) { PopularModelWithCallbacks.create }
23
22
  end
24
23
 
25
24
  end
@@ -0,0 +1,14 @@
1
+ class PopularModelWithCallbacks < PopularModel
2
+ [:after, :before].each do |filter|
3
+ [:befriend, :unfriend].each do |action|
4
+ hook = "#{filter}_#{action}".to_sym
5
+ method_name = "#{hook}_callback".to_sym
6
+
7
+ send hook, method_name
8
+
9
+ define_method method_name do
10
+ true
11
+ end
12
+ end
13
+ end
14
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: popular
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thejchap
@@ -167,8 +167,7 @@ files:
167
167
  - spec/popular/popular_spec.rb
168
168
  - spec/spec_helper.rb
169
169
  - spec/support/popular_model.rb
170
- - spec/support/popular_model_with_after_befriend_callback.rb
171
- - spec/support/popular_model_with_before_befriend_callback.rb
170
+ - spec/support/popular_model_with_callbacks.rb
172
171
  - spec/support/random_class.rb
173
172
  - spec/support/unpopular_model.rb
174
173
  homepage: http://thejchap.github.io/popular
@@ -202,8 +201,7 @@ test_files:
202
201
  - spec/popular/popular_spec.rb
203
202
  - spec/spec_helper.rb
204
203
  - spec/support/popular_model.rb
205
- - spec/support/popular_model_with_after_befriend_callback.rb
206
- - spec/support/popular_model_with_before_befriend_callback.rb
204
+ - spec/support/popular_model_with_callbacks.rb
207
205
  - spec/support/random_class.rb
208
206
  - spec/support/unpopular_model.rb
209
207
  has_rdoc:
@@ -1,7 +0,0 @@
1
- class PopularModelWithAfterBefriendCallback < PopularModel
2
- after_befriend :callback_worked
3
-
4
- def callback_worked
5
- true
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- class PopularModelWithBeforeBefriendCallback < PopularModel
2
- before_befriend :callback_worked
3
-
4
- def callback_worked
5
- true
6
- end
7
- end