diff --git a/lib/rating.rb b/lib/rating.rb index 68a56da..09b8a6c 100644 --- a/lib/rating.rb +++ b/lib/rating.rb @@ -1,8 +1,18 @@ # frozen_string_literal: true module Rating + class << self + def config + @config ||= Config.new + end + + def configure + yield config + end + end end +require 'rating/config' require 'rating/models/rating/extension' require 'rating/models/rating/rate' require 'rating/models/rating/rating' diff --git a/lib/rating/config.rb b/lib/rating/config.rb new file mode 100644 index 0000000..3622683 --- /dev/null +++ b/lib/rating/config.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Rating + class Config + attr_accessor :rate_model, :rating_model + + def models(rate: nil, rating: nil) + @rate_model = rate unless rate.nil? + @rating_model = rating unless rating.nil? + + self + end + + def initialize + @rate_model = ::Rating::Rate + @rating_model = ::Rating::Rating + end + end +end diff --git a/lib/rating/models/rating/rating.rb b/lib/rating/models/rating/rating.rb index d43c169..b8d400e 100644 --- a/lib/rating/models/rating/rating.rb +++ b/lib/rating/models/rating/rating.rb @@ -2,7 +2,7 @@ module Rating class Rating < ActiveRecord::Base - self.table_name = 'rating_ratings' + self.table_name = ::Rating.config.rating_model belongs_to :resource, polymorphic: true belongs_to :scopeable, polymorphic: true @@ -107,7 +107,7 @@ module Rating end def rate_table_name - @rate_table_name ||= Rate.table_name + @rate_table_name ||= ::Rating.config.rate_model end def scope_type_query(scopeable) diff --git a/spec/config/initialize_spec.rb b/spec/config/initialize_spec.rb new file mode 100644 index 0000000..6a395cf --- /dev/null +++ b/spec/config/initialize_spec.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Rating::Config, 'initialize' do + it 'has default models' do + expect(subject.rate_model).to eq ::Rating::Rate + expect(subject.rating_model).to eq ::Rating::Rating + end +end diff --git a/spec/config/tables_spec.rb b/spec/config/tables_spec.rb new file mode 100644 index 0000000..94b5d61 --- /dev/null +++ b/spec/config/tables_spec.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Rating::Config, 'models' do + it 'changes the rating models' do + subject.models rate: Review, rating: ReviewRating + + expect(subject.rate_model).to eq Review + expect(subject.rating_model).to eq ReviewRating + end +end diff --git a/spec/support/db/migrate/create_review_ratings_table.rb b/spec/support/db/migrate/create_review_ratings_table.rb new file mode 100644 index 0000000..a19efdd --- /dev/null +++ b/spec/support/db/migrate/create_review_ratings_table.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class CreateReviewRatingsTable < ActiveRecord::Migration[5.0] + def change + create_table :review_ratings do |t| + t.decimal :average, default: 0, mull: false, precision: 25, scale: 16 + t.decimal :estimate, default: 0, mull: false, precision: 25, scale: 16 + t.integer :sum, default: 0, mull: false + t.integer :total, default: 0, mull: false + + t.references :resource, index: true, null: false, polymorphic: true + t.references :scopeable, index: true, null: true, polymorphic: true + + t.timestamps null: false + end + end +end diff --git a/spec/support/db/migrate/create_reviews_table.rb b/spec/support/db/migrate/create_reviews_table.rb new file mode 100644 index 0000000..c03d815 --- /dev/null +++ b/spec/support/db/migrate/create_reviews_table.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class CreateReviewsTable < ActiveRecord::Migration[5.0] + def change + create_table :reviews do |t| + t.decimal :value, default: 0, precision: 25, scale: 16 + + t.references :author, index: true, null: false, polymorphic: true + t.references :resource, index: true, null: false, polymorphic: true + t.references :scopeable, index: true, null: true, polymorphic: true + + t.timestamps null: false + end + end +end diff --git a/spec/support/migrate.rb b/spec/support/migrate.rb index e37b496..eb1f84f 100644 --- a/spec/support/migrate.rb +++ b/spec/support/migrate.rb @@ -10,4 +10,6 @@ CreateAuthorsTable.new.change CreateCategoriesTable.new.change CreateRateTable.new.change CreateRatingTable.new.change +CreateReviewRatingsTable.new.change +CreateReviewsTable.new.change AddCommentOnRatingRatesTable.new.change diff --git a/spec/support/models/review.rb b/spec/support/models/review.rb new file mode 100644 index 0000000..d88fa65 --- /dev/null +++ b/spec/support/models/review.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class Review < ::ActiveRecord::Base +end diff --git a/spec/support/models/review_rating.rb b/spec/support/models/review_rating.rb new file mode 100644 index 0000000..84ba430 --- /dev/null +++ b/spec/support/models/review_rating.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class ReviewRating < ::ActiveRecord::Base +end