Module: ActiveAdmin::FormHelper

Defined in:
app/helpers/active_admin/form_helper.rb

Constant Summary collapse

RESERVED_PARAMS =
%w(controller action commit utf8).freeze

Instance Method Summary collapse

Instance Method Details

#active_admin_filters_form_for(search, filters, options = {}) ⇒ Object

Helper method to render a filter form



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/helpers/active_admin/form_helper.rb', line 62

def active_admin_filters_form_for(search, filters, options = {})
  defaults = { builder: ActiveAdmin::Filters::FormBuilder, url: collection_path, html: { class: "filters-form" } }
  required = { html: { method: :get }, as: :q }
  options = defaults.deep_merge(options).deep_merge(required)

  form_for search, options do |f|
    f.template.concat hidden_field_tags_for(params, except: except_hidden_fields)

    filters.each do |attribute, opts|
      next if opts.key?(:if) && !call_method_or_proc_on(self, opts[:if])
      next if opts.key?(:unless) && call_method_or_proc_on(self, opts[:unless])

      filter_opts = opts.except(:if, :unless)
      filter_opts[:input_html] = instance_exec(&filter_opts[:input_html]) if filter_opts[:input_html].is_a?(Proc)

      f.filter attribute, filter_opts
    end

    buttons =  :div, class: "filters-form-buttons" do
      f.submit(I18n.t("active_admin.filters.buttons.filter"), class: "filters-form-submit") +
        link_to(I18n.t("active_admin.filters.buttons.clear"), collection_path, class: "filters-form-clear")
    end

    f.template.concat buttons
  end
end

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



6
7
8
9
10
# File 'app/helpers/active_admin/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.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/helpers/active_admin/form_helper.rb', line 30

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? && RESERVED_PARAMS.include?(k.to_s)
    next if except.include?(k.to_s)

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

    case v
    when String, TrueClass, FalseClass
      { 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 => "" }
    else
      raise TypeError, "Cannot convert #{v.class} value: #{v.inspect}"
    end
  end.compact
end

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



12
13
14
15
16
17
# File 'app/helpers/active_admin/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