Skip to content

Add PaperTrail.config.association_reify_error_behaviour#1091

Merged
jaredbeck merged 6 commits intopaper-trail-gem:masterfrom
westonganger:add_association_reify_error_behaviour_option
May 23, 2018
Merged

Add PaperTrail.config.association_reify_error_behaviour#1091
jaredbeck merged 6 commits intopaper-trail-gem:masterfrom
westonganger:add_association_reify_error_behaviour_option

Conversation

@westonganger
Copy link
Copy Markdown
Contributor

Fixes #1090

The added tests do pass when configured appropriately in the dummy app initializer before running the tests. However I am not sure exactly how to dynamically test this option so I have commented out the tests, please advise.

Comment thread README.md Outdated
`PaperTrail::Reifiers::HasOne::FoundMoreThanOne` error)
1. Sometimes the has_one association will find more than one possible candidate and will raise a `PaperTrail::Reifiers::HasOne::FoundMoreThanOne` error. For example, see `spec/models/person_spec.rb`
- If you are not using STI, you may want to just assume the first result (of multiple) is the correct one and continue. Versions pre v8.1.2 and below did this without error or warning. To do so add the following line to your initializer: `PaperTrail.config.association_reify_error_behaviour = :warn`. Valid options are: `[:error, :warn, :ignore]`
- When using STI, even if you enable :warn you will likely still end up recieving an `ActiveRecord::AssociationTypeMismatch` error in which case your SOL
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Try to avoid idioms in documentation. "SOL" is an expression that only native English speakers will understand. Perhaps instead we should recommend the :error option for STI users?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Im not going to recommend the :error option to STI users in case they are also struggling with the non-STI version of this problem. Beside the :error option is already the default.

"PaperTrail.config.track_associations = true\n",
"PaperTrail.config.association_reify_error_behaviour = :error"
)
end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This change seems unrelated to this PR. Please advise.

version = versions.first
version.logger&.warn(
format(FoundMoreThanOne::MESSAGE_FMT, base_class_name, versions.length)
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice idea to use version.logger.

I think I'd prefer:

version.logger&.warn(
  FoundMoreThanOne.new(base_class_name, versions.length).message
)

What do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Meh. I'd prefer not to because that creates an extra error object for no reason.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Meh. I'd prefer not to because that creates an extra error object for no reason.

The reason is to avoid duplicating the implementation of FoundMoreThanOne#message. By duplicating the implementation, if the message changes, you have to change it in two places.

.. that creates an extra error object ..

For me, the duplication of code seems worse than a few object allocations, but that's not based on any benchmark of course, just a gut instinct.

Comment thread spec/models/person_spec.rb Outdated
# )

# person.reload.versions.second.reify(has_one: true)
# end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you're having trouble writing this type of test, I'd be happy with testing load_version in isolation, even using mocks. I'd be happier with a test that used real database records, but for now an isolated test is OK with me.

"config/initializers/paper_trail.rb",
"PaperTrail.config.track_associations = #{!!options.with_associations?}\n"
"PaperTrail.config.track_associations = true\n",
"PaperTrail.config.association_reify_error_behaviour = :error"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice thinking to include this in the generator

)
end
end
end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like how you put these specs in different files.

Can we put them in spec/paper_trail/association_error_behaviour though? Because, spec/models is intended to correlate to app/models.

Comment thread spec/paper_trail/config_spec.rb Outdated
# config.association_reify_error_behaviour = :warn
# expect(config.association_reify_error_behaviour).to eq(:warn)
# end
# end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Given the other specs you've added, it looks like we can delete this commented-out spec?

@jaredbeck jaredbeck merged commit d056c7e into paper-trail-gem:master May 23, 2018
@jaredbeck
Copy link
Copy Markdown
Member

Thanks Weston, I'd like to tidy a few very minor things up (eg. changelog format, nothing major) and then I'll cut a release.

@jaredbeck
Copy link
Copy Markdown
Member

Released in 9.1.0

jkeck added a commit to jkeck/paper_trail that referenced this pull request May 30, 2018
...initializer.

It appears that paper-trail-gem#1091 introduced this unintentionally by moving the initializer generator into a conditional in paper-trail-gem@f9a8a77#diff-385d0586e82e9b33429c177d016048af but when it was finally merged in under d056c7e that conditional was removed but the interpolated `with_associations?` option was never added back.

This means that is you use the default behavior of not using the with-associations option when running the install generator, you still get `PaperTrail.config.track_associations = true` in your generated initializer even though none of the other migration files, etc are generated so `Could not find table 'version_associations'` is raised when saving any model versioned through paper trail.
aried3r pushed a commit to aried3r/paper_trail that referenced this pull request Dec 14, 2020
…gem#1091)

* add PaperTrail.config.association_reify_error_behaviour

* fix test for Rails 4.2

* PaperTrail.config.association_reify_error_behaviour test in isolation, fixes

* association_error_behaviour fix specs

* fix rubocop errors

* assoication_reify_error_behaviour fixes
aried3r pushed a commit to aried3r/paper_trail that referenced this pull request Dec 14, 2020
...initializer.

It appears that paper-trail-gem#1091 introduced this unintentionally by moving the initializer generator into a conditional in paper-trail-gem@f9a8a77#diff-385d0586e82e9b33429c177d016048af but when it was finally merged in under d056c7e that conditional was removed but the interpolated `with_associations?` option was never added back.

This means that is you use the default behavior of not using the with-associations option when running the install generator, you still get `PaperTrail.config.track_associations = true` in your generated initializer even though none of the other migration files, etc are generated so `Could not find table 'version_associations'` is raised when saving any model versioned through paper trail.
hosamaly added a commit to hosamaly/paper_trail that referenced this pull request Feb 21, 2021
v9.2.0

* tag 'v9.2.0': (23 commits)
  Release 9.2.0
  Tests: Fix deprecation: represent_boolean_as_integer
  Tests: Fix deprecation warning re: secret_key_base
  Lint: working on RSpec/InstanceVariable in model_spec.rb
  Docs: Clarify that VersionAssociation is in separate gem
  Update paper_trail-association_tracking version dependency
  First test of PT-AT gem
  Docs: association tracking, other plugins
  Extract gem: paper_trail-association_tracking
  Introduce config.object_changes_adapter
  Automate test setup
  Release 9.1.1
  Use with-associations option for the generated track_associations...
  Docs: Break long line
  Delete spec callback "verify_stubs: false" ..
  Delete the "test" task, we only use rspec
  Docs: issue templates
  Release 9.1.0
  Add PaperTrail.config.association_reify_error_behaviour (paper-trail-gem#1091)
  Fix intermittently failing test
  ...
hosamaly added a commit to hosamaly/paper_trail that referenced this pull request Feb 21, 2021
v9.1.1

* tag 'v9.1.1':
  Release 9.1.1
  Use with-associations option for the generated track_associations...
  Docs: Break long line
  Delete spec callback "verify_stubs: false" ..
  Delete the "test" task, we only use rspec
  Docs: issue templates
  Release 9.1.0
  Add PaperTrail.config.association_reify_error_behaviour (paper-trail-gem#1091)
  Fix intermittently failing test
  Uninstall timecop
  rubocop-rspec 1.25.1 (was 1.24.0)
  rubocop 0.56.0 (was 0.54.0)
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