Class: ActionView::Helpers::FormBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/batch_translations.rb

Instance Method Summary collapse

Instance Method Details

#globalize_fields_for(locale, *args, &proc) ⇒ Object

Raises:

  • (ArgumentError)


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

def globalize_fields_for(locale, *args, &proc)
  raise ArgumentError, "Missing block" unless block_given?
  @locale_index ||= {}

  if @locale_index[locale].nil?
    @index = @index ? @index + 1 : 1
    @locale_index[locale] = @index
  else
    @index = @locale_index[locale]
  end

  object_name = "#{@object_name}[translations_attributes][#{@index}]"
  object = @object.translations.select{|t| t.locale.to_s == locale.to_s}.first || @object.translations.find_by_locale(locale.to_s)
  @template.concat @template.hidden_field_tag("#{object_name}[id]", object ? object.id : "")
  @template.concat @template.hidden_field_tag("#{object_name}[locale]", locale)
  if @template.respond_to? :simple_fields_for
    @template.simple_fields_for(object_name, object, *args, &proc)
  elsif @template.respond_to? :semantic_fields_for
    @template.semantic_fields_for(object_name, object, *args, &proc)
  else
    @template.fields_for(object_name, object, *args, &proc)
  end
end