Class: SimpleNestedForm::Builder

Inherits:
SimpleForm::FormBuilder
  • Object
show all
Defined in:
lib/simple_nested_form/builder.rb

Instance Method Summary collapse

Instance Method Details

#fields_for(record_or_name_or_array, *args, &block) ⇒ Object



52
53
54
55
56
57
# File 'lib/simple_nested_form/builder.rb', line 52

def fields_for(record_or_name_or_array, *args, &block)
  options = args.extract_options!
  options[:builder] = SimpleNestedForm::Builder

  super(record_or_name_or_array, *(args << options), &block)
end

#fields_for_nested_model(name, association, args, block) ⇒ Object



64
65
66
67
68
# File 'lib/simple_nested_form/builder.rb', line 64

def fields_for_nested_model(name, association, args, block)
   :div, :class => "fields #{association.class.model_name.human.downcase}" do
    super
  end
end

#fields_for_with_nested_attributes(association, *args, block) ⇒ Object



59
60
61
62
# File 'lib/simple_nested_form/builder.rb', line 59

def fields_for_with_nested_attributes(association, *args, block)
  nested_fields[association] = block
  super
end


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/simple_nested_form/builder.rb', line 7

def link_to_add(*args, &block)
  if block_given?
    association = args.first
    options     = args.second || {}
  else
    name        = args.first
    association = args.second
    options     = args.third || {}
  end

  options.merge!(:"data-association" => association)
  options[:"data-limit"] = options.delete(:limit)

  after_nested_form(association) do
    model_object = object.class.reflect_on_association(association).klass.new

     :div, :id => "#{association}_fields_blueprint", :style => "display: none" do
      fields_for(association, model_object, :child_index => "new_#{association}", &nested_fields[association])
    end
  end

  if block_given?
    link_to(capture(&block), "javascript:void(0)", options)
  else
    link_to(name, "javascript:void(0)", options)
  end
end


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/simple_nested_form/builder.rb', line 35

def link_to_remove(*args, &block)
  if block_given?
    options = args.first || {}
  else
    name    = args.first
    options = args.second || {}
  end

  options.merge!(:"data-remove-association" => 1)

  hidden_field(:_destroy) + if block_given?
    link_to(capture(&block), "javascript:void(0)", options)
  else
    link_to(name, "javascript:void(0)", options)
  end
end