moiristo / deep_cloneable

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

Optional Block Handling with 3.0.0 #113

Closed c2ofh closed 4 years ago

c2ofh commented 5 years ago

Hi,

I'm using this gem to manage a a copy of a huge model with 41 associations. Each association got also associations.

While the the copy is running, I need access to the new copy, to fix some wrong associations, or to disable a check, which reduce the time to copy about 50% or to manage the file database.

Now I don't know how to handle this. When I have to do this afterwards in any way, I do have to safe all copies, with it's original into an array. This will not work, cause the end callback will just return the copy of the main object.

What is the best way to modify copies with information from the newly copied main object?

moiristo commented 5 years ago

Hi,

I think the optional block is exactly what you're looking for? It is called for every copy of an object that is made during the process (not only the initial object).

c2ofh commented 5 years ago

Hello @moiristo yes, you're right. But the problem is now, that the copied object, is now the last and not the first element. So I'm asking me how to handle that stuff now.

Maybe someone knows a workaround.

moiristo commented 5 years ago

Ah, I see. Seems that some people would expect the block to run after all processing and some expect it to run before all processing :) I'm thinking about a new option to resolve this, maybe something like record.deep_clone(include: :things, preprocessor: ->(record, kopy){ ... })?

c2ofh commented 5 years ago

Hello @moiristo This would help everyone :) Each case, if needed before or afterwards could be accomplished with that method.

moiristo commented 5 years ago

Alright, I'll see what I can do. If you're in a hurry.. I accept PR's as well ;) Related to #81.

c2ofh commented 5 years ago

@moiristo I'm not in hurry, I've locked the gem at 2.4.0 If I have time, I'm trying to contribute :)

c2ofh commented 4 years ago

@moiristo I've tried the change with 3.0.0, but the copy of my_object is still not the first item of the copy process. Am I doing something wrong?

my_object..deep_clone include: [:journeys,
                                :languages,
                                ...
                                :months,
                                :push_categories,
                                :widgets,
                                :restaurants, { restaurants: [:timetable] }], 
                      use_dictionary: true, preprocessor: true do |original, copy|
moiristo commented 4 years ago

Hi, sorry for the late response, I was on a holiday 😎 The preprocessor/postprocessor functionality is currently in master. It works a little bit different than you think, the idea is to pass a lambda to the preprocessor argument:

my_object..deep_clone include: [:journeys,
                                :languages,
                                :restaurants, { restaurants: [:timetable] }], 
                      use_dictionary: true, preprocessor: ->(original, kopy) { ... }