Class: Resources::Manager

Inherits:
Object
  • Object
show all
Includes:
Routes
Defined in:
lib/resources/manager.rb

Instance Method Summary collapse

Methods included from Routes

#current_path, #current_route, #named_routes, #path_parameters, #router, #routes

Constructor Details

#initialize(controller, request, *args) ⇒ Manager

Returns a new instance of Manager.



5
6
7
8
9
# File 'lib/resources/manager.rb', line 5

def initialize controller, request, *args
  @controller = controller
  @request = request
  @settings = @controller.class.resource_configuration
end

Instance Method Details

#build_resourceObject



75
76
77
# File 'lib/resources/manager.rb', line 75

def build_resource
  resource_class.new()
end

#pagination?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/resources/manager.rb', line 28

def pagination?
  settings.pagination.is_a?(Proc) ? settings.pagination.call(params, controller) : settings.pagination
end

#paramsObject



59
60
61
# File 'lib/resources/manager.rb', line 59

def params
  controller.params
end

#params_pageObject



67
68
69
# File 'lib/resources/manager.rb', line 67

def params_page
  @params_page ||= option_with_params(:params_page)
end

#params_resourceObject



71
72
73
# File 'lib/resources/manager.rb', line 71

def params_resource
  @params_resource ||= option_with_params(:params_resource)
end

#params_searchObject



63
64
65
# File 'lib/resources/manager.rb', line 63

def params_search
  @params_search ||= option_with_params(:params_search)
end

#resourceObject



45
46
47
48
49
50
51
52
53
# File 'lib/resources/manager.rb', line 45

def resource
  @resource ||=
    case controller.action_name
    when "new", "create"
      build_resource
    else
      resource_scope.where("#{resource_class.table_name}.id = ?",params[:id]).first rescue nil
    end
end

#resource_classObject



11
12
13
# File 'lib/resources/manager.rb', line 11

def resource_class
  @resource_class_name ||= settings.resource_class_name.to_s.safe_constantize
end

#resource_scopeObject



32
33
34
# File 'lib/resources/manager.rb', line 32

def resource_scope
  @resource_scope = option_with_scope(:resource_scope)
end

#resourcesObject



41
42
43
# File 'lib/resources/manager.rb', line 41

def resources
  @resources ||= settings.search ? resources_search.result(settings.search_options) : resources_scope
end

#resources_scopeObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/resources/manager.rb', line 15

def resources_scope
  scope = option_with_scope(:resources_scope)
  @resources_scope =
    case
    when pagination?
      scope = scope.page(params_page) if scope.respond_to?(:page)
      scope = scope.paginate(page: params_page) if scope.respond_to?(:paginate)
      scope
    else
      scope
    end
end

#resources_searchObject



36
37
38
# File 'lib/resources/manager.rb', line 36

def resources_search
  @resources_search ||= resources_scope.search(params_search) rescue nil
end

#settingsObject



55
56
57
# File 'lib/resources/manager.rb', line 55

def settings
  @settings
end