Last active
December 27, 2015 05:29
-
-
Save stuartchaney/7274894 to your computer and use it in GitHub Desktop.
Updated for newest version of nested_form gem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module NestedForm | |
module BuilderMixin | |
def link_to_add_hmt(*args, &block) | |
options = args.extract_options!.symbolize_keys | |
association = args.pop | |
association_two = 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 = options.delete(:model_object) do | |
reflection = object.class.reflect_on_association(association) | |
reflection.klass.new | |
end | |
options[:class] = [options[:class], "add_nested_fields"].compact.join(" ") | |
options["data-association"] = association | |
options["data-blueprint-id"] = fields_blueprint_id = fields_blueprint_id_for(association) | |
args << (options.delete(:href) || "javascript:void(0)") | |
args << options | |
model_object.send("build_#{association_two}".to_sym) | |
@fields ||= {} | |
@template.after_nested_form(fields_blueprint_id) do | |
blueprint = {:id => fields_blueprint_id, :style => 'display: none'} | |
block, options = @fields[fields_blueprint_id].values_at(:block, :options) | |
options[:child_index] = "new_#{association}" | |
blueprint[:"data-blueprint"] = fields_for(association, model_object, options, &block).to_str | |
@template.content_tag(:div, nil, blueprint) | |
end | |
@template.link_to(*args, &block) | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great work!