Sha256: 5bf346221eeee8e311751c32ef00dad61095bf364eb18a6b785a95219aa05ee0

Contents?: true

Size: 960 Bytes

Versions: 1

Compression:

Stored size: 960 Bytes

Contents

module LinkToAddFieldsHelper
  def link_to_add_fields(name, f, association, options = {})
    new_object = f.object.send(association).klass.new
    id = new_object.object_id
    objects_name = f.object.class.name.underscore.pluralize
    options_partial = options.delete(:partial)
    infered_partial = association.to_s.singularize + "_fields"
    partial_name = options_partial || infered_partial
    template_name = "#{objects_name}/#{partial_name}"
    fields = f.fields_for(association, new_object, child_index: id) do |builder|
      render(template_name, f: builder)
    end
    data = options[:data] || {}
    options[:data] = data.merge(
      link_to_add_field_id: id,
      link_to_add_field: fields.gsub("\n", "")
    )
    link_to name, '#', options
  end

  def link_to_remove_fields(name, target, options = {})
    data = options[:data] || {}
    options[:data] = data.merge(link_to_remove_field: target)
    link_to name, '#', options
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
link_to_add_fields-0.1.0 app/helpers/link_to_add_fields_helper.rb