feat: add ability to persist metadata

main
Washington Botelho 2018-01-26 19:16:56 -02:00
parent 55cd7a3093
commit b0738b5cc8
No known key found for this signature in database
GPG Key ID: 5DE4F42A8F073617
3 changed files with 52 additions and 0 deletions

View File

@ -23,6 +23,8 @@ module Rating
return record if record.persisted? && value == record.value
metadata.each { |k, v| record[k] = v } if metadata.present?
record.value = value
record.save

View File

@ -128,4 +128,47 @@ RSpec.describe Rating::Rate, ':create' do
end
end
end
context 'with metadata' do
before { AddCommentOnRatingRatesTable.new.change }
context 'with nil value' do
it 'creates a rate entry ignoring metadata' do
described_class.create author: author, metadata: nil, resource: article, value: 3
rate = described_class.last
expect(rate.author).to eq author
expect(rate.comment).to eq nil
expect(rate.resource).to eq article
expect(rate.value).to eq 3
end
end
context 'with empty value' do
it 'creates a rate entry ignoring metadata' do
described_class.create author: author, metadata: '', resource: article, value: 3
rate = described_class.last
expect(rate.author).to eq author
expect(rate.comment).to eq nil
expect(rate.resource).to eq article
expect(rate.value).to eq 3
end
end
context 'with hash value' do
it 'creates a rate entry with metadata included' do
described_class.create author: author, metadata: { comment: 'comment' }, resource: article, value: 3
rate = described_class.last
expect(rate.author).to eq author
expect(rate.comment).to eq 'comment'
expect(rate.resource).to eq article
expect(rate.value).to eq 3
end
end
end
end

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddCommentOnRatingRatesTable < ActiveRecord::Migration[5.0]
def change
add_column :rating_rates, :comment, :text
end
end