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

Refactor missing parameter validation based on position

This commit is contained in:
Carlos Antonio da Silva 2012-06-27 23:40:16 -03:00
parent a0f9dc7657
commit 5021c13cea
2 changed files with 12 additions and 8 deletions

View file

@ -211,9 +211,7 @@ module ActiveRecord
end
else
# else column is a timestamp, so if Date bits were not provided, error
if missing_parameter = [1,2,3].detect{ |position| !values_hash_from_param.has_key?(position) }
raise ArgumentError.new("Missing Parameter - #{name}(#{missing_parameter}i)")
end
validate_missing_parameters!(name, [1,2,3], values_hash_from_param)
# If Date bits were provided but blank, then return nil
return nil if (1..3).any? { |position| values_hash_from_param[position].blank? }
@ -238,13 +236,20 @@ module ActiveRecord
def read_other_parameter_value(klass, name, values_hash_from_param)
max_position = extract_max_param_for_multiparameter_attributes(values_hash_from_param)
values = (1..max_position).collect do |position|
raise "Missing Parameter" if !values_hash_from_param.has_key?(position)
values_hash_from_param[position]
end
positions = (1..max_position)
validate_missing_parameters!(name, positions, values_hash_from_param)
values = values_hash_from_param.values_at(*positions)
klass.new(*values)
end
# If some position is not provided, it errors out a missing parameter exception.
def validate_missing_parameters!(name, positions, values_hash)
if missing_parameter = positions.detect { |position| !values_hash.key?(position) }
raise ArgumentError.new("Missing Parameter - #{name}(#{missing_parameter})")
end
end
def extract_max_param_for_multiparameter_attributes(values_hash_from_param, upper_cap = 100)
[values_hash_from_param.keys.max,upper_cap].min
end

View file

@ -34,7 +34,6 @@ class AttributeMethodsTest < ActiveRecord::TestCase
assert t.attribute_present?("written_on")
assert !t.attribute_present?("content")
assert !t.attribute_present?("author_name")
end
def test_attribute_present_with_booleans