class Zobi::ParametersSanitizer
This class responsability is to filter parameters before sending it to the model.
This class is meant to be inherited.
Most of the time, two methods will be overriden: `fields` and `translated_fields`.
If you need complex logic based on context of execution (user rights and so on…), you have access to the controller through the `context` attr. Meaning, `context.current_user` is possible. And then, you can implement all the `if` possible in the world.
Attributes
Public Class Methods
@param [ApplicationController] context @param [ActionController::Parameters] canonical_params
@return [Sanitizer]
# File lib/zobi/parameters_sanitizer.rb, line 25 def initialize(context, canonical_params) @context, @canonical_params = context, canonical_params end
Public Instance Methods
The only public method. Return sanitized parameters.
@return [ActionController::Parameters]
# File lib/zobi/parameters_sanitizer.rb, line 33 def params canonical_params.permit resource_type => permitted_fields end
Protected Instance Methods
@return [Array] array of fields
# File lib/zobi/parameters_sanitizer.rb, line 41 def fields [] end
@return [Array] array of locales
# File lib/zobi/parameters_sanitizer.rb, line 53 def locales I18n.available_locales end
Computes all the fields
@return [Array] Complete list of fields
# File lib/zobi/parameters_sanitizer.rb, line 61 def permitted_fields fields + locales.map {|l| translated_fields.map {|f| "#{f}_#{l}" }} end
@return [Symbol] The resource name to be required
# File lib/zobi/parameters_sanitizer.rb, line 67 def resource_type self.class.to_s.demodulize.gsub('Parameters', '').underscore.to_sym end
@return [Array] array of translated fields
# File lib/zobi/parameters_sanitizer.rb, line 47 def translated_fields [] end