Module: Studio::SidebarSections
- Defined in:
- lib/studio/sidebar_sections.rb
Class Method Summary collapse
- .profile_link?(view) ⇒ Boolean
- .resolve(declared, view) ⇒ Object
-
.standard(view) ⇒ Object
What the ENGINE puts in every app's sidebar, ahead of whatever the host declared.
- .symbolize(hash) ⇒ Object
Class Method Details
.profile_link?(view) ⇒ Boolean
65 66 67 68 69 70 71 72 |
# File 'lib/studio/sidebar_sections.rb', line 65 def profile_link?(view) return false unless defined?(Studio) && Studio.respond_to?(:draw_profile_routes) return false unless Studio.draw_profile_routes return false unless view.respond_to?(:profile_path) return false unless view.respond_to?(:logged_in?) && view.logged_in? true end |
.resolve(declared, view) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/studio/sidebar_sections.rb', line 27 def resolve(declared, view) sections = declared.respond_to?(:call) ? declared.call(view) : declared admin = view.respond_to?(:admin?) && view.admin? (standard(view) + Array(sections)) .map { |section| symbolize(section) } .reject { |section| section[:admin] && !admin } .map { |section| section.merge(links: Array(section[:links]).map { |link| symbolize(link) }) } end |
.standard(view) ⇒ Object
What the ENGINE puts in every app's sidebar, ahead of whatever the host declared. The engine ships /profile, so it ships the way in — otherwise every consumer writes the same entry and they drift in wording and emoji.
PREPENDED, not appended: it is the viewer's own account, the thing nearest to them, and the operator asked for it at the top.
TWO GATES, and both are load-bearing:
* the route must be drawn. An app that set draw_profile_routes = false
would otherwise be handed a menu item pointing at a route that does not
exist — a 404 from its own navigation.
* the viewer must be signed in. /profile requires authentication, so
offering it to a signed-out visitor bounces them to the login page from
something that looked like navigation. Same shape as the `admin:` rule
below, which already drops sections a viewer may not see.
A view that answers neither predicate (a bare context in a unit test) gets nothing, which is the safe direction: a missing link is visible and fixable, a link to nowhere is the bug.
56 57 58 59 60 61 62 63 |
# File 'lib/studio/sidebar_sections.rb', line 56 def standard(view) return [] unless profile_link?(view) [{ title: "You", links: [ { label: "Profile", href: view.profile_path, emoji: "👤", desc: "Your name, email and photo" } ] }] end |
.symbolize(hash) ⇒ Object
74 75 76 |
# File 'lib/studio/sidebar_sections.rb', line 74 def symbolize(hash) hash.to_h.each_with_object({}) { |(key, value), out| out[key.to_sym] = value } end |