From 0b6c6879db59e1e0176ff15a471721a61960a5d9 Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Fri, 26 Jan 2018 19:03:07 -0200 Subject: [PATCH] up: enable pass metadata to create method --- lib/rating/models/rating/extension.rb | 4 ++-- lib/rating/models/rating/rate.rb | 2 +- spec/models/extension/rate_spec.rb | 30 +++++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/rating/models/rating/extension.rb b/lib/rating/models/rating/extension.rb index 9d445d5..a8acca7 100644 --- a/lib/rating/models/rating/extension.rb +++ b/lib/rating/models/rating/extension.rb @@ -5,8 +5,8 @@ module Rating extend ActiveSupport::Concern included do - def rate(resource, value, author: self, scope: nil) - Rate.create author: author, resource: resource, scopeable: scope, value: value + def rate(resource, value, author: self, metadata: {}, scope: nil) + Rate.create author: author, metadata: metadata, resource: resource, scopeable: scope, value: value end def rate_for(resource, scope: nil) diff --git a/lib/rating/models/rating/rate.rb b/lib/rating/models/rating/rate.rb index ab6a439..26f3966 100644 --- a/lib/rating/models/rating/rate.rb +++ b/lib/rating/models/rating/rate.rb @@ -18,7 +18,7 @@ module Rating scope: %i[author_type resource_id resource_type scopeable_id scopeable_type] } - def self.create(author:, resource:, scopeable: nil, value:) + def self.create(author:, metadata:, resource:, scopeable: nil, value:) record = find_or_initialize_by(author: author, resource: resource, scopeable: scopeable) return record if record.persisted? && value == record.value diff --git a/spec/models/extension/rate_spec.rb b/spec/models/extension/rate_spec.rb index 1fedbd2..1dc5e00 100644 --- a/spec/models/extension/rate_spec.rb +++ b/spec/models/extension/rate_spec.rb @@ -8,7 +8,9 @@ RSpec.describe Rating::Extension, ':rate' do context 'with no scopeable' do it 'delegates to rate object' do - expect(Rating::Rate).to receive(:create).with author: author, resource: article, scopeable: nil, value: 3 + expect(Rating::Rate).to receive(:create).with( + author: author, metadata: {}, resource: article, scopeable: nil, value: 3 + ) author.rate article, 3 end @@ -18,9 +20,33 @@ RSpec.describe Rating::Extension, ':rate' do let!(:category) { build :category } it 'delegates to rate object' do - expect(Rating::Rate).to receive(:create).with author: author, resource: article, scopeable: category, value: 3 + expect(Rating::Rate).to receive(:create).with( + author: author, metadata: {}, resource: article, scopeable: category, value: 3 + ) author.rate article, 3, scope: category end end + + context 'with no metadata' do + it 'delegates an empty hash to rate object' do + expect(Rating::Rate).to receive(:create).with( + author: author, resource: article, metadata: {}, scopeable: nil, value: 3 + ) + + author.rate article, 3 + end + end + + context 'with metadata' do + let!(:category) { build :category } + + it 'delegates to rate object' do + expect(Rating::Rate).to receive(:create).with( + author: author, metadata: { comment: 'comment' }, resource: article, scopeable: nil, value: 3 + ) + + author.rate article, 3, metadata: { comment: 'comment' } + end + end end