diff --git a/.gitignore b/.gitignore index 7e1052c..148fc15 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .byebug_history *.gem +/config diff --git a/lib/rating/config.rb b/lib/rating/config.rb index 73c5d9e..172b793 100644 --- a/lib/rating/config.rb +++ b/lib/rating/config.rb @@ -2,18 +2,22 @@ module Rating class Config - attr_accessor :rate_model, :rating_model + def config + @config ||= begin + file_path = File.expand_path('config/rating.yml') - def models(rate: nil, rating: nil) - @rate_model = rate unless rate.nil? - @rating_model = rating unless rating.nil? + return {} unless File.exist?(file_path) - self + YAML.safe_load(File.read(file_path))['rating'] + end end - def initialize - @rate_model = '::Rating::Rate' - @rating_model = '::Rating::Rating' + def rate_table + @rate_table ||= config[__method__.to_s] || 'rating_rates' + end + + def rating_table + @rating_table ||= config[__method__.to_s] || 'rating_ratings' end end end diff --git a/lib/rating/models/rating/extension.rb b/lib/rating/models/rating/extension.rb index e44fb82..e4365ca 100644 --- a/lib/rating/models/rating/extension.rb +++ b/lib/rating/models/rating/extension.rb @@ -48,17 +48,17 @@ module Rating has_many :rating_records, as: :resource, - class_name: ::Rating.config.rating_model, + class_name: '::Rating::Rating', dependent: :destroy has_many :rates_records, as: :resource, - class_name: ::Rating.config.rate_model, + class_name: '::Rating::Rate', dependent: :destroy has_many :rated_records, as: :author, - class_name: ::Rating.config.rate_model, + class_name: '::Rating::Rate', dependent: :destroy scope :order_by_rating, ->(column = :estimate, direction = :desc, scope: nil) { diff --git a/lib/rating/models/rating/rate.rb b/lib/rating/models/rating/rate.rb index 2329ecb..9cc98f1 100644 --- a/lib/rating/models/rating/rate.rb +++ b/lib/rating/models/rating/rate.rb @@ -3,7 +3,7 @@ module Rating class Rate < ActiveRecord::Base self.table_name_prefix = 'rating_' - self.table_name = ::Rating.config.rate_model.constantize.table_name + self.table_name = ::Rating.config.rate_table after_save :update_rating diff --git a/lib/rating/models/rating/rating.rb b/lib/rating/models/rating/rating.rb index a5c4dfa..23e55d4 100644 --- a/lib/rating/models/rating/rating.rb +++ b/lib/rating/models/rating/rating.rb @@ -3,7 +3,7 @@ module Rating class Rating < ActiveRecord::Base self.table_name_prefix = 'rating_' - self.table_name = ::Rating.config.rating_model.constantize.table_name + self.table_name = ::Rating.config.rating_table belongs_to :resource, polymorphic: true belongs_to :scopeable, polymorphic: true diff --git a/spec/config/configure_spec.rb b/spec/config/configure_spec.rb deleted file mode 100644 index cafdd42..0000000 --- a/spec/config/configure_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe Rating::Config, '.configure' do - before do - Rating.configure do |config| - config.models rate: 'Review', rating: 'ReviewRating' - end - end - - let!(:author) { create :author } - let!(:resource) { create :article } - - xit 'changes the rating models' do - author.rate resource, 5 - - expect(Review.count).to eq 1 - expect(ReviewRating.count).to eq 1 - end -end diff --git a/spec/config/initialize_spec.rb b/spec/config/initialize_spec.rb deleted file mode 100644 index 520a932..0000000 --- a/spec/config/initialize_spec.rb +++ /dev/null @@ -1,10 +0,0 @@ -# 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/models_spec.rb b/spec/config/models_spec.rb deleted file mode 100644 index e0fbcc6..0000000 --- a/spec/config/models_spec.rb +++ /dev/null @@ -1,12 +0,0 @@ -# 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/config/rate_table_spec.rb b/spec/config/rate_table_spec.rb new file mode 100644 index 0000000..72968fa --- /dev/null +++ b/spec/config/rate_table_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Rating::Config, '.rate_table' do + context 'when rating.yml does not exist' do + it { expect(subject.rate_table).to eq 'rating_rates' } + end if ENV['CONFIG_ENABLED'] != 'true' + + context 'when rating.yml exists' do + it { expect(subject.rate_table).to eq 'reviews' } + end if ENV['CONFIG_ENABLED'] == 'true' +end diff --git a/spec/config/rating_table_spec.rb b/spec/config/rating_table_spec.rb new file mode 100644 index 0000000..9d9bdc9 --- /dev/null +++ b/spec/config/rating_table_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Rating::Config, '.rating_table' do + context 'when rating.yml does not exist' do + it { expect(subject.rating_table).to eq 'rating_ratings' } + end if ENV['CONFIG_ENABLED'] != 'true' + + context 'when rating.yml exists' do + it { expect(subject.rating_table).to eq 'review_ratings' } + end if ENV['CONFIG_ENABLED'] == 'true' +end diff --git a/spec/support/db/migrate/add_comment_on_rating_rates_table.rb b/spec/support/db/migrate/add_comment_on_rating_rates_table.rb index 690c213..e53c402 100644 --- a/spec/support/db/migrate/add_comment_on_rating_rates_table.rb +++ b/spec/support/db/migrate/add_comment_on_rating_rates_table.rb @@ -3,5 +3,6 @@ class AddCommentOnRatingRatesTable < ActiveRecord::Migration[5.0] def change add_column :rating_rates, :comment, :text + add_column :reviews, :comment, :text end end diff --git a/spec/support/models/review.rb b/spec/support/models/review.rb index d88fa65..f1a3dc3 100644 --- a/spec/support/models/review.rb +++ b/spec/support/models/review.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true class Review < ::ActiveRecord::Base + belongs_to :scopeable, polymorphic: true end