Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

morph_attributes called recursively on array elements and hash values

  • Loading branch information...
commit bb29d423fb0abbcb7383eb30fc6a1ad226a20cdf 1 parent ba25bef
@robmckinnon robmckinnon authored
Showing with 29 additions and 1 deletion.
  1. +8 −0 lib/morph.rb
  2. +21 −1 spec/morph_spec.rb
View
8 lib/morph.rb
@@ -96,7 +96,15 @@ def morph_attributes
unless attribute =~ /=\Z/
symbol = attribute.to_sym
value = send(symbol)
+
+ value.each do |key, v|
+ value[key] = v.morph_attributes if v.respond_to?(:morph_attributes)
+ end if value.is_a? Hash
+
+ value = value.collect {|v| v.respond_to?(:morph_attributes) ? v.morph_attributes : v } if value.is_a? Array
value = value.morph_attributes if value.respond_to? :morph_attributes
+
+
hash[symbol] = value
end
hash
View
22 spec/morph_spec.rb
@@ -62,7 +62,7 @@
@another_morphed_class.morph_methods.should == ['no','no=']
end
- it 'should convert call morph_attributes on both objects, when one object has a reference to another' do
+ it 'should call morph_attributes on both objects, when one object has a reference to another' do
@morph.every = 'which'
@another_morph.way = 'but'
@morph.loose = @another_morph
@@ -72,6 +72,26 @@
attributes[:loose].should == {:way => 'but'}
end
+ it 'should call morph_attributes on both objects, when one object has a reference to array of others' do
+ @morph.every = 'which'
+ @another_morph.way = 'but'
+ @morph.loose = [@another_morph]
+
+ attributes = @morph.morph_attributes
+ attributes[:every].should == 'which'
+ attributes[:loose].should == [{:way => 'but'}]
+ end
+
+ it 'should call morph_attributes on both objects, when one object has a reference to hash of others' do
+ @morph.every = 'which'
+ @another_morph.way = 'but'
+ @morph.loose = { :honky_tonk => @another_morph}
+
+ attributes = @morph.morph_attributes
+ attributes[:every].should == 'which'
+ attributes[:loose].should == { :honky_tonk => {:way => 'but'} }
+ end
+
after :each do
remove_morph_methods
remove_another_morph_methods
Please sign in to comment.
Something went wrong with that request. Please try again.