popular 0.1.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/popular/popular.rb +61 -8
- data/lib/popular/version.rb +1 -1
- data/popular.gemspec +1 -1
- data/spec/popular/popular_model_spec.rb +38 -5
- data/spec/popular/popular_spec.rb +3 -0
- data/spec/spec_helper.rb +2 -6
- data/spec/support/popular_model.rb +3 -0
- data/spec/support/popular_model_with_after_befriend_callback.rb +7 -0
- data/spec/support/popular_model_with_before_befriend_callback.rb +7 -0
- data/spec/support/random_class.rb +1 -0
- data/spec/support/unpopular_model.rb +1 -0
- metadata +13 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NDZkMzQzZDk2OTIzYTc3ZjMzNzA1NWU0NzQ1MTA3ZjcwYzJhNDYzYw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
OWM2YjUyZTExMTVjYWUwYjJmMTUyNWFiZWNjNjA4ZGQ3N2U3MjQwYg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZmFkZjJmZWJkOWU0ZTFhNmJjZDBmNjY1OTJhYmM2YjY3YjI0M2VkNmY2YjYx
|
10
|
+
ZGU0MWJjNTFlYzllYTZkNzFiNDQ4MTAzMjgxNWM0ZWY0NTdmOTk5MDQwMzU5
|
11
|
+
OTdjYWRhYmRjYWJmMWU2MTEzODljYTU1ZDBjYzdhM2MwOWFjYTg=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NDRjZThlZWM2YWQ5MWFmOWFhNWYyMDE5OTFlNTJjOGIwZjkwOTc3YzUyMTZm
|
14
|
+
ZTk5MDk3M2IwYmIzZWEwNThhYTFiYWNhOTViMTI0OThjNWZlZjVlZDIyNDU0
|
15
|
+
MzE3N2ZmZTJkNzc3YTE1ZjgyZGVhZTJhNGVlMmM2NzAyYTgyZGE=
|
data/lib/popular/popular.rb
CHANGED
@@ -2,15 +2,18 @@ module Popular
|
|
2
2
|
|
3
3
|
module Popular
|
4
4
|
|
5
|
-
|
5
|
+
extend ActiveSupport::Concern
|
6
6
|
|
7
|
-
|
7
|
+
included do |base|
|
8
|
+
has_many :friendships, class_name: 'Popular::Friendship', as: :popular_model, dependent: :destroy
|
9
|
+
has_many :friends, through: :friendships, source_type: base
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
+
include ActiveSupport::Callbacks
|
12
|
+
define_callbacks :befriend
|
11
13
|
|
14
|
+
[:before_befriend, :after_befriend].each do |callback|
|
15
|
+
send callback
|
12
16
|
end
|
13
|
-
|
14
17
|
end
|
15
18
|
|
16
19
|
# Adds a friend to an instance's friend's list
|
@@ -24,7 +27,9 @@ module Popular
|
|
24
27
|
#
|
25
28
|
# user.friends_with? other_user #=> true
|
26
29
|
def befriend new_friend
|
27
|
-
|
30
|
+
run_callbacks :befriend do
|
31
|
+
friendships.create friend: new_friend
|
32
|
+
end
|
28
33
|
end
|
29
34
|
|
30
35
|
# Adds a friend to an instance's friend's list
|
@@ -36,10 +41,12 @@ module Popular
|
|
36
41
|
# user = User.create name: "Justin"
|
37
42
|
# other_user = User.create name: "Jenny"
|
38
43
|
# user.befriend! other_user
|
39
|
-
#
|
44
|
+
#
|
40
45
|
# user.friends_with? other_user # => true
|
41
46
|
def befriend! new_friend
|
42
|
-
|
47
|
+
run_callbacks :befriend do
|
48
|
+
friendships.create! friend: new_friend
|
49
|
+
end
|
43
50
|
end
|
44
51
|
|
45
52
|
# Removes a friend from an instance's friend's list
|
@@ -76,6 +83,52 @@ module Popular
|
|
76
83
|
friendships.where( friend: popular_model ).any?
|
77
84
|
end
|
78
85
|
|
86
|
+
module ClassMethods
|
87
|
+
|
88
|
+
# before_befriend callback convenience class method
|
89
|
+
#
|
90
|
+
# @since 0.3.0
|
91
|
+
#
|
92
|
+
# @example
|
93
|
+
#
|
94
|
+
# class User < ActiveRecord::Base
|
95
|
+
# before_befriend :do_something_amazing
|
96
|
+
#
|
97
|
+
# def do_something_amazing
|
98
|
+
# puts name
|
99
|
+
# end
|
100
|
+
# end
|
101
|
+
#
|
102
|
+
# user = User.create name: "Justin"
|
103
|
+
# another_user = User.create name: "Jenny"
|
104
|
+
#
|
105
|
+
# user.befriend another_user #=> "Justin"
|
106
|
+
def before_befriend *args, &blk
|
107
|
+
set_callback :befriend, :before, *args, &blk
|
108
|
+
end
|
109
|
+
|
110
|
+
# after_befriend callback convenience class method
|
111
|
+
#
|
112
|
+
# @since 0.3.0
|
113
|
+
#
|
114
|
+
# @example
|
115
|
+
#
|
116
|
+
# class User < ActiveRecord::Base
|
117
|
+
# after_befriend :do_something_amazing
|
118
|
+
#
|
119
|
+
# def do_something_amazing
|
120
|
+
# puts name
|
121
|
+
# end
|
122
|
+
# end
|
123
|
+
#
|
124
|
+
# user = User.create name: "Justin"
|
125
|
+
# another_user = User.create name: "Jenny"
|
126
|
+
#
|
127
|
+
# user.befriend another_user #=> "Justin"
|
128
|
+
def after_befriend *args, &blk
|
129
|
+
set_callback :befriend, :after, *args, &blk
|
130
|
+
end
|
131
|
+
end
|
79
132
|
end
|
80
133
|
|
81
134
|
end
|
data/lib/popular/version.rb
CHANGED
data/popular.gemspec
CHANGED
@@ -26,5 +26,5 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.add_development_dependency "yard", "~> 0.8"
|
27
27
|
spec.add_development_dependency "redcarpet", "~> 3.1"
|
28
28
|
spec.add_development_dependency "inch", "~> 0.3"
|
29
|
-
spec.add_development_dependency "gem-release", '~> 0.7
|
29
|
+
spec.add_development_dependency "gem-release", '~> 0.7'
|
30
30
|
end
|
@@ -4,16 +4,49 @@ shared_examples "a popular model" do
|
|
4
4
|
it 'creates a one way friendship' do
|
5
5
|
popular_model.befriend! another_popular_model
|
6
6
|
|
7
|
-
expect( popular_model
|
7
|
+
expect( popular_model).to be_friends_with another_popular_model
|
8
|
+
end
|
9
|
+
|
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
|
15
|
+
|
16
|
+
popular_model_with_before_befriend_callback.befriend! another_popular_model
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'successful' do
|
20
|
+
it 'triggers after_befriend callback' do
|
21
|
+
popular_model_with_after_befriend_callback
|
22
|
+
.should_receive( :callback_worked )
|
23
|
+
.once
|
24
|
+
.and_return true
|
25
|
+
|
26
|
+
popular_model_with_after_befriend_callback.befriend! another_popular_model
|
27
|
+
end
|
8
28
|
end
|
9
29
|
end
|
10
30
|
|
11
31
|
describe '.befriend' do
|
12
|
-
it 'creates a one way friendship' do
|
13
|
-
popular_model.befriend another_popular_model
|
14
32
|
|
15
|
-
|
33
|
+
context 'successful' do
|
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
|
39
|
+
|
40
|
+
popular_model_with_after_befriend_callback.befriend another_popular_model
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'creates a one way friendship' do
|
44
|
+
popular_model.befriend another_popular_model
|
45
|
+
|
46
|
+
expect( popular_model).to be_friends_with another_popular_model
|
47
|
+
end
|
16
48
|
end
|
49
|
+
|
17
50
|
end
|
18
51
|
|
19
52
|
describe '#friends_with?' do
|
@@ -33,7 +66,7 @@ shared_examples "a popular model" do
|
|
33
66
|
popular_model.send method, another_popular_model
|
34
67
|
end
|
35
68
|
|
36
|
-
expect( popular_model
|
69
|
+
expect( popular_model).to_not be_friends_with another_popular_model
|
37
70
|
end
|
38
71
|
end
|
39
72
|
|
@@ -15,8 +15,11 @@ describe Popular::Popular do
|
|
15
15
|
end
|
16
16
|
|
17
17
|
it_behaves_like 'a popular model' do
|
18
|
+
let ( :random_object ) { RandomClass.new }
|
18
19
|
let ( :popular_model ) { PopularModel.create }
|
19
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 }
|
20
23
|
end
|
21
24
|
|
22
25
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -6,6 +6,8 @@ end
|
|
6
6
|
require 'sqlite3'
|
7
7
|
require 'popular'
|
8
8
|
|
9
|
+
Dir["./spec/support/**/*.rb"].sort.each { |f| require f }
|
10
|
+
|
9
11
|
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
10
12
|
|
11
13
|
ActiveRecord::Schema.define version: 1 do
|
@@ -19,12 +21,6 @@ ActiveRecord::Schema.define version: 1 do
|
|
19
21
|
|
20
22
|
end
|
21
23
|
|
22
|
-
UnpopularModel = Class.new ActiveRecord::Base
|
23
|
-
|
24
|
-
class PopularModel < ActiveRecord::Base
|
25
|
-
popular
|
26
|
-
end
|
27
|
-
|
28
24
|
RSpec.configure do |config|
|
29
25
|
config.order = 'random'
|
30
26
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
RandomClass = Class.new
|
@@ -0,0 +1 @@
|
|
1
|
+
UnpopularModel = Class.new ActiveRecord::Base
|
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
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- thejchap
|
@@ -128,14 +128,14 @@ dependencies:
|
|
128
128
|
requirements:
|
129
129
|
- - ~>
|
130
130
|
- !ruby/object:Gem::Version
|
131
|
-
version: 0.7
|
131
|
+
version: '0.7'
|
132
132
|
type: :development
|
133
133
|
prerelease: false
|
134
134
|
version_requirements: !ruby/object:Gem::Requirement
|
135
135
|
requirements:
|
136
136
|
- - ~>
|
137
137
|
- !ruby/object:Gem::Version
|
138
|
-
version: 0.7
|
138
|
+
version: '0.7'
|
139
139
|
description: Friendship gem for Rails/ActiveRecord. Popular saves time when developing
|
140
140
|
social apps
|
141
141
|
email:
|
@@ -161,6 +161,11 @@ files:
|
|
161
161
|
- spec/popular/popular_model_spec.rb
|
162
162
|
- spec/popular/popular_spec.rb
|
163
163
|
- spec/spec_helper.rb
|
164
|
+
- spec/support/popular_model.rb
|
165
|
+
- spec/support/popular_model_with_after_befriend_callback.rb
|
166
|
+
- spec/support/popular_model_with_before_befriend_callback.rb
|
167
|
+
- spec/support/random_class.rb
|
168
|
+
- spec/support/unpopular_model.rb
|
164
169
|
homepage: http://thejchap.github.io/popular
|
165
170
|
licenses:
|
166
171
|
- MIT
|
@@ -189,4 +194,9 @@ test_files:
|
|
189
194
|
- spec/popular/popular_model_spec.rb
|
190
195
|
- spec/popular/popular_spec.rb
|
191
196
|
- spec/spec_helper.rb
|
197
|
+
- spec/support/popular_model.rb
|
198
|
+
- spec/support/popular_model_with_after_befriend_callback.rb
|
199
|
+
- spec/support/popular_model_with_before_befriend_callback.rb
|
200
|
+
- spec/support/random_class.rb
|
201
|
+
- spec/support/unpopular_model.rb
|
192
202
|
has_rdoc:
|