ft: Same rate value hits DB to update metadata.
parent
0056d7eaad
commit
514ca966a2
|
@ -23,8 +23,6 @@ module Rating
|
||||||
attributes = { author: author, resource: resource, scopeable: scopeable }.merge(extra_scopes)
|
attributes = { author: author, resource: resource, scopeable: scopeable }.merge(extra_scopes)
|
||||||
record = find_or_initialize_by(attributes)
|
record = find_or_initialize_by(attributes)
|
||||||
|
|
||||||
return record if record.persisted? && value == record.value
|
|
||||||
|
|
||||||
metadata.each { |k, v| record[k] = v } if metadata.present?
|
metadata.each { |k, v| record[k] = v } if metadata.present?
|
||||||
|
|
||||||
record.value = value
|
record.value = value
|
||||||
|
|
|
@ -192,6 +192,64 @@ RSpec.describe Rating::Rate, ':create' do
|
||||||
expect(rate.value).to eq 3
|
expect(rate.value).to eq 3
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when already has an entry' do
|
||||||
|
before do
|
||||||
|
described_class.create(
|
||||||
|
author: author,
|
||||||
|
extra_scopes: {},
|
||||||
|
metadata: { comment: 'comment' },
|
||||||
|
resource: article,
|
||||||
|
value: 1
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when value is the same' do
|
||||||
|
it 'still updates metadata' do
|
||||||
|
described_class.create(
|
||||||
|
author: author,
|
||||||
|
extra_scopes: {},
|
||||||
|
metadata: { comment: 'comment.updated' },
|
||||||
|
resource: article,
|
||||||
|
value: 1
|
||||||
|
)
|
||||||
|
|
||||||
|
rates = described_class.all
|
||||||
|
|
||||||
|
expect(rates.size).to eq 1
|
||||||
|
|
||||||
|
rate = rates[0]
|
||||||
|
|
||||||
|
expect(rate.author).to eq author
|
||||||
|
expect(rate.comment).to eq 'comment.updated'
|
||||||
|
expect(rate.resource).to eq article
|
||||||
|
expect(rate.value).to eq 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when value is different same' do
|
||||||
|
it 'still updates metadata' do
|
||||||
|
described_class.create(
|
||||||
|
author: author,
|
||||||
|
extra_scopes: {},
|
||||||
|
metadata: { comment: 'comment.updated' },
|
||||||
|
resource: article,
|
||||||
|
value: 2
|
||||||
|
)
|
||||||
|
|
||||||
|
rates = described_class.all
|
||||||
|
|
||||||
|
expect(rates.size).to eq 1
|
||||||
|
|
||||||
|
rate = rates[0]
|
||||||
|
|
||||||
|
expect(rate.author).to eq author
|
||||||
|
expect(rate.comment).to eq 'comment.updated'
|
||||||
|
expect(rate.resource).to eq article
|
||||||
|
expect(rate.value).to eq 2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
|
if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
|
||||||
|
|
Loading…
Reference in New Issue