Module: TranslatesFields::ClassMethods

Defined in:
lib/translates_fields/active_record.rb

Instance Method Summary collapse

Instance Method Details

#translates(field) ⇒ Object



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/translates_fields/active_record.rb', line 7

def translates(field)
  serialize field

  send :define_method, "#{field}=".to_sym do |*args|
    if args.first.is_a?(Array)
      key     = args.first.shift
      options = args.first.pop
    else
      key     = args.first
      options = {}
    end
    write_attribute(field, [key, options])
  end

  send :define_method, field do
    return '' unless read_attribute(field).is_a?(Array)
    scope   = read_attribute(field).first
    options = read_attribute(field).last
    key     = "models.#{self.class.to_s.underscore}.attributes.#{field}.#{scope}".to_sym
    I18n.translate(key, options)
  end

end