Module: DRYParams::Dryable::ClassMethods
- Defined in:
- lib/dryparams/dryable.rb
Overview
The class methods of the DRYParams
concern.
Constant Summary collapse
- DEFAULT_ACTIONS =
The default actions for callback controller.
[:show, :edit, :update, :destroy]
Instance Method Summary collapse
-
#filter_params(attributes, name = Naming.new(self)) ⇒ void
Filter the parameters of the request.
-
#set_current_object(options, name = Naming.new(self)) ⇒ void
Set properly the instance variable from the model name.
-
#set_object_and_filter_params(*args) ⇒ void
Add to the instance of the controller: - One method to set properly the instance variable from the model name; - One method to filter the parameters of the request.
Instance Method Details
#filter_params(attributes, name = Naming.new(self)) ⇒ void
This method returns an undefined value.
Filter the parameters of the request.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/dryparams/dryable.rb', line 35 def filter_params(attributes, name = Naming.new(self)) if attributes.present? self.class_eval(<<-RUBY, 'dryable.rb', 37) def #{name.object}_params params.require(:#{name.object}).permit(*#{attributes}) end private :#{name.object}_params RUBY end end |
#set_current_object(options, name = Naming.new(self)) ⇒ void
This method returns an undefined value.
Set properly the instance variable from the model name.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/dryparams/dryable.rb', line 52 def set_current_object(, name = Naming.new(self)) if name.model = .with_indifferent_access methods = .slice(:only, :except).presence || { only: DEFAULT_ACTIONS } self.class_eval(<<-RUBY, 'dryable.rb', 59) def set_#{name.object} @#{name.object} = #{name.klass}.find(params[:id]) end private :set_#{name.object} before_action :set_#{name.object}, #{methods} RUBY end end |
#set_object_and_filter_params(*args) ⇒ void
This method returns an undefined value.
Add to the instance of the controller:
-
One method to set properly the instance variable from the model name;
-
One method to filter the parameters of the request.
23 24 25 26 27 28 |
# File 'lib/dryparams/dryable.rb', line 23 def set_object_and_filter_params(*args) name, = Naming.new(self), args. set_current_object(, name) filter_params(args, name) end |