From 5021c13ceaccd7a1b80638bfc564470db22ea520 Mon Sep 17 00:00:00 2001
From: Carlos Antonio da Silva <carlosantoniodasilva@gmail.com>
Date: Wed, 27 Jun 2012 23:40:16 -0300
Subject: Refactor missing parameter validation based on position

---
 .../lib/active_record/attribute_assignment.rb         | 19 ++++++++++++-------
 activerecord/test/cases/attribute_methods_test.rb     |  1 -
 2 files changed, 12 insertions(+), 8 deletions(-)

(limited to 'activerecord')

diff --git a/activerecord/lib/active_record/attribute_assignment.rb b/activerecord/lib/active_record/attribute_assignment.rb
index e174758fd9..b030e64562 100644
--- a/activerecord/lib/active_record/attribute_assignment.rb
+++ b/activerecord/lib/active_record/attribute_assignment.rb
@@ -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
diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb
index 807971d678..4bc68acd13 100644
--- a/activerecord/test/cases/attribute_methods_test.rb
+++ b/activerecord/test/cases/attribute_methods_test.rb
@@ -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
-- 
cgit v1.2.3