Created
January 12, 2013 02:44
-
-
Save bricker/4515785 to your computer and use it in GitHub Desktop.
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 ActionView | |
module Helpers | |
class FormBuilder | |
#---------------------- | |
def render_fields(partial, options={}) | |
@template.render( | |
:partial => "/admin/shared/fields/#{partial}_fields", | |
:locals => { | |
:f => self, | |
:index => self.object.object_id, | |
:options => options | |
}) | |
end | |
#---------------------- | |
def link_to_add_fields(association, options={}) | |
association = association.to_s | |
partial = options[:partial] || association.singularize | |
title = options[:title] || "Add Another #{association.singularize.titleize}" | |
new_object = self.object.send(association).klass.new | |
id = new_object.object_id | |
fields = self.simple_fields_for(association, new_object, child_index: id) do |nf| | |
nf.render_fields(partial) | |
end | |
@template.link_to(title, "#", class: "js-add-fields", | |
data: { id: id, build_target: options[:build_target], fields: fields.gsub("\n", "") }) | |
end | |
#---------------------- | |
def has_many_fields(association) | |
partial = association.to_s.singularize | |
fields = String.new | |
self.object.send(association).each do |obj| | |
fields << self.simple_fields_for(association, obj) do |nf| | |
nf.render_fields(partial) | |
end | |
end | |
fields.html_safe | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment