Class: LocalizedFields::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/localized_fields.rb

Instance Method Summary collapse

Instance Method Details

#label(attribute, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/localized_fields.rb', line 32

def label(attribute, options = {})
  if @options.has_key?(:language)
    language = @options[:language]
    
    super(attribute, for: "#{object_name}_#{attribute}_translations_#{language}").html_safe
  else
    field_name = @object_name.match(/.*\[(.*)_translations\]/)[1].capitalize
    super(attribute, field_name, options).html_safe
  end
end

#languageObject



28
29
30
# File 'lib/localized_fields.rb', line 28

def language
  @options[:language] if @options[:language]
end

#text_area(attribute, options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/localized_fields.rb', line 58

def text_area(attribute, options = {})
  if @options.has_key?(:language)
    language = @options[:language]
    
    super(attribute, id: "#{object_name}_#{attribute}_translations_#{language}", name: "#{object_name}[#{attribute}_translations][#{language}]").html_safe
  else
    super(attribute, options).html_safe
  end
end

#text_field(attribute, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/localized_fields.rb', line 43

def text_field(attribute, options = {})
  if @options.has_key?(:language)
    language = @options[:language]
    
    translations = @object.send("#{attribute}_translations") || {}
    
    value = translations.has_key?(language.to_s) ? translations[language.to_s] : nil
    
    super(attribute, value: value, id: "#{object_name}_#{attribute}_translations_#{language}", name: "#{object_name}[#{attribute}_translations][#{language}]").html_safe
  else
    value = @object ? @object[attribute.to_s] : nil
    super(attribute, value: value).html_safe
  end
end