Class: Avo::Sidebar::LinkComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/avo/sidebar/link_component.rb

Overview

A single sidebar link (leaf). ‘variant: :default` or `:subitem`. Knows nothing about menus; callers pass the path, label and resolved `active` (a match mode like :inclusive, or a boolean to force it).

Instance Method Summary collapse

Instance Method Details

#is_external?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/components/avo/sidebar/link_component.rb', line 48

def is_external?
  uri = URI(@path)
  # Paths without a scheme are always internal (relative app paths).
  return false if uri.scheme.blank?

  # A link with a scheme is internal only when its path is mounted under Avo.
  # Compare against the URI path component, not the whole URL string, otherwise
  # hosts like "avohq.io" falsely match a "/avo" mount path (via "//avohq.io").
  # Use root_path_without_url so prefix_path + mount_path setups are recognized.
  !uri.path.start_with?(helpers.root_path_without_url)
rescue URI::InvalidURIError
  true
end

Backwards compatibility with ViewComponent 3.x



68
69
70
71
72
73
74
# File 'app/components/avo/sidebar/link_component.rb', line 68

def link_caller
  if Gem::Version.new(ViewComponent::VERSION::STRING) >= Gem::Version.new("4.0.0")
    helpers
  else
    self
  end
end


42
43
44
45
46
# File 'app/components/avo/sidebar/link_component.rb', line 42

def link_data
  return @data if @hotkey.blank?

  @data.merge(hotkey: @hotkey)
end


76
77
78
# File 'app/components/avo/sidebar/link_component.rb', line 76

def link_icon
  @icon
end

For external links active_link_to marks them all as active.



63
64
65
# File 'app/components/avo/sidebar/link_component.rb', line 63

def link_method
  is_external? ? :link_to : :active_link_to
end

#root_css_classObject



29
30
31
32
33
# File 'app/components/avo/sidebar/link_component.rb', line 29

def root_css_class
  return "sidebar-link" unless subitem?

  ["sidebar-subitem", @bar_class].reject(&:blank?).join(" ")
end

#show_icon?Boolean

Sub-items don’t show icons (for now); top-level links do.

Returns:

  • (Boolean)


36
37
38
39
40
# File 'app/components/avo/sidebar/link_component.rb', line 36

def show_icon?
  return false if subitem?

  @reserve_icon_space || link_icon.present?
end

#subitem?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/components/avo/sidebar/link_component.rb', line 25

def subitem?
  @variant == :subitem
end