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

No need to calculate previous values of skipped attributes #1184

Merged
merged 1 commit into from
Feb 4, 2019

Conversation

khiav223577
Copy link
Contributor

@khiav223577 khiav223577 commented Jan 29, 2019

I'm going to remove a column of one model. I want to make sure that no one calls it anymore before removing it, so I overwritted it as below:

def my_column
  fail 'should not be called'
end

But paper_trail will call it even the attribute is skipped.
It calculates all attributes and then filters out skipped attributes.

def object_attrs_for_paper_trail(is_touch)
attrs = attributes_before_change(is_touch).
except(*@record.paper_trail_options[:skip])

The flow can be improved to calculate only non-skipped attributes.

  • Wrote good commit messages.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new
    code introduces user-observable changes.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.

Copy link
Member

@jaredbeck jaredbeck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Rumble, thanks for the contribution. It looks like a good start.

Please add at least one test in spec/paper_trail/events. You may want to use spec/dummy_app/app/models/skipper.rb. Feel free to add your fail to that class.

Please add some comments, at least in events/base.rb, explaining the importance of avoiding calling skipped attributes.

Thanks!

@@ -59,8 +59,11 @@ def attribute_changed_in_latest_version?(attr_name)
end

# @api private
def attributes_before_change(is_touch)
Hash[@record.attributes.map do |k, v|
def attributes_before_change(is_touch, skip: [])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the purpose of the skip argument. This method is only called from one place, and we always want to skip, so what about renaming this method to nonskipped_attributes_before_change, and having it always respect skip?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was no sure if it is ok to change the behavior of attributes_before_change method or not, so I added an extra argument to control it.

Renaming this method sounds to be a good idea, too. 👍

@jaredbeck jaredbeck merged commit e255e71 into paper-trail-gem:master Feb 4, 2019
@jaredbeck
Copy link
Member

Thanks, Rumble.

jaredbeck added a commit that referenced this pull request Feb 6, 2019
jaredbeck added a commit that referenced this pull request Feb 6, 2019
aried3r pushed a commit to aried3r/paper_trail that referenced this pull request Dec 14, 2020
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