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

Add original_version method to more easily obtain the original version. #1204

Closed
2 tasks done
joshuapinter opened this issue May 22, 2019 · 12 comments · May be fixed by #1504
Closed
2 tasks done

Add original_version method to more easily obtain the original version. #1204

joshuapinter opened this issue May 22, 2019 · 12 comments · May be fixed by #1504

Comments

@joshuapinter
Copy link

  • I am committed to building this feature myself
  • I will respond to feedback, and build this feature, in a reasonable amount
    of time

Is your feature suggestion related to a problem? Please describe.

Getting the original version of an item is way too difficult. It stems from the fact that calling reify on the first version returns nil and not the original object. That's fine. But it means that you need to do some fancy footwork to get the original version of an object and also handle when an object doesn't have any changes (i.e. only a single create version).

You can see the issue in these two spots:

Describe the solution you'd like to build

I want to add an original_version method to the has_paper_trail concern that will return the original version of an object, regardless of how many changes the object has.

So instead of writing something like this versions.second&.reify || self you can just write original_version.

Describe alternatives you've considered

versions.second&.reify || self

Commit on Our Fork.

We're using 3.0-stable branch so I forked that and committed our desired change there. No tests but we can certainly write them if you want this merged into master.

@jaredbeck
Copy link
Member

Hi Joshua, thanks for the contribution.

I want to add an original_version method to the has_paper_trail concern that will return the original version of an object, regardless of how many changes the object has.

Sounds like a useful method.

I want to add an [the new] method to the has_paper_trail concern ..

In PT 6, to avoid AR model namespace pollution, we moved almost everything from has_paper_trail.rb to lib/paper_trail/record_trail.rb. So, I'd suggest that this new method go in record_trail.rb also.

.. add an original_version method ..

The name is in keeping with other, similar methods: previous_version and next_version. (Which are, themselves, not a match for source_version, the former returning records while the later returns a Version) There may also be some slight confusion with the existing originator method (a name I've always thought too generic) We could call the new method reify_original_version to clarify that it returns a record rather than a version. Any other suggestions?

No tests but we can certainly write them ..

Thanks, tests are required for all new features.

.. if you want this merged into master.

Did you want it merged into a different branch in addition to master? We don't usually do backports, except for important stuff like security patches.

@joshuapinter
Copy link
Author

Thanks for the quick reply, @jaredbeck!

In PT 6, to avoid AR model namespace pollution, we moved almost everything from has_paper_trail.rb to lib/paper_trail/record_trail.rb. So, I'd suggest that this new method go in record_trail.rb also.

Makes sense, thanks. We're on the old version so once we update we'll make this change to record_trail.rb instead.

The name is in keeping with other, similar methods: previous_version and next_version. (Which are, themselves, not a match for source_version, the former returning records while the later returns a Version) There may also be some slight confusion with the existing originator method (a name I've always thought too generic) We could call the new method reify_original_version to clarify that it returns a record rather than a version. Any other suggestions?

Naming is always tough. :) I definitely see your point about the method not returning a Version instance. I didn't consider that at first. I think reify_original_version is on the right track and would certainly indicate the object is returned. Is reify_original not explicit enough? Let me know what you think.

Did you want it merged into a different branch in addition to master? We don't usually do backports, except for important stuff like security patches.

master is good. We'll update our app to use your latest version before submitting this PR so we can use it in production and verify all is working well.

Thanks!

@jaredbeck
Copy link
Member

Naming is always tough. :) .. reify_original ..?

reify_original sounds good to me. Let's go with that for now.

We'll update our app to [the] latest version ..

I strongly recommend updating one major version at a time. There have been many breaking changes over the years, all documented in the changelog, and some with runtime deprecation warnings. If you upgrade one major at a time, you can benefit from said warnings.

@joshuapinter
Copy link
Author

I strongly recommend updating one major version at a time. There have been many breaking changes over the years, all documented in the changelog, and some with runtime deprecation warnings. If you upgrade one major at a time, you can benefit from said warnings.

Couldn't agree more. As a policy, everything we do at CNTRAL is statically versioned. So no >= or ^ in Gemfile or package.json. And then when we upgrade we do it one version at a time and read through all of the Release notes and/or CHANGELOG entries.

Thanks again, will keep you posted! 👍

@jaredbeck
Copy link
Member

We'll update our app to use your latest version before submitting this PR .. will keep you posted!

Closing in anticipation of PR. Thanks, Joshua.

@joshuapinter
Copy link
Author

Oh noe. Sorry for the tardiness. Was just revisiting this.

@jaredbeck
Copy link
Member

Oh noe. Sorry for the tardiness. Was just revisiting this.

No worries at all. Take your time. I just figured the discussion was complete, so I could mark it off the list.

@cameronbourgeois
Copy link

+1 I think this feature would be handy.

@DanielStevenLewis
Copy link

Did something like this feature get added to PaperTrail? If not should the issue be re-opened?

@joshuapinter
Copy link
Author

@DanielStevenLewis Not as far as I'm aware. We're still using it in our fork but haven't found the time to submit a PR back upstream.

@duhaime
Copy link

duhaime commented Jan 19, 2024

@joshuapinter @jaredbeck can this be reopened? It seems super odd one can't reify the first version of an object, this should be possible

joshuapinter added a commit to joshuapinter/paper_trail that referenced this issue Jan 8, 2025
Returning the original version is a common use-case and is not as easy as it should be, particularly because calling `reify` on the first version returns `nil`, which is the `object` *before* it was created.

This method simply tries to `reify` from the *second* version, if it exists. If it doesn’t exist (i.e. there has not been any changes to the object), it falls back to `self`, which is the original and *only* version of the object.

Fixes paper-trail-gem#1204.
@joshuapinter
Copy link
Author

Just following up on this as I finally got around to making the minor mods and stubbing out a PR for it.

I'll wait to ensure there is still interest in adding this before adding tests. FYI, we're still using this in production and it's been working well for us.

Thanks.

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 a pull request may close this issue.

5 participants