1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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

View file

@ -8,6 +8,10 @@ module ActiveRecord
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

View file

@ -156,6 +156,13 @@ class UniquenessValidationTest < ActiveRecord::TestCase
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)