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

Filter by changed attrs #247

Open
dixpac opened this issue Jun 22, 2024 · 2 comments
Open

Filter by changed attrs #247

dixpac opened this issue Jun 22, 2024 · 2 comments

Comments

@dixpac
Copy link

dixpac commented Jun 22, 2024

I have logidze on a model. That model tracks every attribute change.
I have a page that is visible to the user and on that page I want to show only versions where value column changed.

Here is the simplified example json:

{
  "h": [
    {
      "c": {
        "id": 202,
        "value": "",
        "aasm_state": "new",
        "created_at": "2024-06-22T12:08:26.450706",
        "updated_at": "2024-06-22T12:08:26.450706",
      },
      "m": {
        "_r": "gid://lab/User/19"
      },
      "v": 1,
      "ts": 1719058106451
    },
    {
      "c": {
        "id": 202,
        "value": 1,
        "aasm_state": "new",
        "created_at": "2024-06-22 12:08:26.450706",
        "updated_at": "2024-06-22 12:08:32.315247",
      },
      "m": {
        "_r": "gid://lab/User/19"
      },
      "v": 2,
      "ts": 1719058112315
    },
    {
      "c": {
        "aasm_state": "entered"
      },
      "m": {
        "_r": "gid://lab/User/19"
      },
      "v": 3,
      "ts": 1719058112339
    },
    {
      "c": {
        "id": 202,
        "value": 2,
        "aasm_state": "entered",
        "created_at": "2024-06-22 12:08:26.450706",
        "updated_at": "2024-06-22 12:08:34.300976",
      "m": {
        "_r": "gid://lab/User/19"
      },
      "v": 4,
      "ts": 1719058114301
    }
  ],
  "v": 4
}

If i take enumerator approach:

  def value_history
    logidze_versions(reverse: true, include_self: true).select { |version| version.changes["value"] }
  end

Issue here is with the version 3, running changes["value"] on a version 3 will show changes for the value (from the v1 if I'm seeing this correctly)

Basically I need to get all the records where value changed.
How would you recommend implementing this?

@dixpac
Copy link
Author

dixpac commented Jun 22, 2024

I can do it with custom enumerator method, I'm just wondering is there a better approach you would reccomend :)

  def value_history
    versions_meta = log_data.versions.dup
    versions_with_value_changed = versions_meta.select { |version| version.changes.has_key?("value") }
    Enumerator.new { |yielder| versions_with_value_changed.each { yielder << at(version: _1.version) } }
  end

@palkan
Copy link
Owner

palkan commented Jun 27, 2024

Yeah, we accumulated all changes into a single changeset and apply it to the current record to build a back-in-time copy:

object_at.apply_diff(log_entry.version, log_data.changes_to(version: log_entry.version))

Thus, using #changes doesn't work as you wish.

We can do something like this:

def value_history
  logidze_versions(reverse: true, include_self: true).select do |record|
    record.log_data.find_by_version(record.log_data.version).changes.key?("value")
  end
end

So, we look at the Logize version information for the current version number (log_data.version).

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

No branches or pull requests

2 participants