Add option to pass block to controller's #save! #73
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi @pond, it's me again.
This time I extended
ActiveRecordBackedResourcesController#save!
to accept a block, so that subclasses may override the defaultrecord.save!
call with their own saving logic, without having to reimplement the rescue clause for handling uniqueness errors. Also, it aligns nicely with the destroy action, which already accepts a block for a very similar reasons.In our case we would like a service object to save the record and do some other stuff, because we do not want to employ AR callbacks for that other stuff. With these changes it can look like this:
This will be helpful in many situations. But sometimes it's not enough.
We are trying to make things work with an ActiveModel model that wraps an ActiveRecord record. It mostly delegates stuff to the wrapped record, but it has its own additional validations. That means that we'll need to rescue
ActiveModel::ValidationError
next toActiveRecord::InvalidRecord
. Therefore we need to override the rescue clause in #save!, but we'd still like to avoid copy-pasting all of the invalid record handling logic. Therefore I have extracted that logic to a separate method. This way we only need to copy-paste one method call, and we don't have to override the actual logic:In short, both changes just add a bit more extensibility to the controller.
I would love to see this merged in one form or another. Thanks!