Sha256: a17a195b97c08eaab83d12fc08bae40945a600663a53431f292ed95478207eb0

Contents?: true

Size: 1.35 KB

Versions: 18

Compression:

Stored size: 1.35 KB

Contents

module ActiveAdmin
  module ViewHelpers
    module FormHelper

      # Flatten a params Hash to an array of fields.
      #
      # @param params [Hash]
      # @param options [Hash] :namespace and :except
      #
      # @return [Array] of [Hash] with one element.
      #
      # @example
      #   fields_for_params(scope: "all", users: ["greg"])
      #     => [ {"scope" => "all"} , {"users[]" => "greg"} ]
      #
      def fields_for_params(params, options = {})
        namespace = options[:namespace]
        except = options[:except].is_a?(Array) ? options[:except] : [options[:except]]

        params.map do |k, v|
          next if namespace.nil? && %w(controller action commit utf8).include?(k.to_s)
          next if except.map(&:to_s).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.flatten.compact
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 3 rubygems

Version Path
yousty-activeadmin-1.0.17.pre lib/active_admin/view_helpers/fields_for.rb
yousty-activeadmin-1.0.16.pre lib/active_admin/view_helpers/fields_for.rb
yousty-activeadmin-1.0.15.pre lib/active_admin/view_helpers/fields_for.rb
yousty-activeadmin-1.0.14.pre lib/active_admin/view_helpers/fields_for.rb
yousty-activeadmin-1.0.13.pre lib/active_admin/view_helpers/fields_for.rb
yousty-activeadmin-1.0.12.pre lib/active_admin/view_helpers/fields_for.rb
yousty-activeadmin-1.0.11.pre lib/active_admin/view_helpers/fields_for.rb
yousty-activeadmin-1.0.10.pre lib/active_admin/view_helpers/fields_for.rb
yousty-activeadmin-1.0.9.pre lib/active_admin/view_helpers/fields_for.rb
yousty-activeadmin-1.0.8.pre lib/active_admin/view_helpers/fields_for.rb
yousty-activeadmin-1.0.7.pre lib/active_admin/view_helpers/fields_for.rb
yousty-activeadmin-1.0.6.pre lib/active_admin/view_helpers/fields_for.rb
yousty-activeadmin-1.0.5.pre lib/active_admin/view_helpers/fields_for.rb
active_administration-0.0.3 lib/active_admin/view_helpers/fields_for.rb
activeadministration-0.0.2 lib/active_admin/view_helpers/fields_for.rb
active_administration-0.0.2 lib/active_admin/view_helpers/fields_for.rb
activeadministration-0.0.1 lib/active_admin/view_helpers/fields_for.rb
active_administration-0.0.1 lib/active_admin/view_helpers/fields_for.rb