up: enable pass metadata to create method

main
Washington Botelho 2018-01-26 19:03:07 -02:00
parent 71925e3dc9
commit 0b6c6879db
No known key found for this signature in database
GPG Key ID: 5DE4F42A8F073617
3 changed files with 31 additions and 5 deletions

View File

@ -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)

View File

@ -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

View File

@ -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