Sha256: c88d0855978943c29c2539e5f95394e42a17095d0452020f117582127611194d

Contents?: true

Size: 1.17 KB

Versions: 9

Compression:

Stored size: 1.17 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)

      conditions.merge!(filtered_hash_by_method_for_given_keys(conditions.dup, :downcase, @case_insensitive_keys))
      conditions.merge!(filtered_hash_by_method_for_given_keys(conditions.dup, :strip, @strip_whitespace_keys))

      conditions
    end

    def filtered_hash_by_method_for_given_keys(conditions, method, condition_keys)
      condition_keys.each do |k|
        value = conditions[k]
        conditions[k] = value.send(method) if value.respond_to?(method)
      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

    def param_requires_string_conversion?(value)
      true
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
devise-2.2.8 lib/devise/param_filter.rb
loyal_devise-2.2.6 lib/devise/param_filter.rb
devise-2.2.7 lib/devise/param_filter.rb
loyal_devise-2.2.5 lib/devise/param_filter.rb
devise-2.2.6 lib/devise/param_filter.rb
devise-2.2.5 lib/devise/param_filter.rb
loyal_devise-2.1.10 lib/devise/param_filter.rb
devise-3.0.0.rc lib/devise/param_filter.rb
devise-2.2.4 lib/devise/param_filter.rb