module ActionView::Helpers::FormHelper

Public Instance Methods

fields_for(record_name, record_object = nil, options = {}, &block) click to toggle source

github.com/rails/rails/blob/4-2-stable/actionview/lib/action_view/helpers/form_helper.rb#L712

# File lib/strong_form/form.rb, line 40
def fields_for(record_name, record_object = nil, options = {}, &block)
  if record_object.respond_to?(:permitted_attributes=)
    if options.key?(:permitted_attributes)
      record_object.permitted_attributes = options[:permitted_attributes]
    elsif options[:parent_builder].object.try(:permitted_attributes) &&
          record_object.permitted_attributes.nil?
      assign_child_permitted_attributes!(
        record_name, record_object, options[:parent_builder].object.permitted_attributes
      )
    end
  end

  builder = instantiate_builder(record_name, record_object, options)
  capture(builder, &block)
end
form_for(record, options = {}, &block) click to toggle source
# File lib/strong_form/form.rb, line 6
def form_for(record, options = {}, &block)
  object = record.is_a?(Array) ? record.last : record

  if options.key?(:permitted_attributes) && object.respond_to?(:permitted_attributes=)
    object.permitted_attributes = options.delete(:permitted_attributes)
  end

  orig_form_for(record, options, &block)
end
Also aliased as: orig_form_for
orig_form_for(record, options = {}, &block)
Alias for: form_for

Private Instance Methods

assign_child_permitted_attributes!( record_name, record_object, parent_permitted_attributes ) click to toggle source
# File lib/strong_form/form.rb, line 16
def assign_child_permitted_attributes!(
  record_name, record_object, parent_permitted_attributes
)
  if parent_permitted_attributes == true # parent allowed everything
    record_object.permitted_attributes = parent_permitted_attributes
    return
  end

  # search for nested attributes

  # record_name is of the form `user[child_models_attributes][0]`
  # try to extract `child_models_attributes`
  permitted_name =
    record_name.match(/.*?\[([^\]\[]+)\](\[[0-9]+\])?$/)[1].to_sym

  # find the hash with the key `child_models_attributes`
  record_object.permitted_attributes =
    StrongForm::Finder.find_child_permitted_attributes(
      permitted_name, parent_permitted_attributes
    )
end