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

How to list all versions of a record? #212

Open
dchacke opened this issue Jul 8, 2022 · 4 comments
Open

How to list all versions of a record? #212

dchacke opened this issue Jul 8, 2022 · 4 comments

Comments

@dchacke
Copy link

dchacke commented Jul 8, 2022

I'm new to this gem. Perusing the Readme, I couldn't find a method to retrieve all versions of a record.

I suppose I could just do:

[1..record.log_size].map { |v| record.at(version: v) }

But if there's a built-in way to do this, that would be even easier.

@palkan
Copy link
Owner

palkan commented Jul 13, 2022

Nope, there is no API for that.

Do you have an idea how it could look like? And what is your use case?

@palkan palkan added enhancement awaiting response Awaiting reporter's response labels Jul 13, 2022
@dchacke
Copy link
Author

dchacke commented Jul 13, 2022

Something like post.versions would be fine. It could return an array with the original as the first element and then all subsequent versions in the order they were created.

I host a blog and need to list all revisions to a post with word-based diffs.

@palkan
Copy link
Owner

palkan commented Jul 13, 2022

It could return an array with the original as the first element and then all subsequent versions in the order they were created

That's an interesting question whether the current version should be included or not 🤔

And I'm thinking of having an enumerator instead of returning an Array right away (since created many records could affect performance). Something like this:

post.versions #=> Enumerator

# you can use take to return all
post.versions.take

# or you take a few or call any Enumerable method
post.versions.take(2)

post.versions.find do
  _1.title == "old title"
end

# we can also add options
post.versions(reverse: true) # from older to newer
post.versions(include_self: true) # return self as the first one (default) or the last one record (if reverse: true)

And we should either use a less common name, say, post.logidze_versions or make this feature disabled by default and allow to configure the name. For example:

class Post < ApplicationRecord
  # add #versions method
  has_logidze versions_accessor: true

  # use a custom name
  has_logidze versions_accessor: :log_versions
end

WDYT? Am I missing something?

@dchacke
Copy link
Author

dchacke commented Jul 13, 2022

That all sounds good to me. I vote calling it logidze_versions instead of disabling it by default. You can always just do

def arbitrary_versions_attr
  logidze_versions
end

and so no custom syntax is required.

@palkan palkan removed the awaiting response Awaiting reporter's response label Jul 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants