Module: ActiverecordGlobalize::Translates::ClassMethods

Defined in:
lib/activerecord_globalize/translates.rb

Overview

Patches ActiveRecord models

Instance Method Summary collapse

Instance Method Details

#translates(*attrs) ⇒ Object

Overrides ActiveRecord getters and setters to consider I18n locales

attrs translated fields names

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/activerecord_globalize/translates.rb', line 66

def translates(*attrs)
  raise(ArgumentError, 'must have at least one record to localize') if attrs.empty?

  self.translated_attrs = attrs
  attrs.each do |attr_name|
    define_method "#{attr_name}=" do |value|
      write_translation(attr_name, value)
    end

    define_method attr_name do
      read_translation(attr_name)
    end

    define_method "#{attr_name}_translations" do
      self["#{attr_name}_translations"].try(:with_indifferent_access)
    end
  end
end