Sha256: 91e641ef38c58f37bbc2e2602985ec5875543db6a0ae484a725c05fe647aac7d

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

# encoding: utf-8
module DynamicFieldsForHelper

  def link_to_add_fields(form, association, options = {}, &block)
    partial = options[:partial] || nil
    name = options[:name] || nil
    css_classes = options[:class] || nil

    new_object = form.object.send(association).klass.new
    id = new_object.object_id
    fields = form.fields_for(association, new_object, child_index: id) do |builder|
      if partial
        render("#{form.object.class.name.downcase.pluralize}/#{partial}", fields: builder)
      else
        render("#{form.object.class.name.downcase.pluralize}/#{association.to_s.singularize}_fields", fields: builder)
      end
    end
    css_classes = css_classes(DynamicFieldsForRails.add_css_classes, css_classes)
    if block_given?
    	link_to('#', class: css_classes, data: {id: id, fields: fields.gsub("\n", "")}, &block)
    else
    	link_to(name, '#', class: css_classes, data: {id: id, fields: fields.gsub("\n", "")})
  	end
  end

  def link_to_delete_fields(fields, options = {}, &block)
    name = options[:name] || nil
    css_classes = options[:class] || nil

    link = []
    link << fields.hidden_field(:_destroy)
    css_classes = css_classes(DynamicFieldsForRails.delete_css_classes, css_classes)
    if block_given?
      link << link_to('#', class: css_classes, title: name, &block)
    else
      link << link_to(name, '#', class: css_classes)
    end
    return link.join('').html_safe
  end

  protected

  def css_classes(default, css_class)
    style_class = []
    style_class << default unless default.blank?
    style_class << css_class unless css_class.blank?
    style_class.join(' ')
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dynamic_fields_for_rails-0.5.2 app/helpers/dynamic_fields_for_helper.rb
dynamic_fields_for_rails-0.5.1 app/helpers/dynamic_fields_for_helper.rb
dynamic_fields_for_rails-0.5.0 app/helpers/dynamic_fields_for_helper.rb