Sha256: 2bb225f64c2ad8a2f7692f65c36c78af6ecb4f1da6328af1f3e5214bb0147035

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 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 Hash
            fields_for_params(v, :namespace => k)
          when Array
            v.map do |v|
              { "#{k}[]" => v }
            end
          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

3 entries across 3 versions & 1 rubygems

Version Path
activeadmin-0.5.0 lib/active_admin/view_helpers/fields_for.rb
activeadmin-0.5.0.pre1 lib/active_admin/view_helpers/fields_for.rb
activeadmin-0.5.0.pre lib/active_admin/view_helpers/fields_for.rb