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 = grape? ? @controller.resource_configuration : @controller.class.resource_configuration
end

Instance Method Details

#build_resourceObject



83
84
85
# File 'lib/resources/manager.rb', line 83

def build_resource
  resource_class.new()
end

#grape?Boolean

Returns:

  • (Boolean)


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

def grape?
  defined?(Grape) && !(controller.class < ActionController::Base)
end

#pagination?Boolean

Returns:

  • (Boolean)


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

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

#paramsObject



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

def params
  grape? ? request.params : controller.params
end

#params_pageObject



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

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

#params_resourceObject



79
80
81
# File 'lib/resources/manager.rb', line 79

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

#params_searchObject



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

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

#resourceObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/resources/manager.rb', line 49

def resource
  if grape?
    @resource ||= params[:id].blank? ? build_resource : (resource_scope.find(params[:id]) rescue nil )
  else
    @resource ||=
      case controller.action_name
      when "new", "create"
        build_resource
      else
        resource_scope.find(params[:id]) rescue nil
      end
  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



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

def resource_scope
  @resource_scope = option_with_scope(:resource_scope)
end

#resourcesObject



45
46
47
# File 'lib/resources/manager.rb', line 45

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



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

def resources_search
  @resources_search ||= resources_scope.search(params_search) if resources_scope.respond_to?(:search)
end

#settingsObject



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

def settings
  @settings
end