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

Updates Readme sections for only and ignore #1247

Merged

Conversation

tlynam
Copy link
Contributor

@tlynam tlynam commented Jun 14, 2020

Thank you for your contribution!

Check the following boxes:

  • 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.

@tlynam
Copy link
Contributor Author

tlynam commented Jun 14, 2020

Hi @jaredbeck, hope you're well. It looks like ignore was only working for arrays. Ignore was working for hashes when calculating changed_and_not_ignored. Can we reuse this code for both cases? Once it's flushed out, I can add tests and update the changelog.

@jaredbeck
Copy link
Member

I couldn't find documentation re: hashes in the :ignore array. Thankfully, we do have tests in spec/models/article_spec.rb. Based on my review of those tests, and events/base.rb, it seems like #1240 is not a bug report, but rather, is proposing a new feature.

Currently, we allow the :ignore array to contain hashes. By comparison, #1240 proposes that :ignore be a hash. Therefore, #1240 proposes a new feature.

I'm hesitant to increase the complexity of our public API in this way.

@jaredbeck
Copy link
Member

Frankly, the current API, which allows the :ignore array to contain hashes, seems oddly complicated to me. I'd be open to simplifying it in our next major version. Perhaps :ignore should only accept an array of symbols, and a new option :ignore_if could take a Proc.

@tlynam
Copy link
Contributor Author

tlynam commented Jun 15, 2020

Thank you @jaredbeck! Can you please help me understand this part of the documentation? To me, it reads like ignore can be a hash?

The :ignore and :only options can also accept Hash arguments.

class Article < ActiveRecord::Base
  has_paper_trail only: { title: Proc.new { |obj| !obj.title.blank? } }
end

@jaredbeck
Copy link
Member

Ah, good find. But, looking at Events::Base#notably_changed, I think that documentation is incorrect?

def notably_changed
# Memoized to reduce memory usage
@notably_changed ||= begin
only = @record.paper_trail_options[:only].dup
# Remove Hash arguments and then evaluate whether the attributes (the
# keys of the hash) should also get pushed into the collection.
only.delete_if do |obj|
obj.is_a?(Hash) &&
obj.each { |attr, condition|
only << attr if condition.respond_to?(:call) && condition.call(@record)
}
end
only.empty? ? changed_and_not_ignored : (changed_and_not_ignored & only)
end
end

As with :ignore, I think that :only can contain hashes, but cannot be a hash.

So, now I'm not sure whether the docs are wrong or the implementation is wrong 😅 but I think they're different. 😓

@tlynam
Copy link
Contributor Author

tlynam commented Jun 16, 2020

Ah, thank you! I see that now. Okay, would the simplest route be to update the documentation for ignore describing that it only works with an array of symbols? I like your idea of separating ignore and ignore_if. Would ignore_if be a hash of procs?

@jaredbeck
Copy link
Member

.. would the simplest route be to update the documentation ..

Yeah, let's just update the docs for now.

.. [Should the docs] for ignore describing that it only works with an array of symbols?

Yes, but we should also give an example of an array of symbols that also contains a hash. See spec/dummy_app/app/models/article.rb. We may want to say that we are considering deprecating the later.

I like your idea of separating ignore and ignore_if. Would ignore_if be a hash of procs?

I was thinking it'd be a single proc. WDYT?

@tlynam
Copy link
Contributor Author

tlynam commented Jun 18, 2020

Ah, thank you, I was confused by what you meant by ignore_if.

Okay, so the bug report is mentioning that a hash that's inside of an array isn't working as expected for ignore. It looks like it responds to a hash within an array for changed_and_not_ignored and was trying to extract and use this same logic for ignored_attr_has_changed? as well. It looks like ignored_attr_has_changed? may just work with an array without hashes. Do you see the same thing? Is it possible there isn't an existing test for ignore with a hash within an array?

@jaredbeck
Copy link
Member

[is there] an existing test for ignore with a hash within an array?

Yes, spec/models/article_spec.rb.

I'd suggest that we focus, for now, on adding examples of current behavior to the docs (section 2.c.) and hold off on discussing any changes to the code until that's been done.

@tlynam tlynam force-pushed the bugfix/ignore-when-using-proc branch from 624d029 to dd30caa Compare July 27, 2020 06:09
README.md Outdated
end
```

If an attribute is ignored and only that attribute was updated, then a version won't be created for `updated_at`.
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 didn't see that we document this. Is it located somewhere else?

@tlynam tlynam force-pushed the bugfix/ignore-when-using-proc branch 2 times, most recently from f18213b to 8e74a18 Compare July 27, 2020 06:52
@tlynam
Copy link
Contributor Author

tlynam commented Jul 27, 2020

Hi @jaredbeck, I hope you're well.

  1. Okay, what do you think of these Readme updates saying we support ignore with an array of symbols and hashes with an example?
  2. Do we need to document that we don't save a version row for timestamp attributes if the only updated column is ignored?
  3. I believe I was able to reproduce the reported bug as a failing test in spec/models/gadget_spec.rb. It's saving a version record for updated_at when the updated column is ignored via Hash. This doesn't occur when the column is ignored via symbol. I believe the changes in lib/paper_trail/events/base.rb address it. I think changed_notably? would evaluate ignored_attr_has_changed? as false when it should be true and so it would create a version row for updated_at.
  4. If you see this as a bug, should we address it?

README.md Show resolved Hide resolved
README.md Outdated
end
```

If an attribute is ignored and only that attribute was updated, then a version won't be created for timestamp attributes.
Copy link
Member

@jaredbeck jaredbeck Jul 27, 2020

Choose a reason for hiding this comment

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

I may be misunderstanding, but I think this one line has already been said, above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, I missed that 👍

@jaredbeck
Copy link
Member

Do we need to document that we don't save a version row for timestamp attributes if the only updated column is ignored?

Sure, but can you make it a separate PR please?

I believe I was able to reproduce the reported bug as a failing test in spec/models/gadget_spec.rb. It's saving a version record for updated_at when the updated column is ignored via Hash. This doesn't occur when the column is ignored via symbol. I believe the changes in lib/paper_trail/events/base.rb address it. I think changed_notably? would evaluate ignored_attr_has_changed? as false when it should be true and so it would create a version row for updated_at.
If you see this as a bug, should we address it?

I may be missing something, but I don't see the need to make any code changes. For now, I'm satisfied with correcting the documentation.

- Updates ignore section to include an example with a Hash containing a proc.
- Fixes only section example to use an array as the argument.
@tlynam tlynam force-pushed the bugfix/ignore-when-using-proc branch from 87f46d1 to fe591c1 Compare August 3, 2020 03:25
@tlynam tlynam marked this pull request as ready for review August 3, 2020 03:33
@tlynam tlynam changed the title Fix ignore attributes when defined as hash Updates Readme sections for only and ignore Aug 3, 2020
@tlynam
Copy link
Contributor Author

tlynam commented Aug 3, 2020

Thank you @jaredbeck, this PR now just updates the Readme. I created a new PR with a failing test #1256

I also created a PR to fix CI and set the Rubocop gem to a fixed minor version so CI won't install a newer version that starts erroring in CI. It also updates the Rubocop gems. #1254

@tlynam tlynam merged commit cccbe5a into paper-trail-gem:master Aug 9, 2020
@tlynam tlynam deleted the bugfix/ignore-when-using-proc branch August 9, 2020 02:32
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.

None yet

2 participants