feat: enable custom validation via yml config
parent
2af3187190
commit
1365f948db
|
@ -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
|
||||
|
|
|
@ -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:)
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue