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
# File 'lib/hstore_translate/translates.rb', line 3

def translates(*attrs)
  include InstanceMethods

  class_attribute :translated_attrs
  self.translated_attrs = attrs

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

    class_eval <<-RUBY
      def #{attr_name}
        read_hstore_translation('#{attr_name}')
      end

      def #{attr_name}=(value)
        write_hstore_translation('#{attr_name}', value)
      end
    RUBY
  end

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