Module: ActiveAdmin::ViewHelpers::FormHelper

Included in:
ActiveAdmin::ViewHelpers
Defined in:
lib/active_admin/view_helpers/fields_for.rb,
lib/active_admin/view_helpers/form_helper.rb

Instance Method Summary collapse

Instance Method Details

#active_admin_form_for(resource, options = {}, &block) ⇒ Object



6
7
8
9
10
# File 'lib/active_admin/view_helpers/form_helper.rb', line 6

def active_admin_form_for(resource, options = {}, &block)
  Arbre::Context.new({}, self) do
    active_admin_form_for resource, options, &block
  end.content
end

#fields_for_params(params, options = {}) ⇒ Array

Flatten a params Hash to an array of fields.

Examples:

fields_for_params(scope: "all", users: ["greg"])
  => [ {"scope" => "all"} , {"users[]" => "greg"} ]

Parameters:

  • params (Hash)
  • options (Hash) (defaults to: {})

    :namespace and :except

Returns:

  • (Array)

    of [Hash] with one element.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/active_admin/view_helpers/fields_for.rb', line 17

def fields_for_params(params, options = {})
  namespace = options[:namespace]
  except = Array.wrap(options[:except]).map &:to_s

  params.flat_map do |k, v|
    next if namespace.nil? && %w(controller action commit utf8).include?(k.to_s)
    next if except.include?(k.to_s)

    if namespace
      k = "#{namespace}[#{k}]"
    end

    case v
    when String
      { k => v }
    when Symbol
      { k => v.to_s }
    when Hash
      fields_for_params(v, namespace: k)
    when Array
      v.map do |v|
        { "#{k}[]" => v }
      end
    when nil
      { k => "" }
    when TrueClass, FalseClass
      { k => v }
    else
      raise "I don't know what to do with #{v.class} params: #{v.inspect}"
    end
  end.compact
end

#hidden_field_tags_for(params, options = {}) ⇒ Object



12
13
14
15
16
17
# File 'lib/active_admin/view_helpers/form_helper.rb', line 12

def hidden_field_tags_for(params, options = {})
  fields_for_params(params.to_unsafe_hash, options).map do |kv|
    k, v = kv.first
    hidden_field_tag k, v, id: sanitize_to_id("hidden_active_admin_#{k}")
  end.join("\n").html_safe
end