Class: Panda::CMS::MenuComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- Panda::CMS::MenuComponent
- Defined in:
- app/components/panda/cms/menu_component.rb
Instance Method Summary collapse
- #active_link?(path, match: :starts_with) ⇒ Boolean
-
#initialize(name:, current_path: "", styles: {}, overrides: {}, render_page_menu: false, page_menu_styles: {}) ⇒ void
constructor
Renders the menu item and its children.
- #is_active?(menu_item) ⇒ Boolean
Constructor Details
#initialize(name:, current_path: "", styles: {}, overrides: {}, render_page_menu: false, page_menu_styles: {}) ⇒ void
Renders the menu item and its children
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'app/components/panda/cms/menu_component.rb', line 16 def initialize(name:, current_path: "", styles: {}, overrides: {}, render_page_menu: false, page_menu_styles: {}) @menu = Panda::CMS::Menu.find_by(name: name) @menu_items = @menu. @menu_items = @menu_items.where("depth <= ?", @menu.depth) if @menu.depth @menu_items = @menu_items.order(:lft) @current_path = current_path.to_s @render_page_menu = @menu_items = @menu_items.order(:lft).map do || if is_active?() .define_singleton_method(:css_classes) { "#{styles[:default]} #{styles[:active]}" } else .define_singleton_method(:css_classes) { "#{styles[:default]} #{styles[:inactive]}" } end end # TODO: Surely don't need this but Current.page isn't working in the component return unless @render_page_menu @current_page = Panda::CMS::Page.find_by(path: @current_path) @page_menu_styles = end |
Instance Method Details
#active_link?(path, match: :starts_with) ⇒ Boolean
48 49 50 51 52 53 54 55 56 |
# File 'app/components/panda/cms/menu_component.rb', line 48 def active_link?(path, match: :starts_with) if match == :starts_with return @current_path.starts_with?(path) elsif match == :exact return (@current_path == path) end false end |
#is_active?(menu_item) ⇒ Boolean
41 42 43 44 45 46 |
# File 'app/components/panda/cms/menu_component.rb', line 41 def is_active?() return true if @current_path == "/" && active_link?(.page.path, match: :exact) return true if .page.path != "/" && active_link?(.page.path, match: :starts_with) false end |