Skip to content

Instantly share code, notes, and snippets.

@maxivak
Last active August 30, 2023 17:10
Show Gist options
  • Save maxivak/e1c6b39b9ec5a16d2d3daacbdd2a64d4 to your computer and use it in GitHub Desktop.
Save maxivak/e1c6b39b9ec5a16d2d3daacbdd2a64d4 to your computer and use it in GitHub Desktop.
Rails: Audit Globalize translations
  • Globalize adds model translations to ActiveRecord models.
  • Audited is an ORM extension that logs all changes to your models.

Using Audited gem it will only logs changes to the base model and ignore changes to the translation table.

Below is how to add audited to the translation model.

Audited and Globalize

Globalize adds nested class Translation inside your model class.

We will reopen Translation class inside my model to add 'audited':

class Mymodel < ApplicationRecord

  # translation
  translates :title, :description


  # audit
  audited

 # ADD THIS
  Translation.class_eval do
    audited
  end

Install audited

Gemfile

gem "audited"
  • install db tables
rails generate audited:install
rake db:migrate

Globalize

Gemfile

gem 'globalize', '~> 5.0.0'
@hassan-righthand
Copy link

Instead of adding inside multiple models wherever we are using translate. How we can use concerns and include that concern inside the model to avoid code duplication?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment