ft: Same rate value hits DB to update metadata.

main
Washington Botelho 2020-01-16 18:48:27 -03:00
parent 0056d7eaad
commit 514ca966a2
No known key found for this signature in database
GPG Key ID: 5DE4F42A8F073617
2 changed files with 58 additions and 2 deletions

View File

@ -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

View File

@ -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'