popular 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NzNkYjliYTc5YjZhMTQ0YzYzZDQ3NGY0ZGI1M2VkMjRhMjBlZDcyZQ==
4
+ NWE5Mjc5N2Q5YjVlNzc2OTM5ODM0MjdlYzkwNmJlOWQ1NTI5MGQ1Mg==
5
5
  data.tar.gz: !binary |-
6
- MzdlYjQzM2UwNWJkZGI3NTVmMDVlMDgzNTg3YWMzMmM4NzUxMDY5Zg==
6
+ ZTdhZGY1MjM0MDdhNjM5NDZhODhiNTRkNzYwOTMzODRlY2EyNjM4OQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- N2YyOGQ2ZmUwNDczNzVhNjhlYjA3N2M2N2MzNjJkODQ4ODMzNWU0YjJkODcz
10
- ZWM0Yzc0OGU3OWU5OTBiMTAzMGI2YWEwMTZkNDM0MDNhZDVkOGQ2NGU0MTYy
11
- NmFhOGIwMTE5NjdjZDU2YTQ3ZGE1NjY1YWZmMWViMWNiOTU3MmY=
9
+ OWJjMTBiYjRlOGNkN2NhMmQxYjEyYWJiZGNmOWNkYmQ2ZDhmYTQzOWZkODgw
10
+ YTk5MWE1ZTFkMzNkZTliMmNkYTE1ZGUxYzE2YzJjMzNjODllNTUzNDAzODM4
11
+ YTE3MzNhMTRhNWMzOGVmOTliMDlhZjExNWJlMDA4OGYwZTY4ZTk=
12
12
  data.tar.gz: !binary |-
13
- Y2YwNTRhZDI5YWNkMzc5NjNjYjg3ZDRmZDQzNWQ0NmEzMjYwMjU1N2IzODk5
14
- ZjUxOTNiOWM5NWM0ZjZhNmIwZjY5MzU3OWFmYTc3ZjM2ZDFhZGY3ODRhYjc2
15
- NzQxZDM5ZWYyZDJjMjhmN2JhMmRkZTQwYWIxZDJmYjI2NzY1NjQ=
13
+ ZWM0NGMwM2RjOGY3N2Y0NjE5OGQ3MDc1ZTcyNmI0N2Y2MzA1MjFlZWE0ZDg2
14
+ YjNmZjQ2ZmEyMjhiZDg3ZGM3YmFjZmM2MzJjYzk5ZWUxYTgzNjRjMTlmZGRl
15
+ NzY4ODI4YTBiZTM5ZTU2NWZlNThhNzJhNmIyMTBhM2NlNTZmNTA=
@@ -3,8 +3,6 @@ require "active_record"
3
3
  # Namespace for classes and modules that handle friendship related tasks
4
4
  #
5
5
  # @author Justin Chapman
6
- #
7
- # @since 0.0.1
8
6
  module Popular
9
7
 
10
8
  if defined? ActiveRecord::Base
@@ -7,6 +7,8 @@ module Popular
7
7
  included do |base|
8
8
  has_many :friendships, class_name: 'Popular::Friendship', as: :popular_model, dependent: :destroy
9
9
  has_many :friends, through: :friendships, source_type: base
10
+ has_many :inverse_friendships, class_name: 'Popular::Friendship', as: :friend, foreign_key: :friend_id
11
+ has_many :inverse_friends, through: :inverse_friendships, source: :popular_model, source_type: base
10
12
 
11
13
  include ActiveSupport::Callbacks
12
14
  define_callbacks :befriend, :unfriend
@@ -66,7 +68,26 @@ module Popular
66
68
  end
67
69
  end
68
70
 
69
- # Helper method for finding whether or not the instance is friends with
71
+ # Helper method for finding whether or not the instance has
72
+ # been befriended by another given popular_model
73
+ #
74
+ # @param [Object] popular_model
75
+ # @return [Boolean] if the instance has been friended by another popular_model
76
+ #
77
+ # @example
78
+ # user = User.create name: "Justin"
79
+ # other_user = User.create name: "Jenny"
80
+ #
81
+ # user.friended_by? other_user #=> false
82
+ #
83
+ # other_user.befriend user
84
+ #
85
+ # user.friended_by? other_user #=> true
86
+ def friended_by? popular_model
87
+ inverse_friends.include? popular_model
88
+ end
89
+
90
+ # Helper method for finding whether or not the instance has befriended
70
91
  # another given popular_model
71
92
  #
72
93
  # @param [Object] popular_model
@@ -82,7 +103,7 @@ module Popular
82
103
  #
83
104
  # user.friends_with? other_user #=> true
84
105
  def friends_with? popular_model
85
- friendships.where( friend: popular_model ).any?
106
+ friends.include? popular_model
86
107
  end
87
108
 
88
109
  # ClassMethods included in popular models
@@ -90,8 +111,6 @@ module Popular
90
111
 
91
112
  # after_unfriend callback convenience class method
92
113
  #
93
- # @since 0.4.0
94
- #
95
114
  # @example
96
115
  #
97
116
  # class User < ActiveRecord::Base
@@ -113,8 +132,6 @@ module Popular
113
132
 
114
133
  # before_unfriend callback convenience class method
115
134
  #
116
- # @since 0.4.0
117
- #
118
135
  # @example
119
136
  #
120
137
  # class User < ActiveRecord::Base
@@ -136,8 +153,6 @@ module Popular
136
153
 
137
154
  # before_befriend callback convenience class method
138
155
  #
139
- # @since 0.3.0
140
- #
141
156
  # @example
142
157
  #
143
158
  # class User < ActiveRecord::Base
@@ -158,8 +173,6 @@ module Popular
158
173
 
159
174
  # after_befriend callback convenience class method
160
175
  #
161
- # @since 0.3.0
162
- #
163
176
  # @example
164
177
  #
165
178
  # class User < ActiveRecord::Base
@@ -1,3 +1,3 @@
1
1
  module Popular
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -1,5 +1,13 @@
1
1
  shared_examples "a popular model" do
2
2
 
3
+ describe 'mutual friendships' do
4
+ it '.inverse_friends includes popular_models who have added an instance as a friend' do
5
+ another_popular_model.befriend popular_model
6
+
7
+ expect( popular_model ).to be_friended_by another_popular_model
8
+ end
9
+ end
10
+
3
11
  describe '.befriend!' do
4
12
  it 'creates a one way friendship' do
5
13
  popular_model.befriend! another_popular_model
@@ -15,7 +15,6 @@ describe Popular::Popular do
15
15
  end
16
16
 
17
17
  it_behaves_like 'a popular model' do
18
- let ( :random_object ) { RandomClass.new }
19
18
  let ( :popular_model ) { PopularModel.create }
20
19
  let ( :another_popular_model ) { PopularModel.create }
21
20
  let ( :popular_model_with_callbacks ) { PopularModelWithCallbacks.create }
@@ -6,9 +6,7 @@ class PopularModelWithCallbacks < PopularModel
6
6
 
7
7
  send hook, method_name
8
8
 
9
- define_method method_name do
10
- true
11
- end
9
+ define_method method_name, ->{}
12
10
  end
13
11
  end
14
12
  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.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thejchap
@@ -168,7 +168,6 @@ files:
168
168
  - spec/spec_helper.rb
169
169
  - spec/support/popular_model.rb
170
170
  - spec/support/popular_model_with_callbacks.rb
171
- - spec/support/random_class.rb
172
171
  - spec/support/unpopular_model.rb
173
172
  homepage: http://thejchap.github.io/popular
174
173
  licenses:
@@ -202,6 +201,5 @@ test_files:
202
201
  - spec/spec_helper.rb
203
202
  - spec/support/popular_model.rb
204
203
  - spec/support/popular_model_with_callbacks.rb
205
- - spec/support/random_class.rb
206
204
  - spec/support/unpopular_model.rb
207
205
  has_rdoc:
@@ -1 +0,0 @@
1
- RandomClass = Class.new