Class: CafeCar::Navigation
- Inherits:
-
Object
- Object
- CafeCar::Navigation
- Defined in:
- lib/cafe_car/navigation.rb
Defined Under Namespace
Classes: Route
Instance Method Summary collapse
- #current ⇒ Object
-
#dashboard_href ⇒ Object
Path to the opt-in dashboard overview, or nil until a host writes the dashboard template (
app/views/cafe_car/dashboard/show.html.haml) — its existence is the opt-in. - #groups ⇒ Object
- #index_routes ⇒ Object
-
#initialize(template, **options) ⇒ Navigation
constructor
A new instance of Navigation.
- #link_to(*args, **opts, &block) ⇒ Object
- #named_routes ⇒ Object
- #recognize(obj) ⇒ Object
- #router ⇒ Object
- #routes ⇒ Object
Constructor Details
#initialize(template, **options) ⇒ Navigation
Returns a new instance of Navigation.
37 38 39 40 |
# File 'lib/cafe_car/navigation.rb', line 37 def initialize(template, **) @template = template @options = end |
Instance Method Details
#current ⇒ Object
79 |
# File 'lib/cafe_car/navigation.rb', line 79 def current = recognize(@template.request) |
#dashboard_href ⇒ Object
Path to the opt-in dashboard overview, or nil until a host writes the
dashboard template (app/views/cafe_car/dashboard/show.html.haml) — its
existence is the opt-in. The dashboard route lives in the engine (not the
host's routes), so it's resolved through the engine's own url helpers — that
works from any host controller regardless of where CafeCar is mounted.
52 53 54 55 |
# File 'lib/cafe_car/navigation.rb', line 52 def dashboard_href return unless @template.lookup_context.exists?("show", %w[cafe_car/dashboard], false) CafeCar::Engine.routes.url_helpers.dashboard_path end |
#groups ⇒ Object
57 |
# File 'lib/cafe_car/navigation.rb', line 57 def groups = routes.group_by(&:group) |
#index_routes ⇒ Object
56 |
# File 'lib/cafe_car/navigation.rb', line 56 def index_routes = named_routes.select(&:index?) |
#link_to(*args, **opts, &block) ⇒ Object
81 82 83 84 |
# File 'lib/cafe_car/navigation.rb', line 81 def link_to(*args, **opts, &block) block ||= -> { @template.tag.span(_1, class: ui_class([ :navigation, :link ], :current)) } @template.link_to_unless_current(*args, class: ui_class([ :navigation, :link ]), **opts, &block) end |
#named_routes ⇒ Object
45 |
# File 'lib/cafe_car/navigation.rb', line 45 def named_routes = Rails.application.routes.named_routes.to_h.values.map { Route.new(_1, template: @template) } |
#recognize(obj) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/cafe_car/navigation.rb', line 64 def recognize(obj, **) req = case obj when String path = ActionDispatch::Journey::Router::Utils.normalize_path(path) env = Rack::MockRequest.env_for(path, method: :get, **) ActionDispatch::Request.new(env) when ActionDispatch::Request then obj else raise "cannot recognize this obj" end router.recognize(req) do |route, params| return Route.new(route, template: @template) end end |
#router ⇒ Object
44 |
# File 'lib/cafe_car/navigation.rb', line 44 def router = Rails.application.routes.router |
#routes ⇒ Object
59 60 61 62 |
# File 'lib/cafe_car/navigation.rb', line 59 def routes @routes ||= index_routes.reject(&:rails?) .uniq(&:requirements) end |