Class: ParamsSanitizer::Base
- Inherits:
-
Object
- Object
- ParamsSanitizer::Base
- 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
-
.definitions ⇒ Object
readonly
Returns the value of attribute definitions.
Class Method Summary collapse
-
.check_duplicated_definition!(key) ⇒ Object
Check a duplicated definition rule of parameter.
-
.inherited(child) ⇒ Object
callback after inherited.
-
.permit_filter ⇒ Array or Hash
abstract
Get a list of permit keys.
-
.sanitize(params, key = nil) ⇒ Object
Sanitize parameters.
Instance Method Summary collapse
-
#sanitize_params(params) ⇒ Hash
Sanitize params bu definition rules.
Class Attribute Details
.definitions ⇒ Object (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.
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_filter ⇒ Array or Hash
This method is abstract.
Define after inheritance.
Note:
Keys passed strong parameter.(ActionController::Parameters.permit method.)
Get a list of permit keys.
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.
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.
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 |