Class: Api::V2::ForemanDatacenter::BaseController

Inherits:
V2::BaseController
  • Object
show all
Defined in:
app/controllers/api/v2/foreman_datacenter/base_controller.rb

Instance Method Summary collapse

Instance Method Details

#action_permissionObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/api/v2/foreman_datacenter/base_controller.rb', line 59

def action_permission
  case params[:action]
    when 'connect'
      'connect'
    when 'planned'
      'planned'
    when 'connected'
      'connected'
    when 'disconnect'
      'disconnect'
    else
      super
  end
end

#controller_permissionObject



20
21
22
# File 'app/controllers/api/v2/foreman_datacenter/base_controller.rb', line 20

def controller_permission
  controller_name
end

#find_resourceObject



5
6
7
8
# File 'app/controllers/api/v2/foreman_datacenter/base_controller.rb', line 5

def find_resource
  instance_variable_set("@#{resource_name}",
                        resource_finder(resource_scope, params[:id]))
end

#resource_classObject

Raises:

  • (NameError)


28
29
30
31
32
# File 'app/controllers/api/v2/foreman_datacenter/base_controller.rb', line 28

def resource_class
  @resource_class ||= resource_class_for(resource_name)
  raise NameError, "Could not find resource class for resource #{resource_name}" if @resource_class.nil?
  @resource_class
end

#resource_class_for(resource) ⇒ Object



52
53
54
55
56
57
# File 'app/controllers/api/v2/foreman_datacenter/base_controller.rb', line 52

def resource_class_for(resource)
  klass = "ForemanDatacenter::#{resource.classify}".constantize
  klass
rescue NameError
  nil
end

#resource_finder(scope, id) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


10
11
12
13
14
15
16
17
18
# File 'app/controllers/api/v2/foreman_datacenter/base_controller.rb', line 10

def resource_finder(scope, id)
  raise ActiveRecord::RecordNotFound if scope.empty?
  result = scope.from_param(id) if scope.respond_to?(:from_param)
  begin
    result ||= scope.friendly.find(id) if scope.respond_to?(:friendly)
  rescue ActiveRecord::RecordNotFound
  end
  result || scope.find(id)
end

#resource_name(resource = controller_name) ⇒ Object



24
25
26
# File 'app/controllers/api/v2/foreman_datacenter/base_controller.rb', line 24

def resource_name(resource = controller_name)
  resource.singularize
end

#resource_scope(options = {}) ⇒ Object



34
35
36
# File 'app/controllers/api/v2/foreman_datacenter/base_controller.rb', line 34

def resource_scope(options = {})
  @resource_scope ||= scope_for(resource_class, options)
end

#scope_for(resource, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/api/v2/foreman_datacenter/base_controller.rb', line 38

def scope_for(resource, options = {})
  controller = options.delete(:controller){ controller_permission }
  # don't call the #action_permission method here, we are not sure if the resource is authorized at this point
  # calling #action_permission here can cause an exception, in order to avoid this, ensure :authorized beforehand
  permission = options.delete(:permission)

  if resource.respond_to?(:authorized)
    permission ||= "#{action_permission}_#{controller}"
    resource = resource.authorized(permission, resource)
  end

  resource.where(options)
end