Class: Uchi::Routes
- Inherits:
-
Object
- Object
- Uchi::Routes
- Defined in:
- lib/uchi/routes.rb
Instance Method Summary collapse
- #draw_repository_routes(routes, at: default_at, &block) ⇒ Object
- #draw_root_route(routes, repository:, at: default_at) ⇒ Object
-
#mount(host_routes, at: default_at) { ... } ⇒ Object
Mounts the Uchi engine routes onto the host application's routes.
-
#mount_as ⇒ Object
Returns the name to use when generating routing helper method names.
-
#mount_at ⇒ Object
Returns the path prefix for the routes, i.e.
-
#path_to(name, script_name: "", **options) ⇒ Object
Generates a path to a named route inside the Uchi engine, e.g.
Instance Method Details
#draw_repository_routes(routes, at: default_at, &block) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/uchi/routes.rb', line 41 def draw_repository_routes(routes, at: default_at, &block) repositories = Uchi::Repository.all repositories.each do |repository_class| resources_name = repository_class.controller_name routes.namespace(at, as: mount_as) do routes.resources(resources_name) end end routes.namespace(at, as: mount_as, &block) if block draw_root_route(routes, at: at, repository: repositories.first) unless routes.has_named_route?(root_route_name) end |
#draw_root_route(routes, repository:, at: default_at) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/uchi/routes.rb', line 33 def draw_root_route(routes, repository:, at: default_at) return unless repository routes.namespace(at, as: mount_as) do routes.root to: "#{repository.controller_name}#index" end end |
#mount(host_routes, at: default_at) { ... } ⇒ Object
Mounts the Uchi engine routes onto the host application's routes.
Example usage in host application's routes.rb that install Uchi at /uchi:
Rails.application.routes.draw do Uchi.routes.mount(self) end
application's routes mapper.
specific repository at the root URL:
Uchi.routes.mount(self) do root to: "projects#index" end
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/uchi/routes.rb', line 22 def mount(host_routes, at: default_at, &block) @mount_at = (at || default_at).to_sym host_routes.mount( Uchi::Engine, as: mount_as, at: mount_at ) draw_repository_routes(host_routes, at: mount_at, &block) end |
#mount_as ⇒ Object
Returns the name to use when generating routing helper method names
57 58 59 |
# File 'lib/uchi/routes.rb', line 57 def mount_as :uchi end |
#mount_at ⇒ Object
Returns the path prefix for the routes, i.e. the first URL segment where Uchi can be requested.
63 64 65 |
# File 'lib/uchi/routes.rb', line 63 def mount_at @mount_at ||= default_at end |
#path_to(name, script_name: "", **options) ⇒ Object
Generates a path to a named route inside the Uchi engine, e.g. :search
for search_path.
This calls the engine's own url helpers directly, with an explicit
script_name, instead of going through the uchi routes proxy that Rails
generates for the mount (e.g. helpers.uchi.search_path).
That proxy derives the script name by combining the current request's
script name with the mount's, a calculation that breaks down when Uchi is
mounted at a path with more than one segment (e.g. at: "admin/uchi") and
the current request isn't itself routed through the engine (which is the
common case since repository controllers are drawn directly into the host
application's routes).
request.script_name. Needed so links keep working when the host
application itself is served from a sub-URI, e.g. Rack SCRIPT_NAME.
Defaults to "" so callers without a request in scope still work.
For requests already routed through the Uchi engine itself (e.g. the
search page), request.script_name already includes the mount, so it's
stripped back off here before being re-added, to avoid ending up with it
twice, e.g. "/admin/uchi/admin/uchi".
90 91 92 93 94 95 96 97 98 |
# File 'lib/uchi/routes.rb', line 90 def path_to(name, script_name: "", **) host_script_name = script_name.delete_suffix("/#{mount_at}") Uchi::Engine.routes.url_helpers.public_send( "#{name}_path", **, script_name: "#{host_script_name}/#{mount_at}" ) end |