Sha256: cf9471306ea23599138b08034b01bedbb8f5eb401eb7d6748922207ed605cd43

Contents?: true

Size: 1.35 KB

Versions: 58

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

58 entries across 58 versions & 4 rubygems

Version Path
lalala-4.0.0.dev.128 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.126 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.125 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.124 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.123 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.118 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.116 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.114 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.113 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.111 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.109 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.107 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.94 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.92 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.90 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.84 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.80 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.79 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.78 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb
lalala-4.0.0.dev.77 vendor/deps/active_admin/lib/active_admin/view_helpers/fields_for.rb