rating 0.2.0 → 0.3.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 (37) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -0
  3. data/README.md +64 -24
  4. data/lib/generators/rating/templates/db/migrate/create_rating_tables.rb +8 -8
  5. data/lib/rating/models/rating/extension.rb +2 -2
  6. data/lib/rating/models/rating/rate.rb +5 -3
  7. data/lib/rating/models/rating/rating.rb +13 -8
  8. data/lib/rating/version.rb +1 -1
  9. data/spec/factories/rating/rate.rb +1 -1
  10. data/spec/factories/rating/rating.rb +1 -1
  11. data/spec/models/extension/after_create_spec.rb +5 -5
  12. data/spec/models/extension/order_by_rating_spec.rb +37 -53
  13. data/spec/models/extension/rate_for_spec.rb +5 -5
  14. data/spec/models/extension/rate_spec.rb +31 -5
  15. data/spec/models/extension/rated_question_spec.rb +9 -9
  16. data/spec/models/extension/rated_records_spec.rb +3 -17
  17. data/spec/models/extension/rated_spec.rb +9 -27
  18. data/spec/models/extension/rates_records_spec.rb +2 -16
  19. data/spec/models/extension/rates_spec.rb +5 -19
  20. data/spec/models/extension/rating_records_spec.rb +2 -18
  21. data/spec/models/extension/rating_spec.rb +3 -17
  22. data/spec/models/rate/create_spec.rb +56 -15
  23. data/spec/models/rate/rate_for_spec.rb +7 -7
  24. data/spec/models/rate_spec.rb +2 -2
  25. data/spec/models/rating/averager_data_spec.rb +8 -24
  26. data/spec/models/rating/data_spec.rb +12 -28
  27. data/spec/models/rating/update_rating_spec.rb +2 -18
  28. data/spec/models/rating/values_data_spec.rb +10 -26
  29. data/spec/models/rating_spec.rb +6 -0
  30. data/spec/support/db/migrate/add_comment_on_rating_rates_table.rb +7 -0
  31. data/spec/support/db/migrate/{create_category_spec.rb → create_categories_table.rb} +0 -0
  32. data/spec/support/migrate.rb +3 -1
  33. data/spec/support/shared_context/with_database_records.rb +20 -0
  34. metadata +35 -37
  35. data/spec/factories/user.rb +0 -7
  36. data/spec/support/db/migrate/create_users_table.rb +0 -9
  37. data/spec/support/models/user.rb +0 -5
@@ -3,14 +3,14 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  RSpec.describe Rating::Extension, ':rate_for' do
6
- let!(:user) { create :user }
6
+ let!(:author) { create :author }
7
7
  let!(:article) { create :article }
8
8
 
9
9
  context 'with no scopeable' do
10
10
  it 'delegates to rate object' do
11
- expect(Rating::Rate).to receive(:rate_for).with author: user, resource: article, scopeable: nil
11
+ expect(Rating::Rate).to receive(:rate_for).with author: author, resource: article, scopeable: nil
12
12
 
13
- user.rate_for article
13
+ author.rate_for article
14
14
  end
15
15
  end
16
16
 
@@ -18,9 +18,9 @@ RSpec.describe Rating::Extension, ':rate_for' do
18
18
  let!(:category) { build :category }
19
19
 
20
20
  it 'delegates to rate object' do
21
- expect(Rating::Rate).to receive(:rate_for).with author: user, resource: article, scopeable: category
21
+ expect(Rating::Rate).to receive(:rate_for).with author: author, resource: article, scopeable: category
22
22
 
23
- user.rate_for article, scope: category
23
+ author.rate_for article, scope: category
24
24
  end
25
25
  end
26
26
  end
@@ -3,14 +3,16 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  RSpec.describe Rating::Extension, ':rate' do
6
- let!(:user) { create :user }
6
+ let!(:author) { create :author }
7
7
  let!(:article) { create :article }
8
8
 
9
9
  context 'with no scopeable' do
10
10
  it 'delegates to rate object' do
11
- expect(Rating::Rate).to receive(:create).with author: user, resource: article, scopeable: nil, value: 3
11
+ expect(Rating::Rate).to receive(:create).with(
12
+ author: author, metadata: {}, resource: article, scopeable: nil, value: 3
13
+ )
12
14
 
13
- user.rate article, 3
15
+ author.rate article, 3
14
16
  end
15
17
  end
16
18
 
@@ -18,9 +20,33 @@ RSpec.describe Rating::Extension, ':rate' do
18
20
  let!(:category) { build :category }
19
21
 
20
22
  it 'delegates to rate object' do
21
- expect(Rating::Rate).to receive(:create).with author: user, resource: article, scopeable: category, value: 3
23
+ expect(Rating::Rate).to receive(:create).with(
24
+ author: author, metadata: {}, resource: article, scopeable: category, value: 3
25
+ )
22
26
 
23
- user.rate article, 3, scope: category
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, 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
+ let!(:category) { build :category }
43
+
44
+ it 'delegates to rate object' do
45
+ expect(Rating::Rate).to receive(:create).with(
46
+ author: author, metadata: { comment: 'comment' }, resource: article, scopeable: nil, value: 3
47
+ )
48
+
49
+ author.rate article, 3, metadata: { comment: 'comment' }
24
50
  end
25
51
  end
26
52
  end
@@ -3,20 +3,20 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  RSpec.describe Rating::Extension, ':rated?' do
6
- let!(:user) { create :user }
6
+ let!(:author) { create :author }
7
7
  let!(:article) { create :article }
8
8
 
9
9
  context 'with no scopeable' do
10
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
+ before { allow(author).to receive(:rate_for).with(article, scope: nil).and_return nil }
12
12
 
13
- specify { expect(user.rated?(article)).to eq false }
13
+ specify { expect(author.rated?(article)).to eq false }
14
14
  end
15
15
 
16
16
  context 'when has rate for the given resource' do
17
- before { allow(user).to receive(:rate_for).with(article, scope: nil) { double } }
17
+ before { allow(author).to receive(:rate_for).with(article, scope: nil).and_return double }
18
18
 
19
- specify { expect(user.rated?(article)).to eq true }
19
+ specify { expect(author.rated?(article)).to eq true }
20
20
  end
21
21
  end
22
22
 
@@ -24,15 +24,15 @@ RSpec.describe Rating::Extension, ':rated?' do
24
24
  let!(:category) { build :category }
25
25
 
26
26
  context 'when has no rate for the given resource' do
27
- before { allow(user).to receive(:rate_for).with(article, scope: category) { nil } }
27
+ before { allow(author).to receive(:rate_for).with(article, scope: category).and_return nil }
28
28
 
29
- specify { expect(user.rated?(article, scope: category)).to eq false }
29
+ specify { expect(author.rated?(article, scope: category)).to eq false }
30
30
  end
31
31
 
32
32
  context 'when has rate for the given resource' do
33
- before { allow(user).to receive(:rate_for).with(article, scope: category) { double } }
33
+ before { allow(author).to receive(:rate_for).with(article, scope: category).and_return double }
34
34
 
35
- specify { expect(user.rated?(article, scope: category)).to eq true }
35
+ specify { expect(author.rated?(article, scope: category)).to eq true }
36
36
  end
37
37
  end
38
38
  end
@@ -1,26 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rails_helper'
4
+ require 'support/shared_context/with_database_records'
4
5
 
5
6
  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 }
7
+ include_context 'with_database_records'
22
8
 
23
9
  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]
10
+ expect(author_1.rated_records).to match_array [rate_1, rate_2, rate_3, rate_5]
25
11
  end
26
12
  end
@@ -1,57 +1,39 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rails_helper'
4
+ require 'support/shared_context/with_database_records'
4
5
 
5
6
  RSpec.describe Rating::Extension, ':rated' 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 }
7
+ include_context 'with_database_records'
22
8
 
23
9
  context 'with no scope' do
24
10
  it 'returns rates made by this author' do
25
- expect(user_1.rated).to match_array [rate_1, rate_2, rate_3]
11
+ expect(author_1.rated).to match_array [rate_1, rate_2, rate_3]
26
12
  end
27
13
  end
28
14
 
29
15
  context 'with no scope' do
30
16
  it 'returns scoped rates made by this author' do
31
- expect(user_1.rated(scope: category)).to eq [rate_5]
17
+ expect(author_1.rated(scope: category)).to eq [rate_5]
32
18
  end
33
19
  end
34
20
 
35
21
  context 'when destroy author' do
36
- before do
37
- expect(Rating::Rate.where(author: user_1).count).to eq 4
22
+ it 'destroys rates of that author' do
23
+ expect(Rating::Rate.where(author: author_1).count).to eq 4
38
24
 
39
- user_1.destroy!
40
- end
25
+ author_1.destroy!
41
26
 
42
- it 'destroys rates of that author' do
43
- expect(Rating::Rate.where(author: user_1).count).to eq 0
27
+ expect(Rating::Rate.where(author: author_1).count).to eq 0
44
28
  end
45
29
  end
46
30
 
47
31
  context 'when destroy resource rated by author' do
48
- before do
32
+ it 'destroys rates of that resource' do
49
33
  expect(Rating::Rate.where(resource: article_1).count).to eq 4
50
34
 
51
35
  article_1.destroy!
52
- end
53
36
 
54
- it 'destroys rates of that resource' do
55
37
  expect(Rating::Rate.where(resource: article_1).count).to eq 0
56
38
  end
57
39
  end
@@ -1,24 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rails_helper'
4
+ require 'support/shared_context/with_database_records'
4
5
 
5
6
  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 }
7
+ include_context 'with_database_records'
22
8
 
23
9
  it 'returns all rates that this resource received' do
24
10
  expect(article_1.rates_records).to match_array [rate_1, rate_4, rate_5, rate_6]
@@ -1,24 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rails_helper'
4
+ require 'support/shared_context/with_database_records'
4
5
 
5
6
  RSpec.describe Rating::Extension, ':rates' 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 }
7
+ include_context 'with_database_records'
22
8
 
23
9
  context 'with no scope' do
24
10
  it 'returns rates that this resource received' do
@@ -34,11 +20,11 @@ RSpec.describe Rating::Extension, ':rates' do
34
20
 
35
21
  context 'when destroy author' do
36
22
  it 'destroys rates of that author' do
37
- expect(Rating::Rate.where(author: user_1).count).to eq 4
23
+ expect(Rating::Rate.where(author: author_1).count).to eq 4
38
24
 
39
- user_1.destroy!
25
+ author_1.destroy!
40
26
 
41
- expect(Rating::Rate.where(author: user_1).count).to eq 0
27
+ expect(Rating::Rate.where(author: author_1).count).to eq 0
42
28
  end
43
29
  end
44
30
 
@@ -1,26 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rails_helper'
4
+ require 'support/shared_context/with_database_records'
4
5
 
5
6
  RSpec.describe Rating::Extension, '.rating' 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
- before do
16
- create :rating_rate, author: user_1, resource: article_1, value: 100
17
- create :rating_rate, author: user_1, resource: article_2, value: 11
18
- create :rating_rate, author: user_1, resource: article_3, value: 10
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
23
- end
7
+ include_context 'with_database_records'
24
8
 
25
9
  it 'returns all rating of this resource' do
26
10
  expect(article_1.rating_records).to match_array Rating::Rating.where(resource: article_1)
@@ -1,24 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rails_helper'
4
+ require 'support/shared_context/with_database_records'
4
5
 
5
6
  RSpec.describe Rating::Extension, '.rating' 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 }
7
+ include_context 'with_database_records'
22
8
 
23
9
  context 'with no scope' do
24
10
  it 'returns rating record' do
@@ -36,7 +22,7 @@ RSpec.describe Rating::Extension, '.rating' do
36
22
  it 'does not destroy resource rating' do
37
23
  expect(Rating::Rating.where(resource: article_1).count).to eq 2
38
24
 
39
- user_1.destroy!
25
+ author_1.destroy!
40
26
 
41
27
  expect(Rating::Rating.where(resource: article_1).count).to eq 2
42
28
  end
@@ -3,17 +3,17 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  RSpec.describe Rating::Rate, ':create' do
6
- let!(:user) { create :user }
6
+ let!(:author) { create :author }
7
7
  let!(:article) { create :article }
8
8
 
9
9
  context 'with no scopeable' do
10
- before { create :rating_rate, author: user, resource: article, value: 3 }
10
+ before { described_class.create author: author, metadata: {}, resource: article, value: 3 }
11
11
 
12
12
  context 'when rate does not exist yet' do
13
13
  it 'creates a rate entry' do
14
14
  rate = described_class.last
15
15
 
16
- expect(rate.author).to eq user
16
+ expect(rate.author).to eq author
17
17
  expect(rate.resource).to eq article
18
18
  expect(rate.value).to eq 3
19
19
  end
@@ -30,24 +30,24 @@ RSpec.describe Rating::Rate, ':create' do
30
30
  end
31
31
 
32
32
  context 'when rate already exists' do
33
- let!(:user_2) { create :user }
33
+ let!(:author_2) { create :author }
34
34
 
35
- before { create :rating_rate, author: user_2, resource: article, value: 4 }
35
+ before { described_class.create author: author_2, metadata: {}, resource: article, value: 4 }
36
36
 
37
37
  it 'creates one more rate entry' do
38
- rates = described_class.where(author: [user, user_2]).order('created_at asc')
38
+ rates = described_class.where(author: [author, author_2]).order('created_at asc')
39
39
 
40
40
  expect(rates.size).to eq 2
41
41
 
42
42
  rate = rates[0]
43
43
 
44
- expect(rate.author).to eq user
44
+ expect(rate.author).to eq author
45
45
  expect(rate.resource).to eq article
46
46
  expect(rate.value).to eq 3
47
47
 
48
48
  rate = rates[1]
49
49
 
50
- expect(rate.author).to eq user_2
50
+ expect(rate.author).to eq author_2
51
51
  expect(rate.resource).to eq article
52
52
  expect(rate.value).to eq 4
53
53
  end
@@ -67,13 +67,13 @@ RSpec.describe Rating::Rate, ':create' do
67
67
  context 'with scopeable' do
68
68
  let!(:category) { create :category }
69
69
 
70
- before { create :rating_rate, author: user, resource: article, scopeable: category, value: 3 }
70
+ before { described_class.create author: author, metadata: {}, resource: article, scopeable: category, value: 3 }
71
71
 
72
72
  context 'when rate does not exist yet' do
73
73
  it 'creates a rate entry' do
74
74
  rate = described_class.last
75
75
 
76
- expect(rate.author).to eq user
76
+ expect(rate.author).to eq author
77
77
  expect(rate.resource).to eq article
78
78
  expect(rate.scopeable).to eq category
79
79
  expect(rate.value).to eq 3
@@ -92,25 +92,25 @@ RSpec.describe Rating::Rate, ':create' do
92
92
  end
93
93
 
94
94
  context 'when rate already exists' do
95
- let!(:user_2) { create :user }
95
+ let!(:author_2) { create :author }
96
96
 
97
- before { create :rating_rate, author: user_2, resource: article, scopeable: category, value: 4 }
97
+ before { described_class.create author: author_2, metadata: {}, resource: article, scopeable: category, value: 4 }
98
98
 
99
99
  it 'creates one more rate entry' do
100
- rates = described_class.where(author: [user, user_2]).order('created_at asc')
100
+ rates = described_class.where(author: [author, author_2]).order('created_at asc')
101
101
 
102
102
  expect(rates.size).to eq 2
103
103
 
104
104
  rate = rates[0]
105
105
 
106
- expect(rate.author).to eq user
106
+ expect(rate.author).to eq author
107
107
  expect(rate.resource).to eq article
108
108
  expect(rate.scopeable).to eq category
109
109
  expect(rate.value).to eq 3
110
110
 
111
111
  rate = rates[1]
112
112
 
113
- expect(rate.author).to eq user_2
113
+ expect(rate.author).to eq author_2
114
114
  expect(rate.resource).to eq article
115
115
  expect(rate.scopeable).to eq category
116
116
  expect(rate.value).to eq 4
@@ -128,4 +128,45 @@ RSpec.describe Rating::Rate, ':create' do
128
128
  end
129
129
  end
130
130
  end
131
+
132
+ context 'with metadata' do
133
+ context 'with nil value' do
134
+ it 'creates a rate entry ignoring metadata' do
135
+ described_class.create author: author, metadata: nil, resource: article, value: 3
136
+
137
+ rate = described_class.last
138
+
139
+ expect(rate.author).to eq author
140
+ expect(rate.comment).to eq nil
141
+ expect(rate.resource).to eq article
142
+ expect(rate.value).to eq 3
143
+ end
144
+ end
145
+
146
+ context 'with empty value' do
147
+ it 'creates a rate entry ignoring metadata' do
148
+ described_class.create author: author, metadata: '', resource: article, value: 3
149
+
150
+ rate = described_class.last
151
+
152
+ expect(rate.author).to eq author
153
+ expect(rate.comment).to eq nil
154
+ expect(rate.resource).to eq article
155
+ expect(rate.value).to eq 3
156
+ end
157
+ end
158
+
159
+ context 'with hash value' do
160
+ it 'creates a rate entry with metadata included' do
161
+ described_class.create author: author, metadata: { comment: 'comment' }, resource: article, value: 3
162
+
163
+ rate = described_class.last
164
+
165
+ expect(rate.author).to eq author
166
+ expect(rate.comment).to eq 'comment'
167
+ expect(rate.resource).to eq article
168
+ expect(rate.value).to eq 3
169
+ end
170
+ end
171
+ end
131
172
  end