class SkinnyControllers::Operation::Base

An example Operation may looy like

module EventOperations

class Read < SkinnyControllers::Operation::Base
  def run
    model if allowed?
  end
end

end

TODO: make the above the 'default' and not require to be defined

Attributes

_lookup[RW]
action[RW]
association_name[RW]
authorized_via_parent[RW]
current_user[RW]
model_key[RW]
options[RW]
params[RW]
params_for_action[RW]

Public Class Methods

call(*args)

To support the shorthand ruby/block syntax e.g.: MyOperation.()

Alias for: run
new(current_user, controller_params, params_for_action = nil, action = nil, lookup = nil, options = {}) click to toggle source

TODO: too many optional parameters. Group into options hash

@param [Model] current_user the logged in user @param [Hash] controller_params the params hash raw from the controller @param [Hash] params_for_action optional params hash, generally the result of strong parameters @param [string] action the current action on the controller

# File lib/skinny_controllers/operation/base.rb, line 48
def initialize(current_user,
  controller_params, params_for_action = nil,
  action = nil,
  lookup = nil,
  options = {})

  self.authorized_via_parent = false
  self.current_user = current_user
  self.action = action || controller_params[:action]
  self.params = controller_params
  self.params_for_action = params_for_action || controller_params

  self._lookup = lookup
  self.options = options
  self.model_key = options[:model_params_key]
  self.association_name = options[:association_name]
end
run(*args) click to toggle source
# File lib/skinny_controllers/operation/base.rb, line 25
def run(*args)
  object = new(*args)
  object.run
end
Also aliased as: call

Public Instance Methods

allowed?() click to toggle source
# File lib/skinny_controllers/operation/base.rb, line 108
def allowed?
  allowed_for?(model)
end
allowed_for?(object) click to toggle source

checks the policy

# File lib/skinny_controllers/operation/base.rb, line 117
def allowed_for?(object)
  policy_for(object).send(policy_method_name)
end
association_name_from_object() click to toggle source

@example model_name == Namespace::Item

-> model_name.tableize == namespace/items
-> split.last == items

TODO: maybe make this configurable?

# File lib/skinny_controllers/operation/base.rb, line 95
def association_name_from_object
  association_name || model_name.tableize.split('/').last
end
call()

To support teh shorthand ruby/block syntax e.g.: MyOperation.new().()

Alias for: run
check_allowed!(*args) click to toggle source
# File lib/skinny_controllers/operation/base.rb, line 112
def check_allowed!(*args)
  raise DeniedByPolicy.new(*args.presence || action) unless allowed?
end
id_from_params() click to toggle source
# File lib/skinny_controllers/operation/base.rb, line 75
def id_from_params
  unless @id_from_params
    @id_from_params = params[:id]
    if filter = params[:filter]
      @id_from_params = filter[:id].split(',')
    end
  end

  @id_from_params
end
lookup() click to toggle source
# File lib/skinny_controllers/operation/base.rb, line 66
def lookup
  @lookup ||= begin
    _lookup || Lookup.from_operation(
      operation_class: self.class,
      model_class: options[:model_class]
    )
  end
end
policy_for(object) click to toggle source

@return a new policy object and caches it

# File lib/skinny_controllers/operation/base.rb, line 100
def policy_for(object)
  @policy ||= policy_class.new(
    current_user,
    object,
    authorized_via_parent: authorized_via_parent
  )
end
run() click to toggle source

To be overridden

# File lib/skinny_controllers/operation/base.rb, line 36
def run; end
Also aliased as: call