module SkinnyControllers::Diet

Constants

ALLOWED_OPTIONS

Attributes

options[RW]

Public Class Methods

skinny_controllers_config(options = {}) click to toggle source
# File lib/skinny_controllers/diet.rb, line 16
def skinny_controllers_config(options = {})
  @options = options.select { |o| ALLOWED_OPTIONS.include?(o) }
end

Public Instance Methods

_lookup() click to toggle source
# File lib/skinny_controllers/diet.rb, line 58
def _lookup
  @_lookup ||= Lookup.from_controller(
    controller_class: self.class,
    verb:             verb_for_action,
    model_class:      _options[:model_class]
  )
end
_options() click to toggle source
# File lib/skinny_controllers/diet.rb, line 54
def _options
  self.class.options || {}
end
create_operation(user:, params_for_action: nil) click to toggle source
# File lib/skinny_controllers/diet.rb, line 22
def create_operation(user:, params_for_action: nil)
  operation_class.new(
    user,
    params,
    params_for_action,
    action_name,
    _lookup,
    _options
  )
end
model() click to toggle source

abstraction for `operation.run` useful when there is no logic needed for deciding what to do with an operation or if there is no logic to decide which operation to use

@return [ActiveRecord::Base] the model

# File lib/skinny_controllers/diet.rb, line 72
def model
  @model ||= operation.run
end
operation() click to toggle source

TODO: what if we want multiple operations per action?

@return an instance of the operation with default parameters

# File lib/skinny_controllers/diet.rb, line 36
def operation
  @operation ||= operation_class.new(
    current_user,
    params, params_for_action,
    action_name,
    _lookup,
    _options
  )
end
operation_class() click to toggle source

Assumes the operation name from the controller name

@example SomeObjectsController => Operation::SomeObject::Action @return [Class] the operation class for the model and verb

# File lib/skinny_controllers/diet.rb, line 50
def operation_class
  _lookup.operation_class
end

Private Instance Methods

lookup_params_for_action(lookups) click to toggle source
# File lib/skinny_controllers/diet.rb, line 119
def lookup_params_for_action(lookups)
  lookups.each do |method_name|
    return send(method_name) if respond_to?(method_name, true)
  end

  params
end
params_for_action() click to toggle source

In order of most specific, to least specific:

  • {action}_{model_name}_params

  • {action}_params

  • {model_key}_params

  • resource_params

  • params

It's recommended to use whitelisted strong parameters on actions such as create and update

@return the whitelisted strong parameters object

# File lib/skinny_controllers/diet.rb, line 89
def params_for_action
  return {} if action_name == 'destroy'

  key = _options[:model_params_key]
  # model_class should be a class
  klass = _options[:model_class]

  model_key =
    if key.present?
      key
    elsif klass
      klass.name.underscore
    else
      _lookup.model_name.underscore
    end

  params_lookups = [
    # e.g.: create_post_params
    "#{action_name}_#{model_key}_params",
    # generic for action
    "#{action_name}_params",
    # e.g.: post_params
    "#{model_key}_params",
    # most generic
    'resource_params'
  ]

  lookup_params_for_action(params_lookups)
end
verb_for_action() click to toggle source

action name is inherited from ActionController::Base www.rubydoc.info/docs/rails/2.3.8/ActionController%2FBase%3Aaction_name

# File lib/skinny_controllers/diet.rb, line 130
def verb_for_action
  SkinnyControllers.action_map[action_name] ||
    (action_name && action_name.classify) ||
    SkinnyControllers.action_map['default']
end