rating 0.11.0 → 0.12.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 (68) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +23 -9
  3. data/README.md +16 -5
  4. data/lib/rating/models/rating/extension.rb +1 -2
  5. data/lib/rating/models/rating/rate.rb +2 -2
  6. data/lib/rating/models/rating/rating.rb +2 -2
  7. data/lib/rating/version.rb +1 -1
  8. metadata +56 -130
  9. data/spec/config/rate_table_spec.rb +0 -17
  10. data/spec/config/rating_table_spec.rb +0 -17
  11. data/spec/config/validations_spec.rb +0 -33
  12. data/spec/factories/article.rb +0 -7
  13. data/spec/factories/author.rb +0 -7
  14. data/spec/factories/category.rb +0 -7
  15. data/spec/factories/comment.rb +0 -5
  16. data/spec/factories/global.rb +0 -6
  17. data/spec/factories/rating/rate.rb +0 -10
  18. data/spec/factories/rating/rating.rb +0 -12
  19. data/spec/factories/toy.rb +0 -6
  20. data/spec/models/extension/after_create_spec.rb +0 -47
  21. data/spec/models/extension/order_by_rating_spec.rb +0 -180
  22. data/spec/models/extension/rate_for_spec.rb +0 -41
  23. data/spec/models/extension/rate_spec.rb +0 -60
  24. data/spec/models/extension/rated_question_spec.rb +0 -48
  25. data/spec/models/extension/rated_records_spec.rb +0 -12
  26. data/spec/models/extension/rated_spec.rb +0 -50
  27. data/spec/models/extension/rates_records_spec.rb +0 -12
  28. data/spec/models/extension/rates_spec.rb +0 -50
  29. data/spec/models/extension/rating_records_spec.rb +0 -12
  30. data/spec/models/extension/rating_spec.rb +0 -40
  31. data/spec/models/extension/rating_warm_up_spec.rb +0 -115
  32. data/spec/models/extension/unscoped_rating_spec.rb +0 -99
  33. data/spec/models/extension/where_spec.rb +0 -33
  34. data/spec/models/rate/create_spec.rb +0 -422
  35. data/spec/models/rate/rate_for_spec.rb +0 -107
  36. data/spec/models/rate_spec.rb +0 -28
  37. data/spec/models/rating/averager_data_spec.rb +0 -32
  38. data/spec/models/rating/data_spec.rb +0 -48
  39. data/spec/models/rating/update_rating_spec.rb +0 -48
  40. data/spec/models/rating/values_data_spec.rb +0 -40
  41. data/spec/models/rating_spec.rb +0 -24
  42. data/spec/rails_helper.rb +0 -17
  43. data/spec/support/common.rb +0 -22
  44. data/spec/support/database_cleaner.rb +0 -19
  45. data/spec/support/db/migrate/add_comment_on_rating_rates_table.rb +0 -8
  46. data/spec/support/db/migrate/add_extra_fields_on_rating_rates_table.rb +0 -8
  47. data/spec/support/db/migrate/create_articles_table.rb +0 -9
  48. data/spec/support/db/migrate/create_authors_table.rb +0 -9
  49. data/spec/support/db/migrate/create_categories_table.rb +0 -12
  50. data/spec/support/db/migrate/create_comments_table.rb +0 -7
  51. data/spec/support/db/migrate/create_globals_table.rb +0 -8
  52. data/spec/support/db/migrate/create_rates_table.rb +0 -19
  53. data/spec/support/db/migrate/create_rating_table.rb +0 -20
  54. data/spec/support/db/migrate/create_review_ratings_table.rb +0 -17
  55. data/spec/support/db/migrate/create_reviews_table.rb +0 -15
  56. data/spec/support/db/migrate/create_toys_table.rb +0 -8
  57. data/spec/support/factory_bot.rb +0 -9
  58. data/spec/support/migrate.rb +0 -21
  59. data/spec/support/models/article.rb +0 -7
  60. data/spec/support/models/author.rb +0 -5
  61. data/spec/support/models/category.rb +0 -6
  62. data/spec/support/models/comment.rb +0 -5
  63. data/spec/support/models/global.rb +0 -7
  64. data/spec/support/models/review.rb +0 -5
  65. data/spec/support/models/review_rating.rb +0 -4
  66. data/spec/support/models/toy.rb +0 -5
  67. data/spec/support/shared_context/with_database_records.rb +0 -20
  68. data/spec/support/shoulda.rb +0 -10
@@ -1,180 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
- require 'support/shared_context/with_database_records'
5
-
6
- RSpec.describe Rating::Extension, ':order_by_rating' do
7
- include_context 'with_database_records'
8
-
9
- context 'with default filters' do
10
- it 'sorts by :estimate :desc' do
11
- expect(Article.order_by_rating).to eq [
12
- article_1,
13
- article_2,
14
- article_3,
15
- ]
16
- end
17
- end
18
-
19
- context 'when filtering by :average' do
20
- context 'with as asc' do
21
- it 'works' do
22
- expect(Article.order_by_rating(:average, :asc)).to eq [
23
- article_3,
24
- article_2,
25
- article_1,
26
- ]
27
- end
28
-
29
- context 'with scope' do
30
- it 'works' do
31
- expect(Article.order_by_rating(:average, :asc, scope: category)).to eq [
32
- article_1,
33
- ]
34
- end
35
- end
36
- end
37
-
38
- context 'with as desc' do
39
- it 'works' do
40
- expect(Article.order_by_rating(:average, :desc)).to eq [
41
- article_1,
42
- article_2,
43
- article_3,
44
- ]
45
- end
46
-
47
- context 'with scope' do
48
- it 'works' do
49
- expect(Article.order_by_rating(:average, :desc, scope: category)).to eq [
50
- article_1,
51
- ]
52
- end
53
- end
54
- end
55
- end
56
-
57
- context 'when filtering by :estimate' do
58
- context 'with as asc' do
59
- it 'works' do
60
- expect(Article.order_by_rating(:estimate, :asc)).to eq [
61
- article_3,
62
- article_2,
63
- article_1,
64
- ]
65
- end
66
-
67
- context 'with scope' do
68
- it 'works' do
69
- expect(Article.order_by_rating(:estimate, :asc, scope: category)).to eq [
70
- article_1,
71
- ]
72
- end
73
- end
74
- end
75
-
76
- context 'with as desc' do
77
- it 'works' do
78
- expect(Article.order_by_rating(:estimate, :desc)).to eq [
79
- article_1,
80
- article_2,
81
- article_3,
82
- ]
83
- end
84
-
85
- context 'with scope' do
86
- it 'works' do
87
- expect(Article.order_by_rating(:estimate, :desc, scope: category)).to eq [
88
- article_1,
89
- ]
90
- end
91
- end
92
- end
93
- end
94
-
95
- context 'when filtering by :sum' do
96
- context 'as asc' do
97
- it 'works' do
98
- expect(Article.order_by_rating(:sum, :asc)).to eq [
99
- article_3,
100
- article_2,
101
- article_1,
102
- ]
103
- end
104
-
105
- context 'with scope' do
106
- it 'works' do
107
- expect(Article.order_by_rating(:sum, :asc, scope: category)).to eq [
108
- article_1,
109
- ]
110
- end
111
- end
112
- end
113
-
114
- context 'with as desc' do
115
- it 'works' do
116
- expect(Article.order_by_rating(:sum, :desc)).to eq [
117
- article_1,
118
- article_2,
119
- article_3,
120
- ]
121
- end
122
-
123
- context 'with scope' do
124
- it 'works' do
125
- expect(Article.order_by_rating(:sum, :desc, scope: category)).to eq [
126
- article_1,
127
- ]
128
- end
129
- end
130
- end
131
- end
132
-
133
- context 'when filtering by :total' do
134
- context 'with as asc' do
135
- it 'works' do
136
- result = Article.order_by_rating(:total, :asc)
137
-
138
- expect(result[0..1]).to match_array [article_2, article_3]
139
- expect(result.last).to eq article_1
140
- end
141
-
142
- context 'with scope' do
143
- it 'works' do
144
- expect(Article.order_by_rating(:total, :asc, scope: category)).to eq [
145
- article_1,
146
- ]
147
- end
148
- end
149
- end
150
-
151
- context 'with as desc' do
152
- it 'works' do
153
- result = Article.order_by_rating(:total, :desc)
154
-
155
- expect(result.first).to eq article_1
156
- expect(result[1..2]).to match_array [article_2, article_3]
157
- end
158
-
159
- context 'with scope' do
160
- it 'works' do
161
- expect(Article.order_by_rating(:total, :desc, scope: category)).to eq [
162
- article_1,
163
- ]
164
- end
165
- end
166
- end
167
- end
168
-
169
- context 'with other resource' do
170
- it 'works' do
171
- expect(Author.order_by_rating(:total, :desc)).to match_array [author_1, author_2]
172
- end
173
-
174
- context 'with scope' do
175
- it 'returns empty since creation has no scope' do
176
- expect(Author.order_by_rating(:total, :desc, scope: category)).to eq []
177
- end
178
- end
179
- end
180
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Rating::Extension, ':rate_for' do
6
- let!(:author) { create :author }
7
- let!(:article) { create :article }
8
-
9
- context 'with no scopeable' do
10
- it 'delegates to rate object' do
11
- expect(Rating::Rate).to receive(:rate_for).with author: author, extra_scopes: {}, resource: article, scopeable: nil
12
-
13
- author.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: author, extra_scopes: {}, resource: article, scopeable: category
22
-
23
- author.rate_for article, scope: category
24
- end
25
- end
26
-
27
- context 'with extra_scopes' do
28
- let!(:category) { build :category }
29
-
30
- it 'delegates to rate object' do
31
- expect(Rating::Rate).to receive(:rate_for).with(
32
- author: author,
33
- extra_scopes: { scope_1: 'scope_1' },
34
- resource: article,
35
- scopeable: category
36
- )
37
-
38
- author.rate_for article, extra_scopes: { scope_1: 'scope_1' }, scope: category
39
- end
40
- end
41
- end
@@ -1,60 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Rating::Extension, ':rate' do
6
- let!(:author) { create :author }
7
- let!(:article) { create :article }
8
-
9
- context 'with no scopeable' do
10
- it 'delegates to rate object' do
11
- expect(Rating::Rate).to receive(:create).with(
12
- author: author, extra_scopes: {}, metadata: {}, resource: article, scopeable: nil, value: 3
13
- )
14
-
15
- author.rate article, 3
16
- end
17
- end
18
-
19
- context 'with scopeable' do
20
- let!(:category) { build :category }
21
-
22
- it 'delegates to rate object' do
23
- expect(Rating::Rate).to receive(:create).with(
24
- author: author, extra_scopes: {}, metadata: {}, resource: article, scopeable: category, value: 3
25
- )
26
-
27
- author.rate article, 3, scope: category
28
- end
29
- end
30
-
31
- context 'with no metadata' do
32
- it 'delegates an empty hash to rate object' do
33
- expect(Rating::Rate).to receive(:create).with(
34
- author: author, extra_scopes: {}, resource: article, metadata: {}, scopeable: nil, value: 3
35
- )
36
-
37
- author.rate article, 3
38
- end
39
- end
40
-
41
- context 'with metadata' do
42
- it 'delegates to rate object' do
43
- expect(Rating::Rate).to receive(:create).with(
44
- author: author, extra_scopes: {}, metadata: { comment: 'comment' }, resource: article, scopeable: nil, value: 3
45
- )
46
-
47
- author.rate article, 3, metadata: { comment: 'comment' }
48
- end
49
- end
50
-
51
- context 'with extra_scopes' do
52
- it 'delegates to rate object' do
53
- expect(Rating::Rate).to receive(:create).with(
54
- author: author, extra_scopes: { scope_1: 'scope_1' }, metadata: { comment: 'comment' }, resource: article, scopeable: nil, value: 3
55
- )
56
-
57
- author.rate article, 3, extra_scopes: { scope_1: 'scope_1' }, metadata: { comment: 'comment' }
58
- end
59
- end
60
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Rating::Extension, ':rated?' do
6
- let!(:author) { create :author }
7
- let!(:resource) { create :article }
8
-
9
- context 'with no scopeable' do
10
- before { author.rate resource, 1 }
11
-
12
- context 'when has no rate for the given resource' do
13
- specify { expect(author.rated?(create(:article))).to eq false }
14
- end
15
-
16
- context 'when has rate for the given resource' do
17
- specify { expect(author.rated?(resource)).to eq true }
18
- end
19
- end
20
-
21
- context 'with scopeable' do
22
- let!(:category) { create :category }
23
-
24
- before { author.rate resource, 1, scope: category }
25
-
26
- context 'when has no rate for the given resource' do
27
- specify { expect(author.rated?(resource, scope: create(:category))).to eq false }
28
- end
29
-
30
- context 'when has rate for the given resource' do
31
- specify { expect(author.rated?(resource, scope: category)).to eq true }
32
- end
33
- end
34
-
35
- if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
36
- context 'with extra scopes' do
37
- before { author.rate resource, 1, extra_scopes: { scope_1: 'scope_1' } }
38
-
39
- context 'when has no rate for the given resource with given extra scopes' do
40
- specify { expect(author.rated?(resource, extra_scopes: { scope_1: 'missing' })).to eq false }
41
- end
42
-
43
- context 'when has rate for the given resource with given extra scopes' do
44
- specify { expect(author.rated?(resource, extra_scopes: { scope_1: 'scope_1' })).to eq true }
45
- end
46
- end
47
- end
48
- end
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
- require 'support/shared_context/with_database_records'
5
-
6
- RSpec.describe Rating::Extension, '.rated_records' do
7
- include_context 'with_database_records'
8
-
9
- it 'returns all rates that this author gave' do
10
- expect(author_1.rated_records).to match_array [rate_1, rate_2, rate_3, rate_5]
11
- end
12
- end
@@ -1,50 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
- require 'support/shared_context/with_database_records'
5
-
6
- RSpec.describe Rating::Extension, ':rated' do
7
- include_context 'with_database_records'
8
-
9
- context 'with no scope' do
10
- it 'returns rates made by this author' do
11
- expect(author_1.rated).to match_array [rate_1, rate_2, rate_3]
12
- end
13
- end
14
-
15
- context 'with no scope' do
16
- it 'returns scoped rates made by this author' do
17
- expect(author_1.rated(scope: category)).to eq [rate_5]
18
- end
19
- end
20
-
21
- context 'when destroy author' do
22
- it 'destroys rates of that author' do
23
- expect(Rating::Rate.where(author: author_1).count).to eq 4
24
-
25
- author_1.destroy!
26
-
27
- expect(Rating::Rate.where(author: author_1).count).to eq 0
28
- end
29
- end
30
-
31
- context 'when destroy resource rated by author' do
32
- it 'destroys rates of that resource' do
33
- expect(Rating::Rate.where(resource: article_1).count).to eq 4
34
-
35
- article_1.destroy!
36
-
37
- expect(Rating::Rate.where(resource: article_1).count).to eq 0
38
- end
39
- end
40
-
41
- if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
42
- context 'with extra scopes' do
43
- let!(:extra_scopes_rate) { author_1.rate article_1, 1, extra_scopes: { scope_1: 'scope_1' } }
44
-
45
- it 'returns records considering the extra scopes' do
46
- expect(author_1.rated(extra_scopes: { scope_1: 'scope_1' })).to eq [extra_scopes_rate]
47
- end
48
- end
49
- end
50
- end
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
- require 'support/shared_context/with_database_records'
5
-
6
- RSpec.describe Rating::Extension, '.rates_records' do
7
- include_context 'with_database_records'
8
-
9
- it 'returns all rates that this resource received' do
10
- expect(article_1.rates_records).to match_array [rate_1, rate_4, rate_5, rate_6]
11
- end
12
- end
@@ -1,50 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
- require 'support/shared_context/with_database_records'
5
-
6
- RSpec.describe Rating::Extension, ':rates' do
7
- include_context 'with_database_records'
8
-
9
- context 'with no scope' do
10
- it 'returns rates that this resource received' do
11
- expect(article_1.rates).to match_array [rate_1, rate_4]
12
- end
13
- end
14
-
15
- context 'with scope' do
16
- it 'returns scoped rates that this resource received' do
17
- expect(article_1.rates(scope: category)).to match_array [rate_5, rate_6]
18
- end
19
- end
20
-
21
- context 'when destroy author' do
22
- it 'destroys rates of that author' do
23
- expect(Rating::Rate.where(author: author_1).count).to eq 4
24
-
25
- author_1.destroy!
26
-
27
- expect(Rating::Rate.where(author: author_1).count).to eq 0
28
- end
29
- end
30
-
31
- context 'when destroy resource' do
32
- it 'destroys rates of that resource' do
33
- expect(Rating::Rate.where(resource: article_1).count).to eq 4
34
-
35
- article_1.destroy!
36
-
37
- expect(Rating::Rate.where(resource: article_1).count).to eq 0
38
- end
39
- end
40
-
41
- if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
42
- context 'with extra scopes' do
43
- let!(:extra_scopes_rate) { author_1.rate article_1, 1, extra_scopes: { scope_1: 'scope_1' } }
44
-
45
- it 'returns records considering the extra scopes' do
46
- expect(article_1.rates(extra_scopes: { scope_1: 'scope_1' })).to eq [extra_scopes_rate]
47
- end
48
- end
49
- end
50
- end
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
- require 'support/shared_context/with_database_records'
5
-
6
- RSpec.describe Rating::Extension, '.rating' do
7
- include_context 'with_database_records'
8
-
9
- it 'returns all rating of this resource' do
10
- expect(article_1.rating_records).to match_array Rating::Rating.where(resource: article_1)
11
- end
12
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
- require 'support/shared_context/with_database_records'
5
-
6
- RSpec.describe Rating::Extension, '.rating' do
7
- include_context 'with_database_records'
8
-
9
- context 'with no scope' do
10
- it 'returns rating record' do
11
- expect(article_1.rating).to eq Rating::Rating.find_by(resource: article_1, scopeable: nil)
12
- end
13
- end
14
-
15
- context 'with scope' do
16
- it 'returns scoped rating record' do
17
- expect(article_1.rating(scope: category)).to eq Rating::Rating.find_by(resource: article_1, scopeable: category)
18
- end
19
- end
20
-
21
- context 'when destroy author' do
22
- it 'does not destroy resource rating' do
23
- expect(Rating::Rating.where(resource: article_1).count).to eq 2
24
-
25
- author_1.destroy!
26
-
27
- expect(Rating::Rating.where(resource: article_1).count).to eq 2
28
- end
29
- end
30
-
31
- context 'when destroy resource' do
32
- it 'destroys resource rating too' do
33
- expect(Rating::Rating.where(resource: article_1).count).to eq 2
34
-
35
- article_1.destroy!
36
-
37
- expect(Rating::Rating.where(resource: article_1).count).to eq 0
38
- end
39
- end
40
- end
@@ -1,115 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Rating::Extension, '.rating_warm_up' do
6
- context 'when scoping is nil' do
7
- context 'when update is made' do
8
- let!(:record) { create :comment }
9
- let!(:rating) { ::Rating::Rating.find_by resource: record }
10
-
11
- it 'creates the cache' do
12
- record.rating_warm_up scoping: nil
13
-
14
- expect(::Rating::Rating.all).to eq [rating]
15
- end
16
-
17
- it 'returns the cached object' do
18
- expect(record.rating_warm_up).to eq rating
19
- end
20
- end
21
-
22
- context 'when record does not exist' do
23
- let!(:record) { create :comment }
24
-
25
- before { ::Rating::Rating.destroy_all }
26
-
27
- it 'creates the cache' do
28
- record.rating_warm_up scoping: nil
29
-
30
- expect(::Rating::Rating.all.map(&:resource)).to eq [record]
31
- end
32
-
33
- it 'returns the cached object' do
34
- expect(record.rating_warm_up).to eq ::Rating::Rating.last
35
- end
36
- end
37
- end
38
-
39
- context 'when scoping is not nil' do
40
- context 'when update is made' do
41
- let!(:category_1) { create :category }
42
- let!(:category_2) { create :category }
43
- let!(:record) { create :article, categories: [category_1, category_2] }
44
-
45
- it 'creates the cache' do
46
- record.rating_warm_up scoping: :categories
47
-
48
- ratings = ::Rating::Rating.all
49
-
50
- expect(ratings.map(&:scopeable)).to match_array [category_1, category_2]
51
- expect(ratings.map(&:resource)).to match_array [record, record]
52
- end
53
-
54
- it 'returns the cached objects' do
55
- expect(record.rating_warm_up(scoping: :categories)).to eq ::Rating::Rating.all
56
- end
57
- end
58
-
59
- context 'when record does not exist' do
60
- let!(:category_1) { create :category }
61
- let!(:category_2) { create :category }
62
- let!(:record) { create :article, categories: [category_1, category_2] }
63
-
64
- before { ::Rating::Rating.destroy_all }
65
-
66
- it 'creates the cache' do
67
- record.rating_warm_up scoping: :categories
68
-
69
- ratings = ::Rating::Rating.all
70
-
71
- expect(ratings.map(&:scopeable)).to match_array [category_1, category_2]
72
- expect(ratings.map(&:resource)).to match_array [record, record]
73
- end
74
-
75
- it 'returns the cached objects' do
76
- expect(record.rating_warm_up(scoping: :categories)).to eq ::Rating::Rating.all
77
- end
78
- end
79
-
80
- context 'when a non existent scoping is given' do
81
- let!(:record) { create :article }
82
-
83
- it 'returns an empty array' do
84
- expect(record.rating_warm_up(scoping: :missing)).to eq []
85
- end
86
- end
87
-
88
- context 'when scoping is given inside array' do
89
- let!(:category) { create :category }
90
- let!(:record) { create :article, categories: [category] }
91
-
92
- it 'returns the cache' do
93
- expect(record.rating_warm_up(scoping: [:categories])).to eq ::Rating::Rating.all
94
- end
95
- end
96
-
97
- context 'when scoping is given inside multiple arrays' do
98
- let!(:category) { create :category }
99
- let!(:record) { create :article, categories: [category] }
100
-
101
- it 'returns the cache' do
102
- expect(record.rating_warm_up(scoping: [[:categories]])).to eq ::Rating::Rating.all
103
- end
104
- end
105
-
106
- context 'when scoping is given with nil value together' do
107
- let!(:category) { create :category }
108
- let!(:record) { create :article, categories: [category] }
109
-
110
- it 'returns the cache' do
111
- expect(record.rating_warm_up(scoping: [[:categories, nil], nil])).to eq ::Rating::Rating.all
112
- end
113
- end
114
- end
115
- end