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 track Mobility translated attributes and Action Text? #45

Open
sedubois opened this issue Jan 10, 2024 · 2 comments
Open

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

sedubois opened this issue Jan 10, 2024 · 2 comments

Comments

@sedubois
Copy link

sedubois commented Jan 10, 2024

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:

  • How to fix the reification error?
  • Would there be a way to track the logical title attribute instead of referring to string_translations which is meant to be an implementation detail? As the name suggests, Mobility is supposed to allow flexibly using different types of backends and the association name string_translations is only applicable to its key-value backend.
  • From a logical standpoint, these translations aren't really children. The page's title isn't a "child" of the page but an attribute of the page. Would it make more sense to rename has_snapshot_children to has_associated_snapshots or similar?
  • Similar question as in the Mobility example above but for Rails Action Text rich texts: they are similarly stored in a separate table using polymorphic relationship. How to track those?

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
Copy link
Author

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 sedubois changed the title How to track Mobility translated attributes? How to track Mobility translated attributes and Action Text? Jan 10, 2024
@sedubois
Copy link
Author

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

@sedubois sedubois mentioned this issue Jan 10, 2024
6 tasks
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

1 participant