Module: NestedForm::BuilderMixin
- Included in:
- Builder
- Defined in:
- lib/nested_form/builder_mixin.rb
Instance Method Summary collapse
- #fields_for_nested_model(name, object, options, block) ⇒ Object
- #fields_for_with_nested_attributes(association_name, *args) ⇒ Object
-
#link_to_add(*args, &block) ⇒ Object
Adds a link to insert a new associated records.
-
#link_to_remove(*args, &block) ⇒ Object
Adds a link to remove the associated record.
Instance Method Details
#fields_for_nested_model(name, object, options, block) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/nested_form/builder_mixin.rb', line 90 def fields_for_nested_model(name, object, , block) classes = 'fields' classes << ' marked_for_destruction' if object.respond_to?(:marked_for_destruction?) && object.marked_for_destruction? perform_wrap = .fetch(:nested_wrapper, true) perform_wrap &&= [:wrapper] != false # wrap even if nil if perform_wrap @template.content_tag(:div, super, :class => classes) else super end end |
#fields_for_with_nested_attributes(association_name, *args) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/nested_form/builder_mixin.rb', line 74 def fields_for_with_nested_attributes(association_name, *args) # TODO Test this better block = args.pop || Proc.new { |fields| @template.render(:partial => "#{association_name.to_s.singularize}_fields", :locals => {:f => fields}) } = args.dup. # Rails 3.0.x if .empty? && args[0].kind_of?(Array) = args[0].dup. end @fields ||= {} @fields[fields_blueprint_id_for(association_name)] = { :block => block, :options => } super(association_name, *(args << block)) end |
#link_to_add(*args, &block) ⇒ Object
Adds a link to insert a new associated records. The first argument is the name of the link, the second is the name of the association.
f.link_to_add("Add Task", :tasks)
You can pass HTML options in a hash at the end and a block for the content.
<%= f.link_to_add(:tasks, :class => "add_task", :href => new_task_path) do %>
Add Task
<% end %>
You can also pass model_object
option with an object for use in the blueprint, e.g.:
<%= f.link_to_add(:tasks, :model_object => Task.new(:name => 'Task')) %>
See the README for more details on where to call this method.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/nested_form/builder_mixin.rb', line 19 def link_to_add(*args, &block) = args..symbolize_keys association = args.pop unless object.respond_to?("#{association}_attributes=") raise ArgumentError, "Invalid association. Make sure that accepts_nested_attributes_for is used for #{association.inspect} association." end model_object = .delete(:model_object) do reflection = object.class.reflect_on_association(association) reflection.klass.new end [:class] = [[:class], "add_nested_fields"].compact.join(" ") ["data-association"] = association ["data-blueprint-id"] = fields_blueprint_id = fields_blueprint_id_for(association) args << (.delete(:href) || "javascript:void(0)") args << @fields ||= {} @template.after_nested_form(fields_blueprint_id) do blueprint = {:id => fields_blueprint_id, :style => 'display: none'} block, = @fields[fields_blueprint_id].values_at(:block, :options) [:child_index] = "new_#{association}" blueprint[:"data-blueprint"] = fields_for(association, model_object, , &block).to_str @template.content_tag(:div, nil, blueprint) end @template.link_to(*args, &block) end |
#link_to_remove(*args, &block) ⇒ Object
Adds a link to remove the associated record. The first argment is the name of the link.
f.link_to_remove("Remove Task")
You can pass HTML options in a hash at the end and a block for the content.
<%= f.link_to_remove(:class => "remove_task", :href => "#") do %>
Remove Task
<% end %>
See the README for more details on where to call this method.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/nested_form/builder_mixin.rb', line 60 def link_to_remove(*args, &block) = args..symbolize_keys [:class] = [[:class], "remove_nested_fields"].compact.join(" ") # Extracting "milestones" from "...[milestones_attributes][...]" md = object_name.to_s.match /(\w+)_attributes\]\[[\w\d]+\]$/ association = md && md[1] ["data-association"] = association args << (.delete(:href) || "javascript:void(0)") args << hidden_field(:_destroy) << @template.link_to(*args, &block) end |