Class: Wallaby::EngineUrlFor

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/services/wallaby/engine_url_for.rb

Overview

URL service object for Urlable#url_for helper

Since Engine‘s routes are declared in Rack application fashion via ResourcesRouter to recoganize path in the pattern of `/:mount_path/:resources`. It means when the current request path (e.g. `/admin/categories`) is under the same mount path of Engine (e.g. `/admin`), using the original Rails usl_for (e.g. `url_for action: :index`) without providing the `:resources` param and script name will lead to an ActionController::RoutingError exception.

To generate the proper URL from given params and options for this kind of requests, there are three kinds of scenarios that need to be considered (assume that Engine is mounted at ‘/admin`):

  • if the URL to generate is a regular route defined before mounting the Engine that does not override the resources ‘categories` routes handled by Engine, such as:

    namespace :admin do
      resources :custom_categories
    end
    wallaby_mount at: '/admin'
    
  • if the URL to generate is a route that overrides the existing Engine route (assume that ‘categories` is one of the resources handled by Engine):

    namespace :admin do
      resources :categories
    end
    wallaby_mount at: '/admin'
    
  • regular resources handled by ResourcesRouter, e.g. (‘/admin/products`)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextActionController::Base, ActionView::Base

Returns:

  • (ActionController::Base, ActionView::Base)


42
43
44
# File 'lib/services/wallaby/engine_url_for.rb', line 42

def context
  @context
end

#optionsHash

Returns:

  • (Hash)


45
46
47
# File 'lib/services/wallaby/engine_url_for.rb', line 45

def options
  @options
end

#paramsHash, ActionController::Parameters

Returns:

  • (Hash, ActionController::Parameters)


48
49
50
# File 'lib/services/wallaby/engine_url_for.rb', line 48

def params
  @params
end

Class Method Details

.execute(context:, params:, options:) ⇒ nil

Generate the proper URL depending on the context

Parameters:

  • context (ActionController::Base, ActionView::Base)
  • params (Hash, ActionController::Parameters)
  • options (Hash)
  • model_class (Hash)

    a customizable set of options

  • with_query (Hash)

    a customizable set of options

Returns:

  • (nil)

    nil if params is not a Hash or ActionController::Parameters

See Also:



58
59
60
61
62
# File 'lib/services/wallaby/engine_url_for.rb', line 58

def self.execute(context:, params:, options:)
  return unless params.is_a?(Hash) || params.try(:permitted?)

  new(context: context, params: params, options: options).execute
end

Instance Method Details

#executeString?

Returns:

  • (String)

    URL

  • (nil)

    nil if current request is not under any mounted Wallaby::Engine

See Also:



67
68
69
70
71
72
# File 'lib/services/wallaby/engine_url_for.rb', line 67

def execute
  return if context.current_engine_route.blank?
  return custom_app_route.url if custom_app_route.exist?

  engine_route.url
end