fix: avoid error when has no rate record
parent
1525a7b95f
commit
e29e0c049e
|
@ -101,7 +101,7 @@ module Rating
|
||||||
count = distinct ? 'COUNT(DISTINCT resource_id)' : 'COUNT(1)'
|
count = distinct ? 'COUNT(DISTINCT resource_id)' : 'COUNT(1)'
|
||||||
|
|
||||||
%((
|
%((
|
||||||
SELECT #{count}
|
SELECT GREATEST(#{count}, 1)
|
||||||
FROM #{rate_table_name}
|
FROM #{rate_table_name}
|
||||||
WHERE resource_type = :resource_type AND #{scope_type_query(scopeable)}
|
WHERE resource_type = :resource_type AND #{scope_type_query(scopeable)}
|
||||||
))
|
))
|
||||||
|
|
|
@ -4,9 +4,9 @@ require 'rails_helper'
|
||||||
require 'support/shared_context/with_database_records'
|
require 'support/shared_context/with_database_records'
|
||||||
|
|
||||||
RSpec.describe Rating::Rating, ':update_rating' do
|
RSpec.describe Rating::Rating, ':update_rating' do
|
||||||
include_context 'with_database_records'
|
|
||||||
|
|
||||||
context 'with no scopeable' do
|
context 'with no scopeable' do
|
||||||
|
include_context 'with_database_records'
|
||||||
|
|
||||||
it 'updates the rating data of the given resource' do
|
it 'updates the rating data of the given resource' do
|
||||||
record = described_class.find_by(resource: article_1)
|
record = described_class.find_by(resource: article_1)
|
||||||
|
|
||||||
|
@ -18,6 +18,8 @@ RSpec.describe Rating::Rating, ':update_rating' do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with scopeable' do
|
context 'with scopeable' do
|
||||||
|
include_context 'with_database_records'
|
||||||
|
|
||||||
it 'updates the rating data of the given resource respecting the scope' do
|
it 'updates the rating data of the given resource respecting the scope' do
|
||||||
record = described_class.find_by(resource: article_1, scopeable: category)
|
record = described_class.find_by(resource: article_1, scopeable: category)
|
||||||
|
|
||||||
|
@ -27,4 +29,20 @@ RSpec.describe Rating::Rating, ':update_rating' do
|
||||||
expect(record.total).to eq 2
|
expect(record.total).to eq 2
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when rate table has no record' do
|
||||||
|
let!(:resource) { create :article }
|
||||||
|
let!(:scope) { nil }
|
||||||
|
|
||||||
|
it 'calculates with counts values as zero' do
|
||||||
|
described_class.update_rating resource, scope
|
||||||
|
|
||||||
|
record = described_class.last
|
||||||
|
|
||||||
|
expect(record.average).to eq 0
|
||||||
|
expect(record.estimate).to eq 0
|
||||||
|
expect(record.sum).to eq 0
|
||||||
|
expect(record.total).to eq 0
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue