Skip to content

Commit

Permalink
Split the options checks from read_attribute_for_validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kamipo committed Dec 9, 2020
1 parent 1dee499 commit 720a60e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
4 changes: 1 addition & 3 deletions activemodel/lib/active_model/validations/numericality.rb
Expand Up @@ -108,9 +108,7 @@ def allow_only_integer?(record)
end
end

def read_attribute_for_validation(record, attr_name)
value = super

def read_attribute_for_validation(record, attr_name, value)
return value if record_attribute_changed_in_place?(record, attr_name)

came_from_user = :"#{attr_name}_came_from_user?"
Expand Down
12 changes: 5 additions & 7 deletions activemodel/lib/active_model/validator.rb
Expand Up @@ -147,10 +147,10 @@ def initialize(options)
# override +validate_each+ with validation logic.
def validate(record)
attributes.each do |attribute|
catch(:allowed) do
value = read_attribute_for_validation(record, attribute)
validate_each(record, attribute, value)
end
value = record.read_attribute_for_validation(attribute)
next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
value = read_attribute_for_validation(record, attribute, value)
validate_each(record, attribute, value)
end
end

Expand All @@ -167,9 +167,7 @@ def check_validity!
end

private
def read_attribute_for_validation(record, attr_name)
value = record.read_attribute_for_validation(attr_name)
throw(:allowed) if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
def read_attribute_for_validation(record, attr_name, value)
value
end
end
Expand Down

0 comments on commit 720a60e

Please sign in to comment.