Sha256: ec0b13e8490ff67dbc09392f30f1e3a7c317d9ec5ce19c2be2ee3d8bbb30b746

Contents?: true

Size: 1.31 KB

Versions: 36

Compression:

Stored size: 1.31 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    = 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
    end
  end
end

Version data entries

36 entries across 36 versions & 4 rubygems

Version Path
activeadmin-rails-1.7.1 lib/active_admin/view_helpers/fields_for.rb
activeadmin-rails-1.7.0 lib/active_admin/view_helpers/fields_for.rb
activeadmin-rb-1.6.0 lib/active_admin/view_helpers/fields_for.rb
activeadmin-rb-1.5.2 lib/active_admin/view_helpers/fields_for.rb
activeadmin-rb-1.5.1 lib/active_admin/view_helpers/fields_for.rb
activeadmin-2.7.0 lib/active_admin/view_helpers/fields_for.rb
activeadmin-2.6.1 lib/active_admin/view_helpers/fields_for.rb
activeadmin-2.6.0 lib/active_admin/view_helpers/fields_for.rb
activeadmin-2.5.0 lib/active_admin/view_helpers/fields_for.rb
activeadmin-2.4.0 lib/active_admin/view_helpers/fields_for.rb
activeadmin-2.3.1 lib/active_admin/view_helpers/fields_for.rb
activeadmin-2.3.0 lib/active_admin/view_helpers/fields_for.rb
activeadmin-2.2.0 lib/active_admin/view_helpers/fields_for.rb
activeadmin-2.1.0 lib/active_admin/view_helpers/fields_for.rb
activeadmin-2.0.0 lib/active_admin/view_helpers/fields_for.rb
activeadmin-rb-1.5.0 lib/active_admin/view_helpers/fields_for.rb
activeadmin-2.0.0.rc2 lib/active_admin/view_helpers/fields_for.rb
activeadmin_addons-1.7.1 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/view_helpers/fields_for.rb
activeadmin-2.0.0.rc1 lib/active_admin/view_helpers/fields_for.rb
activeadmin-rb-1.4.0 lib/active_admin/view_helpers/fields_for.rb