Sha256: 6cc493f341fde0fcc20b2e31ff9f2a30c1eeebe514db00c8eca395bbc052a1c8

Contents?: true

Size: 1.19 KB

Versions: 27

Compression:

Stored size: 1.19 KB

Contents

module Devise
  class ParamFilter
    def initialize(case_insensitive_keys, strip_whitespace_keys)
      @case_insensitive_keys = case_insensitive_keys || []
      @strip_whitespace_keys = strip_whitespace_keys || []
    end

    def filter(conditions)
      conditions = stringify_params(conditions.dup)

      @case_insensitive_keys.each do |k|
        value = conditions[k]
        next unless value.respond_to?(:downcase)
        conditions[k] = value.downcase
      end

      @strip_whitespace_keys.each do |k|
        value = conditions[k]
        next unless value.respond_to?(:strip)
        conditions[k] = value.strip
      end

      conditions
    end

    # Force keys to be string to avoid injection on mongoid related database.
    def stringify_params(conditions)
      return conditions unless conditions.is_a?(Hash)
      conditions.each do |k, v|
        conditions[k] = v.to_s if param_requires_string_conversion?(v)
      end
    end

    private

    # Determine which values should be transformed to string or passed as-is to the query builder underneath
    def param_requires_string_conversion?(value)
      [Fixnum, TrueClass, FalseClass, Regexp].none? {|clz| value.is_a? clz }
    end
  end
end

Version data entries

27 entries across 22 versions & 5 rubygems

Version Path
devise-2.2.2 lib/devise/param_filter.rb
devise-2.2.1 lib/devise/param_filter.rb
devise-2.2.0 lib/devise/param_filter.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/devise-2.2.0.rc/lib/devise/param_filter.rb
devise-2.2.0.rc lib/devise/param_filter.rb
af-devise-2.1.2 lib/devise/param_filter.rb
sunrise-cms-0.3.3 vendor/bundle/ruby/1.9.1/gems/devise-2.1.0/lib/devise/param_filter.rb
sunrise-cms-0.3.3 vendor/bundle/ruby/1.9.1/gems/devise-2.0.4/lib/devise/param_filter.rb
sunrise-cms-0.3.2 vendor/bundle/ruby/1.9.1/gems/devise-2.1.0/lib/devise/param_filter.rb
sunrise-cms-0.3.2 vendor/bundle/ruby/1.9.1/gems/devise-2.0.4/lib/devise/param_filter.rb
sunrise-cms-0.3.1 vendor/bundle/ruby/1.9.1/gems/devise-2.0.4/lib/devise/param_filter.rb
sunrise-cms-0.3.1 vendor/bundle/ruby/1.9.1/gems/devise-2.1.0/lib/devise/param_filter.rb
devise-2.1.2 lib/devise/param_filter.rb
sunrise-cms-0.3.0 vendor/bundle/ruby/1.9.1/gems/devise-2.1.0/lib/devise/param_filter.rb
sunrise-cms-0.3.0 vendor/bundle/ruby/1.9.1/gems/devise-2.0.4/lib/devise/param_filter.rb
rmello-devise-2.1.0.2 lib/devise/param_filter.rb
sunrise-cms-0.3.0.rc vendor/bundle/ruby/1.9.1/gems/devise-2.0.4/lib/devise/param_filter.rb
sunrise-cms-0.3.0.rc vendor/bundle/ruby/1.9.1/gems/devise-2.1.0/lib/devise/param_filter.rb
devise-2.1.0 lib/devise/param_filter.rb
devise-2.1.0.rc2 lib/devise/param_filter.rb