moiristo / deep_cloneable

This gem gives every ActiveRecord::Base object the possibility to do a deep clone that includes user specified associations.
MIT License
786 stars 88 forks source link

Test / Documentation for cloning associations with conditionals #48

Closed johnnaegle closed 9 years ago

johnnaegle commented 9 years ago

It took me a long time to figure out the syntax for cloning associations with conditionals, so I wrote a test. Previously, I was using the array syntax for associations, but adding conditions required to switching to a hash syntax and I could not figure out what the right hand side of the hash. An empty array worked and I wrote a test for it.

Previously, I was cloning like this:

@ship.deep_clone(:include => [:pirates => [:treasures, :mateys]])

But then I needed conditionals on the pirates so the pirates associations array becomes a hash like this:

@ship.deep_clone(:include => [:pirates => {:treasures => [], :mateys => []}])

and then the conditional can be added.

This test really just documents how you add conditionals to array based cloning.

moiristo commented 9 years ago

Thanks! However, I think that a more straightforward way should also be supported, agreed? For example:

@ship.deep_clone(:include => [:pirates => [:treasures, :mateys, :unless => lambda {|pirate| pirate.name == 'Jack Sparrow'}]])

I think I can easily add this if you like..

johnnaegle commented 9 years ago

Yes, that would be cleaner

On Thu, Apr 2, 2015 at 3:13 AM, Reinier de Lange notifications@github.com wrote:

Thanks! However, I think that a more straightforward way should also be supported, agreed? For example:

@ship.deep_clone(:include => [:pirates => [:treasures, :mateys, :unless => lambda {|pirate| pirate.name == 'Jack Sparrow'}]])

I think I can easily add this if you like..

— Reply to this email directly or view it on GitHub https://github.com/moiristo/deep_cloneable/pull/48#issuecomment-88816517 .

moiristo commented 9 years ago

Added in master.