Module: I18nColumn::Base::ClassMethods

Defined in:
lib/i18n_column/base.rb

Instance Method Summary collapse

Instance Method Details

#i18n_column(*col_names) ⇒ Object

Creates i18n getter and setter methods for the given column names.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/i18n_column/base.rb', line 17

def i18n_column(*col_names)
  for col_name in col_names
    class_eval <<-EOV
      def #{col_name}
        json = #{col_name}_to_json
        json.nil? ? nil : json[::I18nColumn::Language.current_lang]
      end
    
      def #{col_name}=(value)
        json = #{col_name}_to_json || {}
        json[::I18nColumn::Language.current_lang] = value
        self[:#{col_name}] = json.to_json
        value
      end
    
      private
    
      def #{col_name}_to_json
        ::ActiveSupport::JSON::decode(self[:#{col_name}].to_s) || nil
      end
    EOV
  end
end