Sha256: 99e41251bd09c00eb4e382c211f3ba8324b7e910917cd1c4d88ccdba4f1b9e52

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

# add accessors with locale suffix like `title_en`, `title_de`
module GlobalizeAccessorsWithLocaleSuffix
  def define_translated_attr_reader(name)
    super

    Globalize.available_locales.each do |locale|
      method_name = "#{name}_#{locale}"
      define_method method_name.to_sym do
        Globalize::Interpolation.interpolate(name, self, [locale])
      end
    end
  end

  def define_translated_attr_writer(name)
    super

    Globalize.available_locales.each do |locale|
      define_method :"#{name}_#{locale}=" do |value|
        write_attribute(name, value, {locale: locale})
      end
    end
  end
end

Globalize::ActiveRecord::ClassMethods.module_eval do
  prepend GlobalizeAccessorsWithLocaleSuffix
end


module GlobalizeFixResetAttribute
  def _reset_attribute name
    old_value = record.changed_attributes[name]
    record.original_changed_attributes.except!(name)
    record.send("#{name}=", old_value)
  end
end

Globalize::ActiveRecord::AdapterDirty.module_eval do
  prepend GlobalizeFixResetAttribute
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ab_admin-0.8.1 lib/ab_admin/hooks/globalize_locale_suffix_accessors.rb
ab_admin-0.8.0 lib/ab_admin/hooks/globalize_locale_suffix_accessors.rb
ab_admin-0.7.0 lib/ab_admin/hooks/globalize_locale_suffix_accessors.rb