-
Notifications
You must be signed in to change notification settings - Fork 899
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
Enable control over the order of AR callbacks #614
Conversation
and messed it up again... sigh It's too late today and this is becoming quite frustrating... Guess I'll have to look into it tomorrow once again. |
:( take a break. When you come back, you'll get it 👍 |
OK I think this should be reviewable now... I left everything as it was wherever possible, so the changes should be obvious now. On a side-note, there was a case when a single test would fail on Travis-CI. I looked through the code and just couldn't find what could have caused it (was an "undefined method for nil-class" error), when I couldn't find anything I just reran the build and it passed without me changing anything. Any ideas where that came from? |
Thank you for "minimizing" this PR by moving that other refactoring to a separate PR. A few things I like:
A few things I don't like:
|
Thanks for the feedback (I was really looking forward to this)
To be honest, I did not really like this either, I did it to work around the problem of breaking the original has_paper_trail way of doing it
No problem, I just added this to have a more explicit way of adding the callback instead of adding another "do this + option". If you don't like it, scratch it :)
Love and hate for this Hate: I tried to do it without adding a state, but I found myself with either clattering the models with reduntant stuff, or missing some of the important setups. Since I felt like a complete idiot with the last PR and the way it went (it seems everyone but me knows how to easily sqash commits the way he wants), for the changes you want, should I do a new PR, update this one with a commit or do something else? *What your suggestions would solve: *
Problems your suggestions imply:
class Thingy < ActiveRecord::Base
has_paper_trail :ignore => [:specific_thing]
paper_trail_create :ignore => :another_thing
paper_trail_update
end or should callbacks not take any options at all? If they should, should they apply to the event they track? class Thingy < ActiveRecord::Base
has_paper_trail
paper_trail_create :ignore => :thing
paper_trail_update :ignore => :some_other_thing
end
# This creates the first version, should it ever ignore anything, if set in the options?
thingy = Thingy.create(:thing => "A Swallow", :some_other_thing > "original other thing")
# This creates the update version... No discussion about what we can or can not skip, because it should be up to the consumer.
thingy.update_attributes(:some_other_thing => "rambazamba")
# It leads to some confusion
thingy.versions.first.thing # should it say "A Swallow" or nil?
thingy.versions.last.some_other_thing # should it say "rambazamba" or "original other thing" |
Good question. Yes, I think the easiest thing for now would be to keep all of the options where they are, passed to
Doesn't matter, whatever is easiest for you. |
OK, I am having a hard time trying to keep the traditional way of using has_paper_trail and the "call it explicitely" way... No matter how you look at it, there is no way around introducing some kind of "hack" to not break older versions. |
Yeah, that sounds tough. What about something like: has_paper_trail on: []
paper_trail_create
paper_trail_update
paper_trail_destroy I'm assuming that passing |
Also, what do you think about naming the methods:
Adding the word "on" makes it sound more like a callback, and it's not much more typing. |
I like that. Actually my very first shot at this was with methods named "record_create" etc until I realised there methods with those names already in use internally ;)
Yes it would, but it would make the usage a bit awkward, don't you think? I will put a little more time into trying to find a way to make the traditional has_paper_trail work with this. If I can't make it work, I'll do it with the empty :on option and maybe tackle this problem in another iteration. Thanks for your feedback. |
e27b488
to
9008d67
Compare
Ok, so the way I did it will set all callbacks on The way it would work now is class ModelTrackingCreateEvent < ActiveRecord::Base
# Will track create only
has_paper_trail :ignore => ['something'], ...
paper_trail_on_create # no options possible
end
class ModelTrackingAllEvents
has_paper_trail :ignore => ['something'], ...
end |
I don't like the complexity that this introduces. I'm OK with a slightly harder-to-use API if it makes the library a lot easier to maintain. |
@@ -8,6 +9,7 @@ def self.included(base) | |||
end | |||
|
|||
module ClassMethods | |||
include Callbacks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather keep the contents of Callbacks
here in ClassMethods
. I generally agree with Steve Klabnik's article "Mixins: A refactoring anti-pattern".
All right, I'll just admit defeat on the callback-chain thing ;) I have a way of keeping the library maintainable in mind, but I feel like I am trying to force this instead of accepting it is an unnecessary addition and just do it the easiest way, like you suggested. I moved the methods back into |
@@ -26,6 +26,9 @@ None | |||
|
|||
### Added | |||
|
|||
- Added callback-methods `paper_trail_update` `paper_trail_create` `paper_trail_destroy` | |||
instead of has_paper_trail | |||
[#593](https://github.com/airblade/paper_trail/pull/607) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Should read
paper_trail_on_update
. - PR number in link should be 614, right?
…per_trail_options[:on] when using callback-methods
Enable control over the order of AR callbacks
Looks good. Thanks Fabian! This will be a big help to people who need to control the order of callbacks. |
Ok, this is embarrassing, but it was actually easier for me to delete the fork and redo my changes than trying to get a grip on how to rework all those commits in this PR ... I left the PR as a reference, but I guess all you need to review is this.