Module: HstoreTranslate::Translates

Defined in:
lib/hstore_translate/translates.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#translates(*attrs) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hstore_translate/translates.rb', line 3

def translates(*attrs)
  include InstanceMethods

  class_attribute :translated_attrs
  alias_attribute :translated_attribute_names, :translated_attrs # Improve compatibility with the gem globalize
  self.translated_attrs = attrs

  attrs.each do |attr_name|
    serialize "#{attr_name}_translations", ActiveRecord::Coders::Hstore unless HstoreTranslate::native_hstore?

    define_method attr_name do
      read_hstore_translation(attr_name)
    end

    define_method "#{attr_name}=" do |value|
      write_hstore_translation(attr_name, value)
    end

    define_singleton_method "with_#{attr_name}_translation" do |value, locale = I18n.locale|
      quoted_translation_store = connection.quote_column_name("#{attr_name}_translations")
      where("#{quoted_translation_store} @> hstore(:locale, :value)", locale: locale, value: value)
    end
  end

  alias_method_chain :respond_to?, :translates
  alias_method_chain :method_missing, :translates
end

#translates?Boolean

Improve compatibility with the gem globalize

Returns:

  • (Boolean)


32
33
34
# File 'lib/hstore_translate/translates.rb', line 32

def translates?
  included_modules.include?(InstanceMethods)
end