提交 a62f4506 编写于 作者: X Xavier Noria

Merge pull request #2086 from amatsuda/date_multiparameter_nil

convert multiple Date parameters into a nil if any of its bits were blank
...@@ -2027,15 +2027,18 @@ def read_time_parameter_value(name, values_hash_from_param) ...@@ -2027,15 +2027,18 @@ def read_time_parameter_value(name, values_hash_from_param)
# If Date bits were not provided, error # If Date bits were not provided, error
raise "Missing Parameter" if [1,2,3].any?{|position| !values_hash_from_param.has_key?(position)} raise "Missing Parameter" if [1,2,3].any?{|position| !values_hash_from_param.has_key?(position)}
max_position = extract_max_param_for_multiparameter_attributes(values_hash_from_param, 6) max_position = extract_max_param_for_multiparameter_attributes(values_hash_from_param, 6)
# If Date bits were provided but blank, then return nil
return nil if (1..3).any? {|position| values_hash_from_param[position].blank?}
set_values = (1..max_position).collect{|position| values_hash_from_param[position] } set_values = (1..max_position).collect{|position| values_hash_from_param[position] }
# If Date bits were provided but blank, then default to 1
# If Time bits are not there, then default to 0 # If Time bits are not there, then default to 0
[1,1,1,0,0,0].each_with_index{|v,i| set_values[i] = set_values[i].blank? ? v : set_values[i]} (3..5).each {|i| set_values[i] = set_values[i].blank? ? 0 : set_values[i]}
instantiate_time_object(name, set_values) instantiate_time_object(name, set_values)
end end
def read_date_parameter_value(name, values_hash_from_param) def read_date_parameter_value(name, values_hash_from_param)
set_values = (1..3).collect{|position| values_hash_from_param[position].blank? ? 1 : values_hash_from_param[position]} return nil if (1..3).any? {|position| values_hash_from_param[position].blank?}
set_values = [values_hash_from_param[1], values_hash_from_param[2], values_hash_from_param[3]]
begin begin
Date.new(*set_values) Date.new(*set_values)
rescue ArgumentError # if Date.new raises an exception on an invalid date rescue ArgumentError # if Date.new raises an exception on an invalid date
......
...@@ -540,7 +540,7 @@ def test_multiparameter_attributes_on_date_with_empty_year ...@@ -540,7 +540,7 @@ def test_multiparameter_attributes_on_date_with_empty_year
topic.attributes = attributes topic.attributes = attributes
# note that extra #to_date call allows test to pass for Oracle, which # note that extra #to_date call allows test to pass for Oracle, which
# treats dates/times the same # treats dates/times the same
assert_date_from_db Date.new(1, 6, 24), topic.last_read.to_date assert_nil topic.last_read
end end
def test_multiparameter_attributes_on_date_with_empty_month def test_multiparameter_attributes_on_date_with_empty_month
...@@ -549,7 +549,7 @@ def test_multiparameter_attributes_on_date_with_empty_month ...@@ -549,7 +549,7 @@ def test_multiparameter_attributes_on_date_with_empty_month
topic.attributes = attributes topic.attributes = attributes
# note that extra #to_date call allows test to pass for Oracle, which # note that extra #to_date call allows test to pass for Oracle, which
# treats dates/times the same # treats dates/times the same
assert_date_from_db Date.new(2004, 1, 24), topic.last_read.to_date assert_nil topic.last_read
end end
def test_multiparameter_attributes_on_date_with_empty_day def test_multiparameter_attributes_on_date_with_empty_day
...@@ -558,7 +558,7 @@ def test_multiparameter_attributes_on_date_with_empty_day ...@@ -558,7 +558,7 @@ def test_multiparameter_attributes_on_date_with_empty_day
topic.attributes = attributes topic.attributes = attributes
# note that extra #to_date call allows test to pass for Oracle, which # note that extra #to_date call allows test to pass for Oracle, which
# treats dates/times the same # treats dates/times the same
assert_date_from_db Date.new(2004, 6, 1), topic.last_read.to_date assert_nil topic.last_read
end end
def test_multiparameter_attributes_on_date_with_empty_day_and_year def test_multiparameter_attributes_on_date_with_empty_day_and_year
...@@ -567,7 +567,7 @@ def test_multiparameter_attributes_on_date_with_empty_day_and_year ...@@ -567,7 +567,7 @@ def test_multiparameter_attributes_on_date_with_empty_day_and_year
topic.attributes = attributes topic.attributes = attributes
# note that extra #to_date call allows test to pass for Oracle, which # note that extra #to_date call allows test to pass for Oracle, which
# treats dates/times the same # treats dates/times the same
assert_date_from_db Date.new(1, 6, 1), topic.last_read.to_date assert_nil topic.last_read
end end
def test_multiparameter_attributes_on_date_with_empty_day_and_month def test_multiparameter_attributes_on_date_with_empty_day_and_month
...@@ -576,7 +576,7 @@ def test_multiparameter_attributes_on_date_with_empty_day_and_month ...@@ -576,7 +576,7 @@ def test_multiparameter_attributes_on_date_with_empty_day_and_month
topic.attributes = attributes topic.attributes = attributes
# note that extra #to_date call allows test to pass for Oracle, which # note that extra #to_date call allows test to pass for Oracle, which
# treats dates/times the same # treats dates/times the same
assert_date_from_db Date.new(2004, 1, 1), topic.last_read.to_date assert_nil topic.last_read
end end
def test_multiparameter_attributes_on_date_with_empty_year_and_month def test_multiparameter_attributes_on_date_with_empty_year_and_month
...@@ -585,7 +585,7 @@ def test_multiparameter_attributes_on_date_with_empty_year_and_month ...@@ -585,7 +585,7 @@ def test_multiparameter_attributes_on_date_with_empty_year_and_month
topic.attributes = attributes topic.attributes = attributes
# note that extra #to_date call allows test to pass for Oracle, which # note that extra #to_date call allows test to pass for Oracle, which
# treats dates/times the same # treats dates/times the same
assert_date_from_db Date.new(1, 1, 24), topic.last_read.to_date assert_nil topic.last_read
end end
def test_multiparameter_attributes_on_date_with_all_empty def test_multiparameter_attributes_on_date_with_all_empty
...@@ -678,12 +678,7 @@ def test_multiparameter_attributes_on_time_will_ignore_hour_if_blank ...@@ -678,12 +678,7 @@ def test_multiparameter_attributes_on_time_will_ignore_hour_if_blank
} }
topic = Topic.find(1) topic = Topic.find(1)
topic.attributes = attributes topic.attributes = attributes
assert_equal 1, topic.written_on.year assert_nil topic.written_on
assert_equal 1, topic.written_on.month
assert_equal 1, topic.written_on.day
assert_equal 0, topic.written_on.hour
assert_equal 12, topic.written_on.min
assert_equal 2, topic.written_on.sec
end end
def test_multiparameter_attributes_on_time_will_ignore_date_if_empty def test_multiparameter_attributes_on_time_will_ignore_date_if_empty
...@@ -693,12 +688,7 @@ def test_multiparameter_attributes_on_time_will_ignore_date_if_empty ...@@ -693,12 +688,7 @@ def test_multiparameter_attributes_on_time_will_ignore_date_if_empty
} }
topic = Topic.find(1) topic = Topic.find(1)
topic.attributes = attributes topic.attributes = attributes
assert_equal 1, topic.written_on.year assert_nil topic.written_on
assert_equal 1, topic.written_on.month
assert_equal 1, topic.written_on.day
assert_equal 16, topic.written_on.hour
assert_equal 24, topic.written_on.min
assert_equal 0, topic.written_on.sec
end end
def test_multiparameter_attributes_on_time_with_seconds_will_ignore_date_if_empty def test_multiparameter_attributes_on_time_with_seconds_will_ignore_date_if_empty
attributes = { attributes = {
...@@ -707,12 +697,7 @@ def test_multiparameter_attributes_on_time_with_seconds_will_ignore_date_if_empt ...@@ -707,12 +697,7 @@ def test_multiparameter_attributes_on_time_with_seconds_will_ignore_date_if_empt
} }
topic = Topic.find(1) topic = Topic.find(1)
topic.attributes = attributes topic.attributes = attributes
assert_equal 1, topic.written_on.year assert_nil topic.written_on
assert_equal 1, topic.written_on.month
assert_equal 1, topic.written_on.day
assert_equal 16, topic.written_on.hour
assert_equal 12, topic.written_on.min
assert_equal 02, topic.written_on.sec
end end
def test_multiparameter_attributes_on_time_with_utc def test_multiparameter_attributes_on_time_with_utc
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册