class Interview::PolymorphicNestedFormAddLink
Attributes
polymorphic_classes[RW]
Public Instance Methods
build(b)
click to toggle source
# File lib/interview/controls/polymorphic_nested_form_add_link.rb, line 6 def build(b) form_builder = find_attribute! :form_builder assoc_method = find_attribute!(:assoc_method).to_sym if @polymorphic_classes build_polymorphic_link(b, form_builder, assoc_method) else text = h.t('views.nested_form_add', association: find_attribute!(:singular_title)) html = render_form(form_builder, assoc_method) b.link capion: text, html_options: { href: '#', class: 'nested_form_add_link', data: { content: CGI::escapeHTML(html) } } end end
Protected Instance Methods
build_polymorphic_link(b, form_builder, assoc_method)
click to toggle source
# File lib/interview/controls/polymorphic_nested_form_add_link.rb, line 20 def build_polymorphic_link(b, form_builder, assoc_method) poly_classes = @polymorphic_classes.map do |poly_class| poly_class.is_a?(String) ? poly_class.camelcase.constantize : poly_class end select_options = [[ h.t('helpers.select.prompt'), nil ]] select_options += poly_classes.map do |poly_class| [ poly_class.human_name, poly_class.name ] end data_content = {} poly_classes.each do |poly_class| new_object = form_builder.object.association(assoc_method.to_sym).build(type: poly_class.name) html = render_form(form_builder, assoc_method, new_object) data_content[poly_class.model_name.singular] = html end b.section do b << h.select_tag("add_link_class", h.options_for_select(select_options), class: 'form-control', style: 'display: inline; width: auto;') b.space b << h.content_tag(:a, h.t('views.add'), {href: '#', class: 'nested_form_polymorphic_add_link btn btn-default', data: { content: CGI::escapeHTML(data_content.to_json) } }, false) end end
render_form(form_builder, assoc_method, new_object=nil)
click to toggle source
# File lib/interview/controls/polymorphic_nested_form_add_link.rb, line 45 def render_form(form_builder, assoc_method, new_object=nil) new_object ||= form_builder.object.association(assoc_method.to_sym).build new_object.assign_attributes(new_object.class.defaults) if new_object_assoc = new_object.class.reflect_on_all_associations.find { |a| a.foreign_key == form_builder.object.association(assoc_method.to_sym).reflection.foreign_key } new_object.association(new_object_assoc.name).target ||= form_builder.object end nested_form = find_attribute! :nested_form return form_builder.fields_for(assoc_method, new_object, :child_index => "new_association") do |builder| if @polymorphic_classes nested_form.render_polymorphic_nested_form(builder) else nested_form.render_nested_form(builder) end end end