feat: add ability to persist metadata
This commit is contained in:
		
							parent
							
								
									55cd7a3093
								
							
						
					
					
						commit
						b0738b5cc8
					
				@ -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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,7 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class AddCommentOnRatingRatesTable < ActiveRecord::Migration[5.0]
 | 
			
		||||
  def change
 | 
			
		||||
    add_column :rating_rates, :comment, :text
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user