westonganger / active_snapshot

Simplified snapshots and restoration for ActiveRecord models and associations with a transparent white-box implementation
MIT License
103 stars 16 forks source link

How to track Mobility translated attributes and Action Text? #45

Open sedubois opened 6 months ago

sedubois commented 6 months ago

Thank you for this gem! I am evaluating it to track attributes which PaperTrail does not track properly.

Using Mobility, with the default installation (key-value backend), how do I track translated attributes (without having conflicts between different locales and in a way where records can be restored)?

I tried this:

class Page < ApplicationRecord
  extend Mobility
  include ActiveSnapshot
  translates :title, backend: :key_value, type: :string

  has_snapshot_children do
    instance = self.class.includes(:string_translations).find(id)
    { string_translations: instance.string_translations }
  end
end

If I have e.g. 2 locales and ensure that the page has a title value in each of those 2 locales, then calling snapshot = page.create_snapshot! successfully creates 3 snapshot items: one for the main record and one for each of the associated string_translations. This is good news because I hadn't managed to do this so far with Paper Trail or Audited (but I would be happy to be proven wrong.)

However when calling snapshot.restore! I get the error PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "pages_pkey".

Questions:

Finally, I need to combine all 3 aspects: snapshots for Action Text rich texts which are translated. I manage to translate Action Text rich texts using this gem I built. I could try combining it with this gem when I have more clarifications on the questions above.

sedubois commented 6 months ago

Here is a demo for Action Text:

class Page < ApplicationRecord
  include ActiveSnapshot
  has_rich_text :body
  has_snapshot_children do
    instance = self.class.includes(:rich_text_body).find(id)
    { rich_text_body: instance.rich_text_body }
  end
end

I manage to create the snapshot but again not to restore it.

sedubois commented 6 months ago

Demo with translated Action Text:

class Page < ApplicationRecord
  extend Mobility
  include ActiveSnapshot
  translates :body, backend: :action_text # Using https://github.com/sedubois/mobility-actiontext/
  has_snapshot_children do
    instance = self.class.includes(:rich_text_body).find(id)
    { rich_text_body: instance.rich_text_body }
  end
end