Class: ForestLiana::Router

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/forest_liana/router.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/forest_liana/router.rb', line 2

def call(env)
  params = env['action_dispatch.request.path_parameters']
  collection_name = params[:collection]
  resource = ForestLiana::SchemaUtils.find_model_from_collection_name(collection_name, true)

  if resource.nil?
    FOREST_LOGGER.error "Routing error: Resource not found for collection #{collection_name}."
    FOREST_LOGGER.error "If this is a Smart Collection, please ensure your Smart Collection routes are defined before the mounted ForestLiana::Engine?"
    ForestLiana::BaseController.action(:route_not_found).call(env)
  else
    begin
      component_prefix = ForestLiana.component_prefix(resource)
      controller_name = "#{component_prefix}Controller"

      controller = "ForestLiana::UserSpace::#{controller_name}".constantize
      action = nil

      case env['REQUEST_METHOD']
      when 'GET'
        if params[:id]
          action = 'show'
        elsif env['PATH_INFO'] == "/#{collection_name}/count"
          action = 'count'
        else
          action = 'index'
        end
      when 'PUT'
        action = 'update'
      when 'POST'
        action = 'create'
      when 'DELETE'
        if params[:id]
          action = 'destroy'
        else
          action = 'destroy_bulk'
        end
      end

      controller.action(action.to_sym).call(env)
    rescue NoMethodError => exception
      FOREST_LOGGER.error "Routing error: #{exception}\n#{exception.backtrace.join("\n\t")}"
      ForestLiana::BaseController.action(:route_not_found).call(env)
    end
  end
end