Check :scope input in Uniqueness validator

This commit is contained in:
Kir Shatrov 2017-08-12 21:11:58 +03:00
parent 98360a96cc
commit 883b2a8cee
2 changed files with 11 additions and 0 deletions

@ -8,6 +8,10 @@ def initialize(options)
raise ArgumentError, "#{options[:conditions]} was passed as :conditions but is not callable. " \
"Pass a callable instead: `conditions: -> { where(approved: true) }`"
end
unless Array(options[:scope]).all? { |scope| scope.respond_to?(:to_sym) }
raise ArgumentError, "#{options[:scope]} is not supported format for :scope option. " \
"Pass a symbol or an array of symbols instead: `scope: :user_id`"
end
super({ case_sensitive: true }.merge!(options))
@klass = options[:class]
end

@ -156,6 +156,13 @@ def test_validate_uniqueness_with_scope
assert r3.valid?, "Saving r3"
end
def test_validate_uniqueness_with_scope_invalid_syntax
error = assert_raises(ArgumentError) do
Reply.validates_uniqueness_of(:content, scope: { parent_id: false })
end
assert_match(/Pass a symbol or an array of symbols instead/, error.to_s)
end
def test_validate_uniqueness_with_object_scope
Reply.validates_uniqueness_of(:content, scope: :topic)