bullet-train-pro / bullet_train-action_models

Other
5 stars 1 forks source link

Allow interface for `failed_ids` for erroring out objects on TargetsMany #97

Open BrandonKlotz opened 10 months ago

BrandonKlotz commented 10 months ago

Right now if I force an object to raise being evaluated in perform_on_target the failed_ids attribute will not be updated with the record id. I believe failed_ids is not working at all for ActionModels right now.

Here is an example for an ActionModel that TargetsMany to delete Contact records. I'd expect my callback to ensure the object fails processing and is added to failed_ids

Screenshot 2023-12-20 at 11 54 32 AM

For some reason it seems like failed_ids is nil when trying to << failed object ids into the attribute.

Screenshot 2023-12-19 at 4 47 06 PM

I tried to pull the gem locally and ensure failed_ids is an Array but this resulted in the ActionModel retrying to perform on the record over and over.

Screenshot 2023-12-20 at 12 03 25 PM

Ideally, in the DeleteAction, I'd be able to shovel and persist ids that didn't meet the requirement to be deleted, but this does not seem to be persisting on the ActionModel's failed_ids attribute.

Screenshot 2023-12-20 at 12 12 32 PM
gazayas commented 9 months ago

Hello @BrandonKlotz, sorry for the delay here.

Here is an example for an ActionModel that TargetsMany to delete Contact records. I'd expect my callback to ensure the object fails processing and is added to failed_ids

I see that the screenshot you posted after that has the callback in it. Are you putting the callback directly into the TargetsMany model? I was able to persist failed_ids with a setup like this:

> rails generate super_scaffold Project Team name:text_field
> rails generate super_scaffold:action_models:targets_many Archive Project Team

In app/models/project.rb:

before_destroy :raise_invalid_destroy, if: -> { true }
# 🚅 add callbacks above.

# 🚅 add delegations above.

def raise_invalid_destroy
  raise "Error"
end
# 🚅 add methods above.

In app/models/projects/archive_action.rb:

def perform_on_target(project)
  project.destroy!
end

This should be enough since we're raising an error specifically on perform_on_target.

https://github.com/bullet-train-pro/bullet_train-action_models/blob/49797fd4b680458c3612b07d3c0ec987a2b74d87/app/models/concerns/actions/targets_many.rb#L68-L76