Skip to content

Commit

Permalink
ft: Same rate value hits DB to update metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
wbotelhos committed Jan 16, 2020
1 parent 0056d7e commit 514ca96
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
2 changes: 0 additions & 2 deletions lib/rating/models/rating/rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ def self.create(author:, extra_scopes:, metadata:, resource:, scopeable: nil, va
attributes = { author: author, resource: resource, scopeable: scopeable }.merge(extra_scopes)
record = find_or_initialize_by(attributes)

return record if record.persisted? && value == record.value

metadata.each { |k, v| record[k] = v } if metadata.present?

record.value = value
Expand Down
58 changes: 58 additions & 0 deletions spec/models/rate/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,64 @@
expect(rate.value).to eq 3
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

if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
Expand Down

0 comments on commit 514ca96

Please sign in to comment.