Sha256: 7ba867e001d0b432fd525ab3ecdfda57598ca14cb262dd5e0c3f4db9f55e7491

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module NestedForm
  module BuilderMixin
    alias_method :orig_link_to_add, :link_to_add
    alias_method :orig_link_to_remove, :link_to_remove

    def link_to_add(association, options = {}, &block)
      if object.respond_to?(:permitted_attributes)
        return unless object.permitted_nested_attributes?(association)

        unless options[:model_object]
          reflection = object.class.reflect_on_association(association)
          options[:model_object] = reflection.klass.new
        end

        unless options[:model_object].permitted_attributes.present?
          if object.permitted_attributes == true || object.permitted_attributes.nil?
            options[:model_object].permitted_attributes = true
          else
            options[:model_object].permitted_attributes =
              StrongForm::Finder.find_child_permitted_attributes(
                "#{association}_attributes".to_sym, object.permitted_attributes
              )
          end
        end
      end

      orig_link_to_add(association, options, &block)
    end

    def link_to_remove(*args, &block)
      return if object.respond_to?(:permitted_attributes) &&
                !object.permitted_attributes.nil? &&
                object.permitted_attributes != true &&
                !object.permitted_attributes.include?(:_destroy)
      orig_link_to_remove(*args, &block)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
strong_form-0.0.1 lib/strong_form/nested_form.rb