feat: makes table name configurable via yml
parent
b7ffd3dfc2
commit
e2aaa83598
|
@ -1,2 +1,3 @@
|
||||||
.byebug_history
|
.byebug_history
|
||||||
*.gem
|
*.gem
|
||||||
|
/config
|
||||||
|
|
|
@ -2,18 +2,22 @@
|
||||||
|
|
||||||
module Rating
|
module Rating
|
||||||
class Config
|
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)
|
return {} unless File.exist?(file_path)
|
||||||
@rate_model = rate unless rate.nil?
|
|
||||||
@rating_model = rating unless rating.nil?
|
|
||||||
|
|
||||||
self
|
YAML.safe_load(File.read(file_path))['rating']
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize
|
def rate_table
|
||||||
@rate_model = '::Rating::Rate'
|
@rate_table ||= config[__method__.to_s] || 'rating_rates'
|
||||||
@rating_model = '::Rating::Rating'
|
end
|
||||||
|
|
||||||
|
def rating_table
|
||||||
|
@rating_table ||= config[__method__.to_s] || 'rating_ratings'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -48,17 +48,17 @@ module Rating
|
||||||
|
|
||||||
has_many :rating_records,
|
has_many :rating_records,
|
||||||
as: :resource,
|
as: :resource,
|
||||||
class_name: ::Rating.config.rating_model,
|
class_name: '::Rating::Rating',
|
||||||
dependent: :destroy
|
dependent: :destroy
|
||||||
|
|
||||||
has_many :rates_records,
|
has_many :rates_records,
|
||||||
as: :resource,
|
as: :resource,
|
||||||
class_name: ::Rating.config.rate_model,
|
class_name: '::Rating::Rate',
|
||||||
dependent: :destroy
|
dependent: :destroy
|
||||||
|
|
||||||
has_many :rated_records,
|
has_many :rated_records,
|
||||||
as: :author,
|
as: :author,
|
||||||
class_name: ::Rating.config.rate_model,
|
class_name: '::Rating::Rate',
|
||||||
dependent: :destroy
|
dependent: :destroy
|
||||||
|
|
||||||
scope :order_by_rating, ->(column = :estimate, direction = :desc, scope: nil) {
|
scope :order_by_rating, ->(column = :estimate, direction = :desc, scope: nil) {
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
module Rating
|
module Rating
|
||||||
class Rate < ActiveRecord::Base
|
class Rate < ActiveRecord::Base
|
||||||
self.table_name_prefix = 'rating_'
|
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
|
after_save :update_rating
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
module Rating
|
module Rating
|
||||||
class Rating < ActiveRecord::Base
|
class Rating < ActiveRecord::Base
|
||||||
self.table_name_prefix = 'rating_'
|
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 :resource, polymorphic: true
|
||||||
belongs_to :scopeable, polymorphic: true
|
belongs_to :scopeable, polymorphic: true
|
||||||
|
|
|
@ -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
|
|
|
@ -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
|
|
|
@ -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
|
|
|
@ -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
|
|
@ -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
|
|
@ -3,5 +3,6 @@
|
||||||
class AddCommentOnRatingRatesTable < ActiveRecord::Migration[5.0]
|
class AddCommentOnRatingRatesTable < ActiveRecord::Migration[5.0]
|
||||||
def change
|
def change
|
||||||
add_column :rating_rates, :comment, :text
|
add_column :rating_rates, :comment, :text
|
||||||
|
add_column :reviews, :comment, :text
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Review < ::ActiveRecord::Base
|
class Review < ::ActiveRecord::Base
|
||||||
|
belongs_to :scopeable, polymorphic: true
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue