lib/snake-eyes/interface_changes.rb in snake-eyes-0.0.6 vs lib/snake-eyes/interface_changes.rb in snake-eyes-0.0.7

- old
+ new

@@ -22,11 +22,11 @@ nested_schema = build_nested_schema(options[:nested_attributes] || {}) @previous_params ||= { } - return @previous_params[nested_schema] if @previous_params[nested_schema] + # return @previous_params[nested_schema] if @previous_params[nested_schema] # Similar to original_params_sub_trees, a list of subtrees used to maintain # the traversal position of nested_schema. This is kept in sync with the # traversal of original_params, to ensure the correct leaf of # nested_schema is checked at each point in the traversal of original_params @@ -34,44 +34,41 @@ nested_schema_sub_trees = [ nested_schema ] transformed_params = original_params.deep_transform_keys do |original_key| - # The matching leaf of nested_schema for this point in the traversal - nested_schema_sub_tree = nested_schema_sub_trees.last + # Synchronise the original params sub-tree with the current key being + # transformed. We can detect that the sub-tree is stale because the key + # being transformed does not appear amongst its own. When the sub-tree is + # indeed stale, move the position to its parent for the original params + # sub-tree and the nested schema sub-tree and repeat the check. + while original_params_sub_trees.length > 1 && original_params_sub_trees.last[original_key].nil? + original_params_sub_trees.pop + nested_schema_sub_trees.pop + end + + original_params_sub_tree = original_params_sub_trees.last[original_key] + # Append the '_attributes' suffix if the original params key has the # same name and is nested in the same place as one mentioned in the # nested_attributes option transformed_key_base = original_key.underscore transformed_key = - if nested_schema_sub_tree[transformed_key_base] + if nested_schema_sub_trees.last[transformed_key_base] transformed_key_base + '_attributes' else transformed_key_base end - # Synchronise the original params sub-tree with the current key being - # transformed. We can detect that the sub-tree is stale because the key - # being transformed does not appear amongst its own. When the sub-tree is - # indeed stale, move the position to its parent for the original params - # sub-tree and the nested schema sub-tree and repeat the check. - - while original_params_sub_trees.length > 1 && original_params_sub_trees.last[original_key].nil? - original_params_sub_trees.pop - nested_schema_sub_trees.pop - end - - original_params_sub_tree = original_params_sub_trees.last[transformed_key_base] - if original_params_sub_tree.kind_of?(Hash) original_params_sub_trees.push(original_params_sub_tree) nested_schema_sub_trees.push( - nested_schema_sub_tree[transformed_key_base] || - nested_schema_sub_tree['_' + transformed_key_base] || + nested_schema_sub_trees.last[transformed_key_base] || + nested_schema_sub_trees.last['_' + transformed_key_base] || {} ) end transformed_key