-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarify its use within models #16
Comments
For using it in models instance methods, you can just include this gem's main module into your model (or right into class ApplicationRecord < ActiveRecord::Base
include AfterCommitEverywhere
end And that's it! For class methods you can't extend it into model as it will effectively replace existing For now you can monkey-patch gem itself in such a way: module AfterCommitEverywhere
module_function :after_commit, :before_commit, :after_rollback
end And then you will be able to call its methods directly: AfterCommitEverywhere.after_commit { puts "Done!" } May be it worth it to add support for these direct calls to gem API (however, But my main concern for your examples is that model layer isn't appropriate place for stuff like this. It is much better to have separate classes that implements your business logic along with all callbacks. They can be called “actions”, “operations”, or “services”. |
Thanks for the suggestions.
I'm working on an large, existing app to replace delayed_job with sidekiq. The code I'm on could use more refactoring, but I'm trying to limit the scope of the changes I'm making. Trying to not let "perfect" get in the way of "better". |
Does this gem not work within models or is it just generally not recommended because you can use AR's built-in stuff instead?
For instance-level stuff, I'm thinking of doing something like this (simplified):
Is there any problem with this? It could be rewritten to use Rails's
after_commit
but then that would have to inspect for changes toactivated_at
which just seems to make it more complicated.How about in class methods in models? I've seen #13, so I know using
after_commit
does not do what might be expected here. But is there a way to use it properly?The text was updated successfully, but these errors were encountered: