Module: Wallaby::Engineable

Included in:
ApplicationConcern, ApplicationHelper
Defined in:
lib/concerns/wallaby/engineable.rb

Overview

Engine related helper methods to be included for both controller and view.

Instance Method Summary collapse

Instance Method Details

#current_engineActionDispatch::Routing::RoutesProxy

This helper method returns the current Wallaby::Engine routing proxy. For example, if Wallaby is mounted at different paths at the same time:

mount Wallaby::Engine, at: '/admin'
mount Wallaby::Engine, at: '/inner', as: :inner_engine,
  defaults: { resources_controller: InnerController }

If ‘/inner` is current request path, it returns `inner_engine` engine proxy.

Returns:

  • (ActionDispatch::Routing::RoutesProxy)

    engine proxy for current request



15
16
17
# File 'lib/concerns/wallaby/engineable.rb', line 15

def current_engine
  @current_engine ||= try current_engine_name
end

#current_engine_nameString

Find out the Wallaby::Engine routing proxy name for the current request, it comes from either:

  • Current controller’s engine_name

  • Judge from the current request path (which contains the script and path info)

Returns:

  • (String)

    engine name for current request

See Also:



25
26
27
# File 'lib/concerns/wallaby/engineable.rb', line 25

def current_engine_name
  @current_engine_name ||= wallaby_controller.engine_name || EngineNameFinder.execute(request.path)
end

#current_engine_routeActionDispatch::Journey::Route

Returns engine route for current request.

Returns:

  • (ActionDispatch::Journey::Route)

    engine route for current request



30
31
32
33
34
# File 'lib/concerns/wallaby/engineable.rb', line 30

def current_engine_route
  return if current_engine_name.blank?

  Rails.application.routes.named_routes[current_engine_name]
end

#script_nameString

Note:

This script name prefix is required for Rails #url_for to generate the correct URL.

Returns current engine’s script name.

Returns:

  • (String)

    current engine’s script name



40
41
42
# File 'lib/concerns/wallaby/engineable.rb', line 40

def script_name
  current_engine_route.try { |route| route.path.spec.to_s }
end