feat: add tables name config

main
Ion Drimba Filho 2018-02-08 13:29:20 -02:00 committed by Washington Botelho
parent 754aaca0e8
commit ab9610bfe0
No known key found for this signature in database
GPG Key ID: 5DE4F42A8F073617
10 changed files with 95 additions and 2 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,4 @@
# frozen_string_literal: true
class Review < ::ActiveRecord::Base
end

View File

@ -0,0 +1,4 @@
# frozen_string_literal: true
class ReviewRating < ::ActiveRecord::Base
end