Class: SkinnyControllers::Operation::Base

Inherits:
Object
  • Object
show all
Includes:
ModelHelpers
Defined in:
lib/skinny_controllers/operation/base.rb

Overview

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

Direct Known Subclasses

Default

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ModelHelpers

#find_model, #model, #model_from_id, #model_from_named_id, #model_from_params, #model_from_parent, #model_from_scope, #model_param_name, #model_params, #sanitized_params, #scoped_model

Constructor Details

#initialize(current_user, controller_params, params_for_action = nil, action = nil) ⇒ Base

Returns a new instance of Base.

Parameters:

  • current_user (Model)

    the logged in user

  • controller_params (Hash)

    the params hash raw from the controller

  • params_for_action (Hash) (defaults to: nil)

    optional params hash, generally the result of strong parameters

  • action (string) (defaults to: nil)

    the current action on the controller



29
30
31
32
33
34
35
# File 'lib/skinny_controllers/operation/base.rb', line 29

def initialize(current_user, controller_params, params_for_action = nil, action = nil)
  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
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



18
19
20
# File 'lib/skinny_controllers/operation/base.rb', line 18

def action
  @action
end

#authorized_via_parentObject

Returns the value of attribute authorized_via_parent.



18
19
20
# File 'lib/skinny_controllers/operation/base.rb', line 18

def authorized_via_parent
  @authorized_via_parent
end

#current_userObject

Returns the value of attribute current_user.



18
19
20
# File 'lib/skinny_controllers/operation/base.rb', line 18

def current_user
  @current_user
end

#paramsObject

Returns the value of attribute params.



18
19
20
# File 'lib/skinny_controllers/operation/base.rb', line 18

def params
  @params
end

#params_for_actionObject

Returns the value of attribute params_for_action.



18
19
20
# File 'lib/skinny_controllers/operation/base.rb', line 18

def params_for_action
  @params_for_action
end

Class Method Details

.run(current_user, params) ⇒ Object



20
21
22
23
# File 'lib/skinny_controllers/operation/base.rb', line 20

def self.run(current_user, params)
  object = new(current_user, params)
  object.run
end

Instance Method Details

#allowed?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/skinny_controllers/operation/base.rb', line 82

def allowed?
  allowed_for?(model)
end

#allowed_for?(object) ⇒ Boolean

checks the policy

Returns:

  • (Boolean)


87
88
89
# File 'lib/skinny_controllers/operation/base.rb', line 87

def allowed_for?(object)
  policy_for(object).send(policy_method_name)
end

#association_name_from_objectObject



56
57
58
# File 'lib/skinny_controllers/operation/base.rb', line 56

def association_name_from_object
  model_name.tableize
end

#id_from_paramsObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/skinny_controllers/operation/base.rb', line 37

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

#model_classObject



48
49
50
# File 'lib/skinny_controllers/operation/base.rb', line 48

def model_class
  @model_class ||= Lookup::Model.class_from_operation(self.class.name)
end

#model_nameObject



52
53
54
# File 'lib/skinny_controllers/operation/base.rb', line 52

def model_name
  @object_type_name ||= Lookup::Model.name_from_operation(self.class.name)
end

#policy_classObject

Takes the class name of self and converts it to a Policy class name

Examples:

In Operation::Event::Read, Policy::EventPolicy is returned




63
64
65
# File 'lib/skinny_controllers/operation/base.rb', line 63

def policy_class
  @policy_class ||= Lookup::Policy.class_from_model(model_name)
end

#policy_for(object) ⇒ Object

Returns a new policy object and caches it.

Returns:

  • a new policy object and caches it



75
76
77
78
79
80
# File 'lib/skinny_controllers/operation/base.rb', line 75

def policy_for(object)
  @policy ||= policy_class.new(
    current_user,
    object,
    authorized_via_parent: authorized_via_parent)
end

#policy_method_nameObject

Converts the class name to the method name to call on the policy

Examples:

Operation::Event::Read would become read?




70
71
72
# File 'lib/skinny_controllers/operation/base.rb', line 70

def policy_method_name
  @policy_method_name ||= Lookup::Policy.method_name_for_operation(self.class.name)
end