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

Instance Method Details

#filter_params(attributes, name = Naming.new(self)) ⇒ void

This method returns an undefined value.

Filter the parameters of the request.

Parameters:

  • attributes (Array)

    the attributes to permit in the request.

  • name (Naming) (defaults to: Naming.new(self))

    an instance of Naming class.



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.

Parameters:

  • options (HashWithIndifferentAccess, Hash)

    the option of the callback (:only, :except).

  • name (Naming) (defaults to: Naming.new(self))

    an instance of Naming class.



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(options, name = Naming.new(self))
  if name.model
    options = options.with_indifferent_access
    methods = options.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.

Parameters:

  • args (Array)

    the arguments of the generations methods.



23
24
25
26
27
28
# File 'lib/dryparams/dryable.rb', line 23

def set_object_and_filter_params(*args)
  name, options = Naming.new(self), args.extract_options!

  set_current_object(options, name)
  filter_params(args, name)
end