Class: Api::V2::ForemanDatacenter::BaseController
- Inherits:
-
V2::BaseController
- Object
- V2::BaseController
- Api::V2::ForemanDatacenter::BaseController
show all
- Defined in:
- app/controllers/api/v2/foreman_datacenter/base_controller.rb
Direct Known Subclasses
ConsolePortTemplatesController, ConsolePortsController, ConsoleServerPortTemplatesController, ConsoleServerPortsController, DeviceBayTemplatesController, DeviceBaysController, DeviceInterfaceConnectionsController, DeviceInterfacesController, DeviceModulesController, DeviceRolesController, DeviceTypesController, DevicesController, InterfaceTemplatesController, ManagementDevicesController, ManufacturersController, PlatformsController, PowerOutletTemplatesController, PowerOutletsController, PowerPortTemplatesController, PowerPortsController, RackGroupsController, RacksController, SitesController
Instance Method Summary
collapse
Instance Method Details
#action_permission ⇒ Object
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_permission ⇒ Object
20
21
22
|
# File 'app/controllers/api/v2/foreman_datacenter/base_controller.rb', line 20
def controller_permission
controller_name
end
|
#find_resource ⇒ Object
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_class ⇒ Object
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
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 }
permission = options.delete(:permission)
if resource.respond_to?(:authorized)
permission ||= "#{action_permission}_#{controller}"
resource = resource.authorized(permission, resource)
end
resource.where(options)
end
|