rating 0.1.0 → 0.2.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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +7 -0
  3. data/README.md +111 -5
  4. data/lib/generators/rating/install_generator.rb +7 -3
  5. data/lib/generators/rating/templates/db/migrate/create_rating_tables.rb +10 -6
  6. data/lib/rating/models/rating/extension.rb +32 -13
  7. data/lib/rating/models/rating/rate.rb +9 -8
  8. data/lib/rating/models/rating/rating.rb +34 -17
  9. data/lib/rating/version.rb +1 -1
  10. data/spec/factories/article.rb +1 -1
  11. data/spec/factories/author.rb +7 -0
  12. data/spec/factories/category.rb +7 -0
  13. data/spec/factories/user.rb +1 -1
  14. data/spec/models/extension/after_create_spec.rb +16 -7
  15. data/spec/models/extension/order_by_rating_spec.rb +75 -0
  16. data/spec/models/extension/rate_for_spec.rb +15 -3
  17. data/spec/models/extension/rate_spec.rb +15 -3
  18. data/spec/models/extension/rated_question_spec.rb +24 -6
  19. data/spec/models/extension/rated_records_spec.rb +26 -0
  20. data/spec/models/extension/rated_spec.rb +33 -13
  21. data/spec/models/extension/rates_records_spec.rb +26 -0
  22. data/spec/models/extension/rates_spec.rb +33 -17
  23. data/spec/models/extension/rating_records_spec.rb +28 -0
  24. data/spec/models/extension/rating_spec.rb +31 -15
  25. data/spec/models/rate/create_spec.rb +104 -37
  26. data/spec/models/rate/rate_for_spec.rb +26 -6
  27. data/spec/models/rate_spec.rb +2 -1
  28. data/spec/models/rating/averager_data_spec.rb +24 -5
  29. data/spec/models/rating/data_spec.rb +38 -11
  30. data/spec/models/rating/update_rating_spec.rb +24 -6
  31. data/spec/models/rating/values_data_spec.rb +31 -8
  32. data/spec/models/rating_spec.rb +1 -0
  33. data/spec/support/db/migrate/create_authors_table.rb +9 -0
  34. data/spec/support/db/migrate/create_category_spec.rb +9 -0
  35. data/spec/support/migrate.rb +3 -1
  36. data/spec/support/models/author.rb +5 -0
  37. data/spec/support/models/category.rb +4 -0
  38. metadata +22 -20
  39. data/spec/support/html_matchers.rb +0 -7
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rating
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  FactoryBot.define do
4
4
  factory :article do
5
- sequence(:name) { |i| "Name #{i}" }
5
+ sequence(:name) { |i| "Article #{i}" }
6
6
  end
7
7
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ FactoryBot.define do
4
+ factory :author do
5
+ sequence(:name) { |i| "Author #{i}" }
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ FactoryBot.define do
4
+ factory :category do
5
+ sequence(:name) { |i| "Category #{i}" }
6
+ end
7
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  FactoryBot.define do
4
4
  factory :user do
5
- sequence(:name) { |i| "Name #{i}" }
5
+ sequence(:name) { |i| "User #{i}" }
6
6
  end
7
7
  end
@@ -3,17 +3,26 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  RSpec.describe Rating::Extension, ':after_create' do
6
- context 'when creates object' do
6
+ context 'with :as as nil' do
7
7
  let!(:user) { create :user }
8
8
 
9
- it 'creates a record with zero values just to be easy to make the count' do
9
+ it 'creates a rating record with zero values just to be easy to make the count' do
10
10
  rating = Rating::Rating.find_by(resource: user)
11
11
 
12
- expect(rating.average).to eq 0
13
- expect(rating.estimate).to eq 0
14
- expect(rating.resource).to eq user
15
- expect(rating.sum).to eq 0
16
- expect(rating.total).to eq 0
12
+ expect(rating.average).to eq 0
13
+ expect(rating.estimate).to eq 0
14
+ expect(rating.resource).to eq user
15
+ expect(rating.scopeable).to eq nil
16
+ expect(rating.sum).to eq 0
17
+ expect(rating.total).to eq 0
18
+ end
19
+ end
20
+
21
+ context 'with :as as :author' do
22
+ let!(:author) { create :author }
23
+
24
+ it 'does not creates a rating record' do
25
+ expect(Rating::Rating.exists?(resource: author)).to eq false
17
26
  end
18
27
  end
19
28
  end
@@ -3,6 +3,8 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  RSpec.describe Rating::Extension, ':order_by_rating' do
6
+ let!(:category) { create :category }
7
+
6
8
  let!(:user_1) { create :user }
7
9
  let!(:user_2) { create :user }
8
10
 
@@ -15,6 +17,9 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
15
17
  create :rating_rate, author: user_1, resource: article_2, value: 11
16
18
  create :rating_rate, author: user_1, resource: article_3, value: 10
17
19
  create :rating_rate, author: user_2, resource: article_1, value: 1
20
+
21
+ create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1
22
+ create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2
18
23
  end
19
24
 
20
25
  context 'with default filters' do
@@ -36,6 +41,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
36
41
  article_1
37
42
  ]
38
43
  end
44
+
45
+ context 'with scope' do
46
+ it 'works' do
47
+ expect(Article.order_by_rating(:average, :asc, scope: category)).to eq [
48
+ article_1
49
+ ]
50
+ end
51
+ end
39
52
  end
40
53
 
41
54
  context 'as desc' do
@@ -46,6 +59,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
46
59
  article_3
47
60
  ]
48
61
  end
62
+
63
+ context 'with scope' do
64
+ it 'works' do
65
+ expect(Article.order_by_rating(:average, :desc, scope: category)).to eq [
66
+ article_1
67
+ ]
68
+ end
69
+ end
49
70
  end
50
71
  end
51
72
 
@@ -58,6 +79,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
58
79
  article_1
59
80
  ]
60
81
  end
82
+
83
+ context 'with scope' do
84
+ it 'works' do
85
+ expect(Article.order_by_rating(:estimate, :asc, scope: category)).to eq [
86
+ article_1
87
+ ]
88
+ end
89
+ end
61
90
  end
62
91
 
63
92
  context 'as desc' do
@@ -68,6 +97,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
68
97
  article_3
69
98
  ]
70
99
  end
100
+
101
+ context 'with scope' do
102
+ it 'works' do
103
+ expect(Article.order_by_rating(:estimate, :desc, scope: category)).to eq [
104
+ article_1
105
+ ]
106
+ end
107
+ end
71
108
  end
72
109
  end
73
110
 
@@ -80,6 +117,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
80
117
  article_1
81
118
  ]
82
119
  end
120
+
121
+ context 'with scope' do
122
+ it 'works' do
123
+ expect(Article.order_by_rating(:sum, :asc, scope: category)).to eq [
124
+ article_1
125
+ ]
126
+ end
127
+ end
83
128
  end
84
129
 
85
130
  context 'as desc' do
@@ -90,6 +135,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
90
135
  article_3
91
136
  ]
92
137
  end
138
+
139
+ context 'with scope' do
140
+ it 'works' do
141
+ expect(Article.order_by_rating(:sum, :desc, scope: category)).to eq [
142
+ article_1
143
+ ]
144
+ end
145
+ end
93
146
  end
94
147
 
95
148
  context 'filtering by :total' do
@@ -100,6 +153,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
100
153
  expect(result[0..1]).to match_array [article_2, article_3]
101
154
  expect(result.last).to eq article_1
102
155
  end
156
+
157
+ context 'with scope' do
158
+ it 'works' do
159
+ expect(Article.order_by_rating(:total, :asc, scope: category)).to eq [
160
+ article_1
161
+ ]
162
+ end
163
+ end
103
164
  end
104
165
 
105
166
  context 'as desc' do
@@ -109,6 +170,14 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
109
170
  expect(result.first).to eq article_1
110
171
  expect(result[1..2]).to match_array [article_2, article_3]
111
172
  end
173
+
174
+ context 'with scope' do
175
+ it 'works' do
176
+ expect(Article.order_by_rating(:total, :desc, scope: category)).to eq [
177
+ article_1
178
+ ]
179
+ end
180
+ end
112
181
  end
113
182
  end
114
183
  end
@@ -117,5 +186,11 @@ RSpec.describe Rating::Extension, ':order_by_rating' do
117
186
  it 'works' do
118
187
  expect(User.order_by_rating(:total, :desc)).to match_array [user_1, user_2]
119
188
  end
189
+
190
+ context 'with scope' do
191
+ it 'returns empty since creation has no scope' do
192
+ expect(User.order_by_rating(:total, :desc, scope: category)).to eq []
193
+ end
194
+ end
120
195
  end
121
196
  end
@@ -6,9 +6,21 @@ RSpec.describe Rating::Extension, ':rate_for' do
6
6
  let!(:user) { create :user }
7
7
  let!(:article) { create :article }
8
8
 
9
- it 'delegates to rate object' do
10
- expect(Rating::Rate).to receive(:rate_for).with author: user, resource: article
9
+ context 'with no scopeable' do
10
+ it 'delegates to rate object' do
11
+ expect(Rating::Rate).to receive(:rate_for).with author: user, resource: article, scopeable: nil
11
12
 
12
- user.rate_for article
13
+ user.rate_for article
14
+ end
15
+ end
16
+
17
+ context 'with scopeable' do
18
+ let!(:category) { build :category }
19
+
20
+ it 'delegates to rate object' do
21
+ expect(Rating::Rate).to receive(:rate_for).with author: user, resource: article, scopeable: category
22
+
23
+ user.rate_for article, scope: category
24
+ end
13
25
  end
14
26
  end
@@ -6,9 +6,21 @@ RSpec.describe Rating::Extension, ':rate' do
6
6
  let!(:user) { create :user }
7
7
  let!(:article) { create :article }
8
8
 
9
- it 'delegates to rate object' do
10
- expect(Rating::Rate).to receive(:create).with author: user, resource: article, value: 3
9
+ context 'with no scopeable' do
10
+ it 'delegates to rate object' do
11
+ expect(Rating::Rate).to receive(:create).with author: user, resource: article, scopeable: nil, value: 3
11
12
 
12
- user.rate article, 3
13
+ user.rate article, 3
14
+ end
15
+ end
16
+
17
+ context 'with scopeable' do
18
+ let!(:category) { build :category }
19
+
20
+ it 'delegates to rate object' do
21
+ expect(Rating::Rate).to receive(:create).with author: user, resource: article, scopeable: category, value: 3
22
+
23
+ user.rate article, 3, scope: category
24
+ end
13
25
  end
14
26
  end
@@ -6,15 +6,33 @@ RSpec.describe Rating::Extension, ':rated?' do
6
6
  let!(:user) { create :user }
7
7
  let!(:article) { create :article }
8
8
 
9
- context 'when has no rate for the given resource' do
10
- before { allow(user).to receive(:rate_for).with(article) { nil } }
9
+ context 'with no scopeable' do
10
+ context 'when has no rate for the given resource' do
11
+ before { allow(user).to receive(:rate_for).with(article, scope: nil) { nil } }
11
12
 
12
- specify { expect(user.rated?(article)).to eq false }
13
+ specify { expect(user.rated?(article)).to eq false }
14
+ end
15
+
16
+ context 'when has rate for the given resource' do
17
+ before { allow(user).to receive(:rate_for).with(article, scope: nil) { double } }
18
+
19
+ specify { expect(user.rated?(article)).to eq true }
20
+ end
13
21
  end
14
22
 
15
- context 'when has rate for the given resource' do
16
- before { allow(user).to receive(:rate_for).with(article) { double } }
23
+ context 'with scopeable' do
24
+ let!(:category) { build :category }
25
+
26
+ context 'when has no rate for the given resource' do
27
+ before { allow(user).to receive(:rate_for).with(article, scope: category) { nil } }
28
+
29
+ specify { expect(user.rated?(article, scope: category)).to eq false }
30
+ end
31
+
32
+ context 'when has rate for the given resource' do
33
+ before { allow(user).to receive(:rate_for).with(article, scope: category) { double } }
17
34
 
18
- specify { expect(user.rated?(article)).to eq true }
35
+ specify { expect(user.rated?(article, scope: category)).to eq true }
36
+ end
19
37
  end
20
38
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ RSpec.describe Rating::Extension, '.rated_records' do
6
+ let!(:category) { create :category }
7
+
8
+ let!(:user_1) { create :user }
9
+ let!(:user_2) { create :user }
10
+
11
+ let!(:article_1) { create :article }
12
+ let!(:article_2) { create :article }
13
+ let!(:article_3) { create :article }
14
+
15
+ let!(:rate_1) { create :rating_rate, author: user_1, resource: article_1, value: 100 }
16
+ let!(:rate_2) { create :rating_rate, author: user_1, resource: article_2, value: 11 }
17
+ let!(:rate_3) { create :rating_rate, author: user_1, resource: article_3, value: 10 }
18
+ let!(:rate_4) { create :rating_rate, author: user_2, resource: article_1, value: 1 }
19
+
20
+ let!(:rate_5) { create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1 }
21
+ let!(:rate_6) { create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2 }
22
+
23
+ it 'returns all rates that this author gave' do
24
+ expect(user_1.rated_records).to match_array [rate_1, rate_2, rate_3, rate_5]
25
+ end
26
+ end
@@ -3,36 +3,56 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  RSpec.describe Rating::Extension, ':rated' do
6
- let!(:user) { create :user }
7
- let!(:article) { create :article }
6
+ let!(:category) { create :category }
8
7
 
9
- before { user.rate article, 3 }
8
+ let!(:user_1) { create :user }
9
+ let!(:user_2) { create :user }
10
10
 
11
- it 'returns rates made by the caller' do
12
- expect(user.rated).to eq [Rating::Rate.find_by(resource: article)]
11
+ let!(:article_1) { create :article }
12
+ let!(:article_2) { create :article }
13
+ let!(:article_3) { create :article }
14
+
15
+ let!(:rate_1) { create :rating_rate, author: user_1, resource: article_1, value: 100 }
16
+ let!(:rate_2) { create :rating_rate, author: user_1, resource: article_2, value: 11 }
17
+ let!(:rate_3) { create :rating_rate, author: user_1, resource: article_3, value: 10 }
18
+ let!(:rate_4) { create :rating_rate, author: user_2, resource: article_1, value: 1 }
19
+
20
+ let!(:rate_5) { create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1 }
21
+ let!(:rate_6) { create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2 }
22
+
23
+ context 'with no scope' do
24
+ it 'returns rates made by this author' do
25
+ expect(user_1.rated).to match_array [rate_1, rate_2, rate_3]
26
+ end
27
+ end
28
+
29
+ context 'with no scope' do
30
+ it 'returns scoped rates made by this author' do
31
+ expect(user_1.rated(scope: category)).to eq [rate_5]
32
+ end
13
33
  end
14
34
 
15
35
  context 'when destroy author' do
16
36
  before do
17
- expect(Rating::Rate.where(author: user).count).to eq 1
37
+ expect(Rating::Rate.where(author: user_1).count).to eq 4
18
38
 
19
- user.destroy!
39
+ user_1.destroy!
20
40
  end
21
41
 
22
- it 'destroys rates of this author' do
23
- expect(Rating::Rate.where(author: user).count).to eq 0
42
+ it 'destroys rates of that author' do
43
+ expect(Rating::Rate.where(author: user_1).count).to eq 0
24
44
  end
25
45
  end
26
46
 
27
47
  context 'when destroy resource rated by author' do
28
48
  before do
29
- expect(Rating::Rate.where(resource: article).count).to eq 1
49
+ expect(Rating::Rate.where(resource: article_1).count).to eq 4
30
50
 
31
- article.destroy!
51
+ article_1.destroy!
32
52
  end
33
53
 
34
- it 'destroys rates for that resource' do
35
- expect(Rating::Rate.where(resource: article).count).to eq 0
54
+ it 'destroys rates of that resource' do
55
+ expect(Rating::Rate.where(resource: article_1).count).to eq 0
36
56
  end
37
57
  end
38
58
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ RSpec.describe Rating::Extension, '.rates_records' do
6
+ let!(:category) { create :category }
7
+
8
+ let!(:user_1) { create :user }
9
+ let!(:user_2) { create :user }
10
+
11
+ let!(:article_1) { create :article }
12
+ let!(:article_2) { create :article }
13
+ let!(:article_3) { create :article }
14
+
15
+ let!(:rate_1) { create :rating_rate, author: user_1, resource: article_1, value: 100 }
16
+ let!(:rate_2) { create :rating_rate, author: user_1, resource: article_2, value: 11 }
17
+ let!(:rate_3) { create :rating_rate, author: user_1, resource: article_3, value: 10 }
18
+ let!(:rate_4) { create :rating_rate, author: user_2, resource: article_1, value: 1 }
19
+
20
+ let!(:rate_5) { create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1 }
21
+ let!(:rate_6) { create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2 }
22
+
23
+ it 'returns all rates that this resource received' do
24
+ expect(article_1.rates_records).to match_array [rate_1, rate_4, rate_5, rate_6]
25
+ end
26
+ end
@@ -3,36 +3,52 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  RSpec.describe Rating::Extension, ':rates' do
6
- let!(:user) { create :user }
7
- let!(:article) { create :article }
6
+ let!(:category) { create :category }
8
7
 
9
- before { user.rate article, 3 }
8
+ let!(:user_1) { create :user }
9
+ let!(:user_2) { create :user }
10
10
 
11
- it 'returns rates record' do
12
- expect(article.rates).to eq [Rating::Rate.last]
13
- end
11
+ let!(:article_1) { create :article }
12
+ let!(:article_2) { create :article }
13
+ let!(:article_3) { create :article }
14
14
 
15
- context 'when destroy author' do
16
- before do
17
- expect(Rating::Rate.where(resource: article).count).to eq 1
15
+ let!(:rate_1) { create :rating_rate, author: user_1, resource: article_1, value: 100 }
16
+ let!(:rate_2) { create :rating_rate, author: user_1, resource: article_2, value: 11 }
17
+ let!(:rate_3) { create :rating_rate, author: user_1, resource: article_3, value: 10 }
18
+ let!(:rate_4) { create :rating_rate, author: user_2, resource: article_1, value: 1 }
19
+
20
+ let!(:rate_5) { create :rating_rate, author: user_1, resource: article_1, scopeable: category, value: 1 }
21
+ let!(:rate_6) { create :rating_rate, author: user_2, resource: article_1, scopeable: category, value: 2 }
18
22
 
19
- user.destroy!
23
+ context 'with no scope' do
24
+ it 'returns rates that this resource received' do
25
+ expect(article_1.rates).to match_array [rate_1, rate_4]
20
26
  end
27
+ end
21
28
 
22
- it 'destroys rates of that resource' do
23
- expect(Rating::Rate.where(resource: article).count).to eq 0
29
+ context 'with scope' do
30
+ it 'returns scoped rates that this resource received' do
31
+ expect(article_1.rates(scope: category)).to match_array [rate_5, rate_6]
24
32
  end
25
33
  end
26
34
 
27
- context 'when destroy resource' do
28
- before do
29
- expect(Rating::Rate.where(resource: article).count).to eq 1
35
+ context 'when destroy author' do
36
+ it 'destroys rates of that author' do
37
+ expect(Rating::Rate.where(author: user_1).count).to eq 4
38
+
39
+ user_1.destroy!
30
40
 
31
- article.destroy!
41
+ expect(Rating::Rate.where(author: user_1).count).to eq 0
32
42
  end
43
+ end
33
44
 
45
+ context 'when destroy resource' do
34
46
  it 'destroys rates of that resource' do
35
- expect(Rating::Rate.where(resource: article).count).to eq 0
47
+ expect(Rating::Rate.where(resource: article_1).count).to eq 4
48
+
49
+ article_1.destroy!
50
+
51
+ expect(Rating::Rate.where(resource: article_1).count).to eq 0
36
52
  end
37
53
  end
38
54
  end