Add PaperTrail.config.association_reify_error_behaviour#1091
Conversation
| `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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
This change seems unrelated to this PR. Please advise.
| version = versions.first | ||
| version.logger&.warn( | ||
| format(FoundMoreThanOne::MESSAGE_FMT, base_class_name, versions.length) | ||
| ) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Meh. I'd prefer not to because that creates an extra error object for no reason.
There was a problem hiding this comment.
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.
| # ) | ||
|
|
||
| # person.reload.versions.second.reify(has_one: true) | ||
| # end |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
Nice thinking to include this in the generator
| ) | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
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.
| # config.association_reify_error_behaviour = :warn | ||
| # expect(config.association_reify_error_behaviour).to eq(:warn) | ||
| # end | ||
| # end |
There was a problem hiding this comment.
Given the other specs you've added, it looks like we can delete this commented-out spec?
|
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. |
|
Released in 9.1.0 |
...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.
…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
...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.
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 ...
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)
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.