feat: makes table name configurable via yml

main
Washington Botelho 2018-02-14 14:43:13 -02:00
parent b7ffd3dfc2
commit e2aaa83598
No known key found for this signature in database
GPG Key ID: 5DE4F42A8F073617
12 changed files with 46 additions and 56 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.byebug_history
*.gem
/config

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,4 +1,5 @@
# frozen_string_literal: true
class Review < ::ActiveRecord::Base
belongs_to :scopeable, polymorphic: true
end