popular 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.inch.yml +3 -0
- data/README.md +6 -3
- data/lib/popular/extenders/popular.rb +1 -1
- data/lib/popular/friendship.rb +15 -0
- data/lib/popular/popular.rb +2 -1
- data/lib/popular/version.rb +1 -1
- data/spec/popular/friendship_model_spec.rb +24 -0
- data/spec/popular/friendship_spec.rb +10 -0
- metadata +6 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MDBkZDk2OTY1OTczYjYzMzkzOTVmN2QyZDMyMjYwNmE1ZjBjOWYxOA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NTA3NTI1MmUwY2UzMzk1MWY1MTMxM2FmZGQxNzE4NTZmMGVjY2ZhZA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZDc4MDI4NjI3OWNlNmM4MzRmNzVmMzYxZDBjYWRhYzFjNDgxM2E2NWExMDRk
|
10
|
+
ZjgwNjZmMDAyYzQzOGE3NzkxMDZjNjA3MWVlZTVkMzIyZDk4NjllMjQ4YzRj
|
11
|
+
NDU5ZjZhMjMxZGE4OGM2Njk2NGM0NGE5ZGRjNGU3ZDQ4MTMxMWU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OThiOGJlNDljMmUzMTNmOTI4NGNkYmFmYzliOWJiNzgxODAwYzEzODljZGUx
|
14
|
+
N2YzNjg3NTVlODFjN2NhZjUyMDc1MzgzNTIyOGFhMTIxYTM0NGE5OTYyZjA4
|
15
|
+
MDI3Y2E5YmI1MmVkOTJhYjZhZDlmMTlmMWZlNjEwMjFiNzM3OTA=
|
data/.inch.yml
ADDED
data/README.md
CHANGED
@@ -44,11 +44,14 @@ end
|
|
44
44
|
@sam = User.create name: "Samuel"
|
45
45
|
@jackson = User.create name: "Jackson"
|
46
46
|
|
47
|
-
|
47
|
+
# Adding and removing friends
|
48
|
+
@sam.friends_with? @jackson #=> false
|
49
|
+
@sam.friends.length #=> 0
|
48
50
|
@sam.befriend @jackson
|
49
|
-
@sam.friends_with? @jackson
|
51
|
+
@sam.friends_with? @jackson #=> true
|
52
|
+
@sam.friends.length #=> 1
|
50
53
|
@sam.unfriend @jackson
|
51
|
-
@sam.friends_with? @jackson
|
54
|
+
@sam.friends_with? @jackson #=> false
|
52
55
|
```
|
53
56
|
|
54
57
|
### Callbacks
|
@@ -2,7 +2,7 @@ module Popular
|
|
2
2
|
# Namespace for classes and modules that handle including the library's modules
|
3
3
|
module Extenders
|
4
4
|
|
5
|
-
# Namespace for classes and modules that handle making a given model
|
5
|
+
# Namespace for classes and modules that handle making a given model popular
|
6
6
|
module Popular
|
7
7
|
|
8
8
|
# Helper method for determining whether or not a model has included
|
data/lib/popular/friendship.rb
CHANGED
@@ -1,10 +1,25 @@
|
|
1
1
|
module Popular
|
2
2
|
|
3
|
+
# Friendship class. Provides self-referential join for popular_models and their friends
|
3
4
|
class Friendship < ::ActiveRecord::Base
|
4
5
|
|
5
6
|
belongs_to :popular_model, polymorphic: true
|
6
7
|
belongs_to :friend, polymorphic: true
|
7
8
|
|
9
|
+
validates_presence_of :friend, :popular_model
|
10
|
+
|
11
|
+
# Ensures that popular_models cannot add themselves as a friend
|
12
|
+
validate :ensure_popular_model_different_from_friend
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
# Checks friend against popular_model.
|
17
|
+
#
|
18
|
+
# TODO add more descriptive error message/i18n
|
19
|
+
def ensure_popular_model_different_from_friend
|
20
|
+
errors.add( :friend ) if friend == popular_model
|
21
|
+
end
|
22
|
+
|
8
23
|
end
|
9
24
|
|
10
25
|
end
|
data/lib/popular/popular.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Popular
|
2
|
-
|
2
|
+
# Namespace for methods included in popular models
|
3
3
|
module Popular
|
4
4
|
|
5
5
|
extend ActiveSupport::Concern
|
@@ -83,6 +83,7 @@ module Popular
|
|
83
83
|
friendships.where( friend: popular_model ).any?
|
84
84
|
end
|
85
85
|
|
86
|
+
# ClassMethods included in popular models
|
86
87
|
module ClassMethods
|
87
88
|
|
88
89
|
# before_befriend callback convenience class method
|
data/lib/popular/version.rb
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
shared_examples 'a friendship model' do
|
2
|
+
|
3
|
+
it 'ensures the friend is not the same as the popular_model' do
|
4
|
+
invalid_model = described_class.new(
|
5
|
+
friend: popular_model,
|
6
|
+
popular_model: popular_model
|
7
|
+
)
|
8
|
+
|
9
|
+
expect( invalid_model ).to_not be_valid
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'validates presence of popular_model' do
|
13
|
+
invalid_model = described_class.new friend: popular_model
|
14
|
+
|
15
|
+
expect( invalid_model ).to_not be_valid
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'validates presence of friend' do
|
19
|
+
invalid_model = described_class.new popular_model: popular_model
|
20
|
+
|
21
|
+
expect( invalid_model ).to_not be_valid
|
22
|
+
end
|
23
|
+
|
24
|
+
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.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- thejchap
|
@@ -146,6 +146,7 @@ extra_rdoc_files: []
|
|
146
146
|
files:
|
147
147
|
- .gemrelease
|
148
148
|
- .gitignore
|
149
|
+
- .inch.yml
|
149
150
|
- .rspec
|
150
151
|
- .travis.yml
|
151
152
|
- Gemfile
|
@@ -160,6 +161,8 @@ files:
|
|
160
161
|
- lib/popular/popular.rb
|
161
162
|
- lib/popular/version.rb
|
162
163
|
- popular.gemspec
|
164
|
+
- spec/popular/friendship_model_spec.rb
|
165
|
+
- spec/popular/friendship_spec.rb
|
163
166
|
- spec/popular/popular_model_spec.rb
|
164
167
|
- spec/popular/popular_spec.rb
|
165
168
|
- spec/spec_helper.rb
|
@@ -193,6 +196,8 @@ signing_key:
|
|
193
196
|
specification_version: 4
|
194
197
|
summary: Friendship gem for Rails/ActiveRecord
|
195
198
|
test_files:
|
199
|
+
- spec/popular/friendship_model_spec.rb
|
200
|
+
- spec/popular/friendship_spec.rb
|
196
201
|
- spec/popular/popular_model_spec.rb
|
197
202
|
- spec/popular/popular_spec.rb
|
198
203
|
- spec/spec_helper.rb
|