Module: NestedEditorFor::Builder

Defined in:
lib/nested_editor_for/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



3
4
5
# File 'lib/nested_editor_for/builder.rb', line 3

def object
  @object
end

#templateObject (readonly)

Returns the value of attribute template.



3
4
5
# File 'lib/nested_editor_for/builder.rb', line 3

def template
  @template
end

Instance Method Details

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

override: do arrays like attributes



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/nested_editor_for/builder.rb', line 8

def fields_for(method_or_object, *args, &block)
  options = args.extract_options!
  if @object
    case method_or_object
    when String, Symbol
      object = @object.send method_or_object
      if object.is_a?(Array) or object.is_a?(ActiveRecord::Relation)
        name = options[:name] || "#{@object_name}[#{method_or_object}_attributes]"
        return ((0...object.length).collect do |i|
          template.fields_for("#{name}[#{i}]", object[i], *args, &block)
        end).join.html_safe
      else
        name = options[:name] || method_or_object
        return super(name, object, *args, &block)
      end
    end
  end

  super(method_or_object, *args, &block)
end

#hidden_field(method_or_object, *args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nested_editor_for/builder.rb', line 31

def hidden_field(method_or_object, *args)
  method = case method_or_object
  when String, Symbol; method_or_object
  when Array; obj.first.class.name.tableize.singularize
  else; obj.class.name.tableize.singularize
  end

  html_options = args.extract_options!
  html_options[:id] = html_options[:name].parameterize.underscore if html_options[:name] && !html_options.key?(:id)
  html_options["data-attr"] = method

  super(method_or_object, html_options)
end

#nested_editor_for(method, *args, &block) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/nested_editor_for/builder.rb', line 61

def nested_editor_for(method, *args, &block)
  raise ArgumentError, "Missing block" unless block_given?

  template.instance_variable_set "@enable_nested_records", true

  attr_name = "#{@object_name}[#{method}_attributes]"

  # for some reason, things break if I make "#{@object_name}[#{object_name.to_s}_attributes]" the 'id' of the table
  template.(:div, class: "nested editor") do
    template.(:ol, attr: attr_name) do
      nested_editor_wrapper(method, attr_name, &block)
    end
  end
end

#static_field(method, value) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/nested_editor_for/builder.rb', line 47

def static_field(method, value)
  attr_name = "#{@object_name}[#{method}]"
  html_options = {
    'data-attr' => method,
    :type => "hidden",
    :value => value,
    :attr => attr_name,
    :name => attr_name
  }
  template.tag("input", html_options)
end