Class: Wallaby::EngineUrlFor
- Inherits:
-
Object
- Object
- Wallaby::EngineUrlFor
- 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
- #context ⇒ ActionController::Base, ActionView::Base
- #options ⇒ Hash
- #params ⇒ Hash, ActionController::Parameters
Class Method Summary collapse
-
.execute(context:, params:, options:) ⇒ nil
Generate the proper URL depending on the context.
Instance Method Summary collapse
Instance Attribute Details
#context ⇒ ActionController::Base, ActionView::Base
42 43 44 |
# File 'lib/services/wallaby/engine_url_for.rb', line 42 def context @context end |
#options ⇒ Hash
45 46 47 |
# File 'lib/services/wallaby/engine_url_for.rb', line 45 def @options end |
#params ⇒ 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
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: ).execute end |
Instance Method Details
#execute ⇒ String?
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 |