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
- #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
69 |
# File 'lib/cafe_car/navigation.rb', line 69 def current = recognize(@template.request) |
#groups ⇒ Object
47 |
# File 'lib/cafe_car/navigation.rb', line 47 def groups = routes.group_by(&:group) |
#index_routes ⇒ Object
46 |
# File 'lib/cafe_car/navigation.rb', line 46 def index_routes = named_routes.select(&:index?) |
#link_to(*args, **opts, &block) ⇒ Object
71 72 73 74 |
# File 'lib/cafe_car/navigation.rb', line 71 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
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cafe_car/navigation.rb', line 54 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
49 50 51 52 |
# File 'lib/cafe_car/navigation.rb', line 49 def routes @routes ||= index_routes.reject(&:rails?) .uniq(&:requirements) end |