amoeba-rb/amoeba

Applying different amoeba configurations for different use cases

Opened this issue · 5 comments

Hi guys,

I have a small challenge. I'd like to apply different configs for amoeba, when I have different cases of cloning my objects.

Let assume that I have class Job with fields: name, description. The thing is that in one case I'd like to clone name & description and in the other case, I'd like to skip cloning description.

The other example can be, that I have a structure: MilestoneTemplate, TaskTemplate. Milestone template has many task templates. For one case I cast objects into the other classes: Milestone & Task and in the other case, I'd like to keep the original classes.

I'm not sure how to handle these cases - conditionally applying logic for cloning across multiple classes. Most likely I'll write custom thing for that. I'd really like to use / extend amoeba for this case. Maybe it could be solved by e.g. using refinements and overriding method for cloning?

@simon2k you need to use customize for this. And inside this you can define your logic.

pdobb commented

I agree with @simon2k. One might still want to apply e.g. a different "through" or "remapper" configuration for different situations. As far as I can tell that's not possible in the customize lambdas since that code appears to be run after applying the clones and associations.

pirj commented

I have the same issue and was also thinking refinements, but it did not work out of the box.

pirj commented

This is another option that will work with no changes to amoeba.

I have a similar task where teachers can create an availability schedule. To make it easier for them, they can add a time to the calendar that they are available in a 1 hour block. Then I created a dup method that allows them to click on an icon next to that schedule item and it will create another one for the next hour on the same day. That way they don't have to keep using the date, time and type pickers to add each new schedule time. The only fields I'm changing is the start and end times but no other fields.

I use Customize like so in the controller not the model:

orig_schedule = Schedule.find(params[:id])
orig_schedule.class.amoeba do
  customize(lambda { |original_object,new_object|
   new_object.start = original_object.start + 1.hour
   new_object.end = original_object.end + 1.hour
 })
end
@schedule = orig_schedule.amoeba_dup

Now I'm working on trying to create another method that would allow them to pick a start date and time then a number from 1-8 and amoeba will create new records (from 1-8 new records) for each hour starting from the start date and time. So far, it's been a tricky affair for me and I haven't gotten it to work yet. I'd like teachers to be able to quickly fill in their schedules with 1 hour time blocks for the day and also a week at a time too.