Sha256: c786571c8cf2ae65dc75dbde900c3b8ecf41a94a9aced89e4e35213898dc292b

Contents?: true

Size: 1.51 KB

Versions: 51

Compression:

Stored size: 1.51 KB

Contents

class ForestLiana::Router
  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::ApplicationController.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'
          action = 'destroy'
        end

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

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
forest_liana-4.2.0 app/controllers/forest_liana/router.rb
forest_liana-4.1.3 app/controllers/forest_liana/router.rb
forest_liana-4.1.2 app/controllers/forest_liana/router.rb
forest_liana-4.1.1 app/controllers/forest_liana/router.rb
forest_liana-4.1.0 app/controllers/forest_liana/router.rb
forest_liana-4.0.2 app/controllers/forest_liana/router.rb
forest_liana-4.0.1 app/controllers/forest_liana/router.rb
forest_liana-4.0.0 app/controllers/forest_liana/router.rb
forest_liana-4.0.0.pre.beta.1 app/controllers/forest_liana/router.rb
forest_liana-3.3.0 app/controllers/forest_liana/router.rb
forest_liana-4.0.0.pre.beta.0 app/controllers/forest_liana/router.rb
forest_liana-3.2.0 app/controllers/forest_liana/router.rb
forest_liana-3.1.1 app/controllers/forest_liana/router.rb
forest_liana-3.1.0 app/controllers/forest_liana/router.rb
forest_liana-3.0.7 app/controllers/forest_liana/router.rb
forest_liana-3.0.6 app/controllers/forest_liana/router.rb
forest_liana-3.0.5 app/controllers/forest_liana/router.rb
forest_liana-3.0.4 app/controllers/forest_liana/router.rb
forest_liana-3.0.3 app/controllers/forest_liana/router.rb
forest_liana-3.0.2 app/controllers/forest_liana/router.rb