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

canonical_params[R]
context[R]

Public Class Methods

new(context, canonical_params) click to toggle source

@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

params() click to toggle source

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

fields() click to toggle source

@return [Array] array of fields

# File lib/zobi/parameters_sanitizer.rb, line 41
def fields
  []
end
locales() click to toggle source

@return [Array] array of locales

# File lib/zobi/parameters_sanitizer.rb, line 53
def locales
  I18n.available_locales
end
permitted_fields() click to toggle source

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
resource_type() click to toggle source

@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
translated_fields() click to toggle source

@return [Array] array of translated fields

# File lib/zobi/parameters_sanitizer.rb, line 47
def translated_fields
  []
end