Class: ParamsSanitizer::Base

Inherits:
Object
  • Object
show all
Includes:
Sanitizers::AcceptRange, Sanitizers::AcceptRegex, Sanitizers::AcceptValue, Sanitizers::ExistValue, Sanitizers::RejectRange, Sanitizers::RejectRegex, Sanitizers::RejectValue
Defined in:
lib/params_sanitizer/base.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.definitionsObject (readonly)

Returns the value of attribute definitions.



94
95
96
# File 'lib/params_sanitizer/base.rb', line 94

def definitions
  @definitions
end

Class Method Details

.check_duplicated_definition!(key) ⇒ Object

Check a duplicated definition rule of parameter.

Parameters:

  • key (String)

    key of parameter.

Raises:

  • (ArgumentError)

    if duplicate the rule.



24
25
26
27
28
29
30
31
32
33
# File 'lib/params_sanitizer/base.rb', line 24

def self.check_duplicated_definition!(key)
  string_key = key.to_s
  definitions.each_value do |rules|
    rules.each_key do |definition_key|
      if definition_key == string_key
        raise ArgumentError, 'already define the ruel for #{key}!!'
      end
    end
  end
end

.inherited(child) ⇒ Object

callback after inherited.



38
39
40
# File 'lib/params_sanitizer/base.rb', line 38

def self.inherited(child)
  child.instance_variable_set(:@definitions, Hash.new)
end

.permit_filterArray or Hash

This method is abstract.

Define after inheritance.

Note:

Keys passed strong parameter.(ActionController::Parameters.permit method.)

Get a list of permit keys.

Examples:

def self.permit_filter
  [:user_name, :user_email, :user_age]
end

Returns:

  • (Array or Hash)

    a list of keys.

Raises:

  • (ArgumentError)

    if not abstract.



53
54
55
# File 'lib/params_sanitizer/base.rb', line 53

def self.permit_filter
  raise NoMethodError, 'Not define `self.permit_filter`. '
end

.sanitize(params, key = nil) ⇒ Object

Sanitize parameters.

Examples:

# if sent next params.
# {
#   user: { name: 'hoge', email: 'fuga'  }
# }
SanitizerClass.sanitize(params, :user)

# if sent next params.
# { name: 'hoge', email: 'fuga' }
SanitizerClass.sanitize(params)

Parameters:

  • params (ActiveController::Parameters)

    parameter of Action.

  • key (String) (defaults to: nil)

    key of parameter. (if params ‘email:‘fuga’}‘ then :user)

Raises:

  • (ActionController::ParameterMissing)

    if nothing key.



73
74
75
# File 'lib/params_sanitizer/base.rb', line 73

def self.sanitize(params, key = nil)
  new.sanitize_params (key ? params.require(key) : params).permit(permit_filter)
end

Instance Method Details

#sanitize_params(params) ⇒ Hash

Sanitize params bu definition rules.

Parameters:

  • parmas (ActionController::Parameter)

    parameter,

Returns:

  • (Hash)

    sanitizer hash of params.(Hash keys are symbol)



82
83
84
85
86
87
88
89
90
# File 'lib/params_sanitizer/base.rb', line 82

def sanitize_params(params)
  sanitized = params.to_hash

  self.class.definitions.each do |key, rules|
    send("sanitize_#{key}!", sanitized, rules)
  end

  sanitized.symbolize_keys
end