From 514ca966a263605e62fea253e7d00e74bf43b610 Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Thu, 16 Jan 2020 18:48:27 -0300 Subject: [PATCH] ft: Same rate value hits DB to update metadata. --- lib/rating/models/rating/rate.rb | 2 -- spec/models/rate/create_spec.rb | 58 ++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/lib/rating/models/rating/rate.rb b/lib/rating/models/rating/rate.rb index 89e626a..06dfd4c 100644 --- a/lib/rating/models/rating/rate.rb +++ b/lib/rating/models/rating/rate.rb @@ -23,8 +23,6 @@ module Rating 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 diff --git a/spec/models/rate/create_spec.rb b/spec/models/rate/create_spec.rb index 2016c15..60141d6 100644 --- a/spec/models/rate/create_spec.rb +++ b/spec/models/rate/create_spec.rb @@ -192,6 +192,64 @@ RSpec.describe Rating::Rate, ':create' do 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'