Class: ForemanDatacenter::ApplicationController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/foreman_datacenter/application_controller.rb

Instance Method Summary collapse

Instance Method Details

#action_permissionObject

def resource_base_search_and_page(tables = [])

base = tables.empty? ? resource_base_with_search : resource_base_with_search.eager_load(*tables)
base.paginate(:page => params[:page], :per_page => params[:per_page])

end



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/foreman_datacenter/application_controller.rb', line 73

def action_permission
  case params[:action]
    when 'new_connection'
      'new_connection'
    when 'connect'
      'connect'
    when 'planned'
      'planned'
    when 'connected'
      'connected'
    when 'disconnect'
      'disconnect'
    when 'populate_new'
      'populate_new'
    when 'depopulate'
      'depopulate'
    when 'populate'
      'populate'
    when 'move'
      'move'
    when 'racks'
      'racks'
    else
      super
  end
end

#controller_permissionObject



29
30
31
# File 'app/controllers/foreman_datacenter/application_controller.rb', line 29

def controller_permission
  controller_name
end

#find_resourceObject



14
15
16
17
# File 'app/controllers/foreman_datacenter/application_controller.rb', line 14

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

#not_found(exception = nil) ⇒ Object



5
6
7
8
9
10
11
12
# File 'app/controllers/foreman_datacenter/application_controller.rb', line 5

def not_found(exception = nil)
  logger.debug "not found: #{exception}" if exception
  respond_to do |format|
    format.html { render "foreman_datacenter/common/404", :status => :not_found }
    format.any { head :not_found}
  end
  true
end

#resource_classObject

Raises:

  • (NameError)


37
38
39
40
41
# File 'app/controllers/foreman_datacenter/application_controller.rb', line 37

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



61
62
63
64
65
66
# File 'app/controllers/foreman_datacenter/application_controller.rb', line 61

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

#resource_finder(scope, id) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


19
20
21
22
23
24
25
26
27
# File 'app/controllers/foreman_datacenter/application_controller.rb', line 19

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



33
34
35
# File 'app/controllers/foreman_datacenter/application_controller.rb', line 33

def resource_name(resource = controller_name)
  resource.singularize
end

#resource_scope(options = {}) ⇒ Object



43
44
45
# File 'app/controllers/foreman_datacenter/application_controller.rb', line 43

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

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



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/foreman_datacenter/application_controller.rb', line 47

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