popular 0.6.0 → 0.7.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
- ZGExM2NmNGUxNmI3ZTk5MjgwMDE2OTJiN2E3NWY0Njk4NTgxMjZiZg==
4
+ NWU5MzFlMWEwZGQyYTMxNTU1ZWQ4OTgxYzA0MmZhMzYzMDhkMDczNg==
5
5
  data.tar.gz: !binary |-
6
- OWY2NTBmYzRlMmI3NDIzMTU5YmI4MzVmMGVhNTg3MzdmZGQ2NzkyYg==
6
+ M2ZiMmI2ZmM0NmY5ZmUxYzliYzRmYTIzZjcwODE4OGI4ZjNkMGZkMw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NjYxZjRhZGI3YTI0NmNiY2Q1NDZhNWVjYzQ2MDdiOTEyYjY2YzJmMTZkZTEz
10
- MzgwY2ZmNWNiYTUwZjk4ZjBiZTkxNzFlZjU4ZmUwNjJkNWRhZDI4ZDZiODc3
11
- YjJhMmU4M2U1YTZjN2VlMzQ5NjE2NzI3YzcwOTNhN2FjMzI0MjY=
9
+ ZGIzNDViYzY0MDUyMmExMTMxMWQ5YzBkZDRlYjM4NDNlNjRiNmVhNjBkNmY0
10
+ YTI5YmI5ZDYxM2E0MWMwOTlhMTNkZjUxMDA1NDY0OWM5OWJkNGEwYTU0NGNh
11
+ MjAxODMxNzlmYmM3YzhlZmE3YzQ1Y2VkNmEzODA0ODc4ODcxNWQ=
12
12
  data.tar.gz: !binary |-
13
- OWMyZTZmM2U0Yjc3MmMyMTU0MGJlNGM5YmZiODQxNzZjZDdmYzkzNmFkMGY3
14
- NGEzYjljYjU1MTk3NDczNmJjZjU1YjZmYWNhOTQxZmJiMTIyNjFjNDQ1Yzlh
15
- NTA4NTAyZjhlNDEyOWRmNzMzZTZiMWFlYmUyZDE0YjMxMTAyN2E=
13
+ NzU0YzA1ZDFkN2QxYjM3YWQ2NTFmZmM0MjVkNGM1MDllNTVhNTFhNzQzMTQz
14
+ OTk2N2UxMjRhNmM5OTY5MGUyOGFmZTFhMDNhYTNhMjI4YTg2ZWU3ODRjNWZh
15
+ ZWQ0ZmVkZWE5ZDRmZTZjOGI3ZTJiMWViNjdlODQ2YTU2M2I5NTE=
data/README.md CHANGED
@@ -110,6 +110,33 @@ end
110
110
  @justin.unfriend @jenny #=> ":("
111
111
  ```
112
112
 
113
+ ### Customization
114
+
115
+ Popular is intended to provide basic utilities around self-referential relationships in social apps.
116
+ Often, more customization is necessary. If you would like to store more information around the friendship,
117
+ Popular provides a hook to connect its `friendship` model to a user defined `friendship_profile` model.
118
+ This allows Popular to remain somewhat lightweight and factor out only the code that is repeated alot between apps,
119
+ while allowing flexibility where it is needed.
120
+
121
+ Do to this, create a `FriendshipProfile` model that belongs to `friendship`, and has whatever custom attributes
122
+ you want
123
+
124
+ ```ruby
125
+ rails g model FriendshipProfile friendship:belongs_to meeting_location:string meeting_latitude:float meeting_longitude:float
126
+ rake db:migrate
127
+ ```
128
+
129
+ Then, in your Popular model, just set the `friendship_profile` option to true:
130
+
131
+ ```ruby
132
+ class User < ActiveRecord::Base
133
+ popular friendship_profile: true
134
+ end
135
+ ```
136
+
137
+ Now, everytime a `friendship` is created, an accompanying `friendship_profile` will be attached to it, allowing you to define
138
+ as many custom attributes as you wish in a separate table
139
+
113
140
  ## Related gems
114
141
 
115
142
  If Popular isn't quite what you're looking for, here are some other useful gems in the same category:
@@ -15,12 +15,17 @@ module Popular
15
15
 
16
16
  # Includes the module in a given class
17
17
  #
18
- # @overload friendable *args
18
+ # @overload popular *args
19
19
  # @param [Hash] options
20
+ # @option args [Boolean] :friendship_profile whether or not friendships should create a friendship_profile
20
21
  def popular *args
21
22
  require 'popular/popular'
22
23
  include ::Popular::Popular
23
-
24
+
25
+ args.extract_options!.each do |key, val|
26
+ send "#{key}=", val
27
+ end
28
+
24
29
  class_eval do
25
30
  def self.popular?
26
31
  true
@@ -2,17 +2,31 @@ module Popular
2
2
 
3
3
  # Friendship class. Provides self-referential join for popular_models and their friends
4
4
  class Friendship < ::ActiveRecord::Base
5
-
6
5
  belongs_to :popular_model, polymorphic: true
7
6
  belongs_to :friend, polymorphic: true
7
+ has_one :friendship_profile
8
8
 
9
9
  validates_presence_of :friend, :popular_model
10
10
 
11
11
  # Ensures that popular_models cannot add themselves as a friend
12
12
  validate :ensure_popular_model_different_from_friend
13
13
 
14
+ # If the user has created a friendship profile table, this will get attached
15
+ # after create
16
+ after_create :create_friendship_profile!,
17
+ if: Proc.new { |friendship| friendship.popular_model.class.friendship_profile }
18
+
14
19
  private
15
20
 
21
+ def create_friendship_profile!
22
+ if defined? FriendshipProfile
23
+ super
24
+ else
25
+ warn "You have set friendship_profile: true in your popular model, however no FriendshipProfile model exists"
26
+ warn "To fix this, run 'rails g model FriendshipProfile friendship:belongs_to'"
27
+ end
28
+ end
29
+
16
30
  # Checks friend against popular_model.
17
31
  #
18
32
  # TODO add more descriptive error message/i18n
@@ -1,4 +1,5 @@
1
1
  module Popular
2
+
2
3
  # Namespace for methods included in popular models
3
4
  module Popular
4
5
 
@@ -145,12 +146,15 @@ module Popular
145
146
 
146
147
  # ClassMethods included in popular models
147
148
  module ClassMethods
149
+ attr_accessor :friendship_profile
148
150
 
149
151
  # after_unfriend callback convenience class method
152
+ # Fired after a popular_model unfriends another popular_model
150
153
  #
151
154
  # @example
152
155
  #
153
156
  # class User < ActiveRecord::Base
157
+ # popular
154
158
  # after_unfriend :do_something_amazing
155
159
  #
156
160
  # def do_something_amazing
@@ -168,10 +172,12 @@ module Popular
168
172
  end
169
173
 
170
174
  # before_unfriend callback convenience class method
175
+ # Fired before a popular_model unfriends another popular_model
171
176
  #
172
177
  # @example
173
178
  #
174
179
  # class User < ActiveRecord::Base
180
+ # popular
175
181
  # before_unfriend :do_something_amazing
176
182
  #
177
183
  # def do_something_amazing
@@ -189,10 +195,12 @@ module Popular
189
195
  end
190
196
 
191
197
  # before_befriend callback convenience class method
198
+ # Fired before a popular model befriends another popular_model
192
199
  #
193
200
  # @example
194
201
  #
195
202
  # class User < ActiveRecord::Base
203
+ # popular
196
204
  # before_befriend :do_something_amazing
197
205
  #
198
206
  # def do_something_amazing
@@ -209,10 +217,12 @@ module Popular
209
217
  end
210
218
 
211
219
  # after_befriend callback convenience class method
220
+ # Fired after a popular_model befriends another popular_model
212
221
  #
213
222
  # @example
214
223
  #
215
224
  # class User < ActiveRecord::Base
225
+ # popular
216
226
  # after_befriend :do_something_amazing
217
227
  #
218
228
  # def do_something_amazing
@@ -1,3 +1,3 @@
1
1
  module Popular
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -1,5 +1,19 @@
1
1
  shared_examples 'a friendship model' do
2
2
 
3
+ describe 'custom data' do
4
+ it 'allows for a one-to-one mapping between friendship and a friendship_profile' do
5
+ friendship = custom_popular_model.befriend another_custom_popular_model
6
+
7
+ expect( friendship.friendship_profile ).to_not be_nil
8
+ end
9
+
10
+ it 'model does not get created unless explicitly opted in' do
11
+ friendship = popular_model.befriend another_popular_model
12
+
13
+ expect( friendship.friendship_profile ).to be_nil
14
+ end
15
+ end
16
+
3
17
  it 'ensures the friend is not the same as the popular_model' do
4
18
  invalid_model = described_class.new(
5
19
  friend: popular_model,
@@ -5,6 +5,8 @@ describe Popular::Friendship do
5
5
  it_behaves_like 'a friendship model' do
6
6
  let ( :popular_model ) { PopularModel.create }
7
7
  let ( :another_popular_model ) { PopularModel.create }
8
+ let ( :custom_popular_model ) { CustomPopularModel.create }
9
+ let ( :another_custom_popular_model ) { CustomPopularModel.create }
8
10
  end
9
11
 
10
12
  end
@@ -18,6 +18,10 @@ ActiveRecord::Schema.define version: 1 do
18
18
  end
19
19
 
20
20
  create_table :popular_models
21
+ create_table :custom_popular_models
22
+ create_table :friendship_profiles do |t|
23
+ t.references :friendship
24
+ end
21
25
 
22
26
  end
23
27
 
@@ -26,7 +30,7 @@ RSpec.configure do |config|
26
30
  end
27
31
 
28
32
  def clean_database
29
- [PopularModel, Popular::Friendship].each do |model|
33
+ [CustomPopularModel, PopularModel, Popular::Friendship].each do |model|
30
34
  ActiveRecord::Base.connection.execute "DELETE FROM #{model.table_name}"
31
35
  end
32
36
  end
@@ -0,0 +1,3 @@
1
+ class CustomPopularModel < ActiveRecord::Base
2
+ popular friendship_profile: true
3
+ end
@@ -0,0 +1,2 @@
1
+ class FriendshipProfile < ActiveRecord::Base
2
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: popular
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thejchap
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-30 00:00:00.000000000 Z
11
+ date: 2014-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -166,6 +166,8 @@ files:
166
166
  - spec/popular/popular_model_spec.rb
167
167
  - spec/popular/popular_spec.rb
168
168
  - spec/spec_helper.rb
169
+ - spec/support/custom_popular_model.rb
170
+ - spec/support/friendship_profile.rb
169
171
  - spec/support/popular_model.rb
170
172
  - spec/support/popular_model_with_callbacks.rb
171
173
  - spec/support/unpopular_model.rb
@@ -199,6 +201,8 @@ test_files:
199
201
  - spec/popular/popular_model_spec.rb
200
202
  - spec/popular/popular_spec.rb
201
203
  - spec/spec_helper.rb
204
+ - spec/support/custom_popular_model.rb
205
+ - spec/support/friendship_profile.rb
202
206
  - spec/support/popular_model.rb
203
207
  - spec/support/popular_model_with_callbacks.rb
204
208
  - spec/support/unpopular_model.rb