feat: enable custom validation via yml config

main
Washington Botelho 2018-03-01 17:49:51 -03:00
parent 2af3187190
commit 1365f948db
No known key found for this signature in database
GPG Key ID: 5DE4F42A8F073617
3 changed files with 42 additions and 2 deletions

View File

@ -21,5 +21,18 @@ module Rating
def rating_table
@rating_table ||= config[__method__.to_s] || 'rating_ratings'
end
def validations
@validations ||= begin
default_scope = %w[author_type resource_id resource_type scopeable_id scopeable_type]
{
rate: {
case_sensitive: config.dig('validations', 'rate', 'case_sensitive') || false,
scope: config.dig('validations', 'rate', 'scope') || default_scope
}
}.deep_stringify_keys
end
end
end
end

View File

@ -15,8 +15,8 @@ module Rating
validates :value, numericality: { greater_than_or_equal_to: 1, less_than_or_equal_to: 100 }
validates :author_id, uniqueness: {
case_sensitive: false,
scope: %i[author_type resource_id resource_type scopeable_id scopeable_type]
case_sensitive: ::Rating::Config.validations['rate']['case_sensitive'],
scope: ::Rating::Config.validations['rate']['scope'].map(&:to_sym)
}
def self.create(author:, extra_scopes:, metadata:, resource:, scopeable: nil, value:)

View File

@ -0,0 +1,27 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Rating::Config, '.validations' do
context 'when rating.yml does not exist' do
it do
expect(subject.validations).to eq({
rate: {
case_sensitive: false,
scope: %w[author_type resource_id resource_type scopeable_id scopeable_type]
}
}.deep_stringify_keys)
end
end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] != 'true'
context 'when rating.yml exists' do
it do
expect(subject.validations).to eq({
rate: {
case_sensitive: false,
scope: %w[author_type resource_id resource_type scopeable_id scopeable_type scope_1 scope_2]
}
}.deep_stringify_keys)
end
end if ENV['CONFIG_ENABLED_WITH_EXTRA_SCOPES'] == 'true'
end