Skip to content
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

Set Callbacks explicitely instead of using has_papertrail #607

Closed
wants to merge 24 commits into from
Closed

Conversation

Ninigi
Copy link

@Ninigi Ninigi commented Sep 3, 2015

Refering to this 3rd-party issue I wrote some methods to set callbacks:

  paper_trail_update
  paper_trail_create
  paper_trail_destroy # can take arguments :before or :after, defaults to :after if no argument given

paper_trail_destroy(:after/:before) can be aliased as paper_trail_after_destroy/paper_trail_before_destroy.

There is a caveat when passing options:

  paper_trail_update :ignore => [:things]
  paper_trail_create :ignore => [:other_things]

will NOT result in paper_trail ignoring changes to :things and :other_things, but only :things, since the "initialization" will only be triggered once.

Also when using has_paper_trail will override the explicitely set callbacks:

  paper_trail_update :ignore => [:things]
  has_paper_trail

will result in all events to be tracked and the ignore option will not be set at all...

Not sure how to fix those issues yet, because I did not want to get rid of has_paper_trail and/or break working functions...

@jaredbeck
Copy link
Member

Wow, this is great! I haven't reviewed your implementation yet, but I think this is a great idea, giving users more control over their ActiveRecord callbacks. Specifically, being able to control the order in which callbacks happen will solve a whole class of issues, especially integration with other libraries that have their own callbacks.

As you mentioned, this has been discussed before:

Precedent in other libraries

Other popular ruby libraries give users control over callbacks, so there is a precedent for this. For example load_and_authorize_resource in CanCan[Can], or protect_from_forgery in ActionController.

Other libraries go so far as to only provide methods, and require users to set up callbacks manually. For example verify_authorized in Pundit.

Next steps for this PR

This is a big change, so it's going to take Ben and I a while to review. In the meantime, a few housekeeping requests:

  • Please add a short changelog entry
  • Use git rebase to squash the "fixed typo" and "merged master" commits

@Ninigi
Copy link
Author

Ninigi commented Sep 3, 2015

Oh, I didn't think this would strike you as a big change, so I was a little sloppy about some things... For example I moved the InstanceMethod module to its own file, because I felt pretty bugged by that huge has_paper_trail.rb file. I should have moved that into another PR now that I think about it.
I left all those commits in the PR too, because I did not think it would be a big deal again, but I'll do it right away ^^

But really don't expect too much of this, I just rewrote the has_paper_trail method to use the paper_trail_update(etc) methods etc and added an option to the destroy method... The other methods have been moved, but not changed, but it would not be hard to implement the option for them too.
So I basically just split the has_paper_trail method and made the method an alias for using the callback methods one after the other.

Could you give me some kind of guideline how to change the changelog? I didn't change it because I was under the impression that's what you guys want to do.

@jaredbeck
Copy link
Member

I moved the InstanceMethod module to its own file, because I felt pretty bugged by that huge has_paper_trail.rb file. I should have moved that into another PR now that I think about it.

Yeah, if you could split that refactoring into a second PR, that would really help use review this one more quickly. Thanks!

@jaredbeck
Copy link
Member

Could you give me some kind of guideline how to change the changelog?

Just follow the conventions you see in there. Further reading: http://keepachangelog.com/

@Ninigi
Copy link
Author

Ninigi commented Sep 3, 2015

ok... I have no idea how to do that. Should I just revert those changes, make another branche and make another PR? Or can I use some git magic? I really don't know...

@jaredbeck
Copy link
Member

I have no idea how to do that

No idea how to do what? Split the refactoring into a second PR?

@Ninigi
Copy link
Author

Ninigi commented Sep 5, 2015

ok, give me another day :) I have been sick for a few days, but I think I will be able to tackle this tomorrow.

zitterf added 3 commits September 7, 2015 09:17
Docs: Contributing guide, initial revision

[ci skip]

fixed typo in readme
@Ninigi
Copy link
Author

Ninigi commented Sep 8, 2015

ok, I've squashed the commits, I am not really sure if it's worth it to revert the 'moved instance methods to its own file' commit and move it to another PR now that I've thought about it tbh, but of course I'll still do it if you prefer.
I was thinking about adding a whole bunch of tests though, to make sure options work with callback-methods, but I'd like to hear from you before I start to put more work into something that will get denied in the end :)

@jaredbeck
Copy link
Member

I am not really sure if it's worth it to revert the 'moved instance methods to its own file' commit and move it to another PR now that I've thought about it tbh, but of course I'll still do it if you prefer.

Please do, thanks. We have very limited time to review PRs/issues, and the smaller this PR is, the easier it will be for us to review. Thanks.

Control the order of `set_paper_trail_whodunnit` callback
.. to reflect that master now represents 5.0

[ci skip]
We would prefer to only constrain mysql2 to '~> 0.3',
but a rails bug (rails/rails#21544)
requires us to constrain to '~> 0.3.20' for now.
@jaredbeck
Copy link
Member

Please remove any unnecessary refactoring, rebase on master, and squash everything into a single commit. After you've done that, we'll be able to review this PR. Thanks!

zitterf and others added 5 commits September 9, 2015 20:41
[ci skip]

Control order of `set_paper_trail_whodunnit` callback

Fixes #301

Stop automatically adding the `set_paper_trail_whodunnit`
before_filter.  This gives people control over the order of this
callback.

Docs: Update compat. table

.. to reflect that master now represents 5.0

[ci skip]

Temporarily constrain mysql2 gem to ~> 0.3.20

We would prefer to only constrain mysql2 to '~> 0.3',
but a rails bug (rails/rails#21544)
requires us to constrain to '~> 0.3.20' for now.

Also constrain mysql2 in the rails 3.0 gemfile

See previous commit.

updated readme to include added callbacks

add explicit callbacks

added rspec tests for callbacks

added explicit callbacks

moved instance methods to its own file

added a setup_model function

Docs: Contributing guide, initial revision

[ci skip]

fixed typo in readme

removed redundant include

update changeleog added callback-methods

add explicit callbacks

added rspec tests for callbacks

added explicit callbacks

moved instance methods to its own file

added a setup_model function

Docs: Contributing guide, initial revision

[ci skip]

fixed typo in readme

removed redundant include

update changeleog added callback-methods

revoked unnecessary refactoring
@jaredbeck
Copy link
Member

Fabian, we can close this PR, right? It is superseded by #614

@jaredbeck jaredbeck closed this Sep 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants