From 722eb9ef0517fbc279b36d4555787f79ce0db902 Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Fri, 3 Nov 2017 00:02:23 -0200 Subject: [PATCH] add support to specify author models --- README.md | 9 +++++++++ lib/rating/models/rating/extension.rb | 4 ++-- spec/factories/author.rb | 7 +++++++ spec/models/extension/after_create_spec.rb | 12 ++++++++++-- spec/support/db/migrate/create_authors_table.rb | 9 +++++++++ spec/support/migrate.rb | 1 + spec/support/models/author.rb | 5 +++++ 7 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 spec/factories/author.rb create mode 100644 spec/support/db/migrate/create_authors_table.rb create mode 100644 spec/support/models/author.rb diff --git a/README.md b/README.md index 90059ef..4dbf5e8 100644 --- a/README.md +++ b/README.md @@ -272,6 +272,15 @@ article.rates_records # { value: 1 }, { value: 3, scopeable: category_1 }, { value: 5, scopeable: category_2 } ``` +### As + +If you have a model that will only be able to rate but not to receive a rate, configure it as `author`. +An author model still can be rated, but won't genarate a Rating record with all values as zero to be easier to display. + +```ruby +rating as: :author +``` + ## Love it! Via [PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=X8HEP2878NDEG&item_name=rating) or [Gratipay](https://gratipay.com/rating). Thanks! (: diff --git a/lib/rating/models/rating/extension.rb b/lib/rating/models/rating/extension.rb index 6788293..9d445d5 100644 --- a/lib/rating/models/rating/extension.rb +++ b/lib/rating/models/rating/extension.rb @@ -31,8 +31,8 @@ module Rating end module ClassMethods - def rating - after_create { Rating.find_or_create_by resource: self } + def rating(as: nil) + after_create -> { Rating.find_or_create_by resource: self }, unless: -> { as == :author } has_many :rating_records, as: :resource, diff --git a/spec/factories/author.rb b/spec/factories/author.rb new file mode 100644 index 0000000..d703aea --- /dev/null +++ b/spec/factories/author.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :author do + sequence(:name) { |i| "Author #{i}" } + end +end diff --git a/spec/models/extension/after_create_spec.rb b/spec/models/extension/after_create_spec.rb index 0ba8ff0..ce9a38f 100644 --- a/spec/models/extension/after_create_spec.rb +++ b/spec/models/extension/after_create_spec.rb @@ -3,10 +3,10 @@ require 'rails_helper' RSpec.describe Rating::Extension, ':after_create' do - context 'when creates object' do + context 'with :as as nil' do let!(:user) { create :user } - it 'creates a record with zero values just to be easy to make the count' do + it 'creates a rating record with zero values just to be easy to make the count' do rating = Rating::Rating.find_by(resource: user) expect(rating.average).to eq 0 @@ -17,4 +17,12 @@ RSpec.describe Rating::Extension, ':after_create' do expect(rating.total).to eq 0 end end + + context 'with :as as :author' do + let!(:author) { create :author } + + it 'does not creates a rating record' do + expect(Rating::Rating.exists?(resource: author)).to eq false + end + end end diff --git a/spec/support/db/migrate/create_authors_table.rb b/spec/support/db/migrate/create_authors_table.rb new file mode 100644 index 0000000..b6c44db --- /dev/null +++ b/spec/support/db/migrate/create_authors_table.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class CreateAuthorsTable < ActiveRecord::Migration[5.0] + def change + create_table :authors do |t| + t.string :name, null: false + end + end +end diff --git a/spec/support/migrate.rb b/spec/support/migrate.rb index 610fbe3..2a901fd 100644 --- a/spec/support/migrate.rb +++ b/spec/support/migrate.rb @@ -3,6 +3,7 @@ require File.expand_path('../../lib/generators/rating/templates/db/migrate/create_rating_tables.rb', __dir__) CreateArticlesTable.new.change +CreateAuthorsTable.new.change CreateCategoriesTable.new.change CreateRatingTables.new.change CreateUsersTable.new.change diff --git a/spec/support/models/author.rb b/spec/support/models/author.rb new file mode 100644 index 0000000..138e67f --- /dev/null +++ b/spec/support/models/author.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class Author < ::ActiveRecord::Base + rating as: :author +end