Class: Alchemy::Configurations::MenuItem

Inherits:
Alchemy::Configuration show all
Defined in:
lib/alchemy/configurations/menu_item.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Alchemy::Configuration

#==, configuration, defined_configurations, defined_options, defined_values, #fetch, #hash, #initialize, option, #set, #set_from_yaml, #show, #to_h

Constructor Details

This class inherits a constructor from Alchemy::Configuration

Class Method Details

.from_legacy_hash(hash) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/alchemy/configurations/menu_item.rb', line 43

def self.from_legacy_hash(hash)
  hash = hash.to_h.deep_stringify_keys
  item = new(
    from_legacy_navigation(hash["navigation"] || {}, engine_name: hash["engine_name"]).merge(
      "name" => hash["name"]
    ).compact
  )
  # +register_module+ is deprecated already
  item.send(:set_position, hash["position"]) if hash["position"]
  item
end

.index_for_legacy_position(position) ⇒ Object

Positions sort entries around the default menu's entries, which use the positions 1 to 7, so 4.1 maps to the index before Sites.



57
58
59
# File 'lib/alchemy/configurations/menu_item.rb', line 57

def self.index_for_legacy_position(position)
  [position.ceil - 1, 0].max
end

Instance Method Details

#active?(view_context) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/alchemy/configurations/menu_item.rb', line 101

def active?(view_context)
  current?(view_context) || children.any? { |child| child.active?(view_context) }
end

#current?(view_context) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/alchemy/configurations/menu_item.rb', line 105

def current?(view_context)
  current_path = view_context.request.path
  path_matches = if match_path
    match_path.match?(current_path)
  elsif (own_path = local_path(view_context)&.chomp("/"))
    current_path.chomp("/") == own_path || current_path.start_with?("#{own_path}/")
  end

  path_matches || [self, *nested].any? do |entry|
    entry.matches_controller?(controller: view_context.params[:controller], action: view_context.params[:action])
  end
end

#matches_controller?(controller:, action:) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
# File 'lib/alchemy/configurations/menu_item.rb', line 118

def matches_controller?(controller:, action:)
  return false unless self.controller

  controller = controller.to_s.delete_prefix("/")
  action = action.to_s
  (self.controller.delete_prefix("/") == controller || nested_controllers.include?(controller)) &&
    (self.action == action || nested_actions.include?(action))
end

#path(view_context) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/alchemy/configurations/menu_item.rb', line 82

def path(view_context)
  if url
    view_context.instance_exec(&url)
  elsif controller
    routes = engine_name.present? ? view_context.public_send(engine_name) : view_context.main_app
    routes.url_for(controller:, action:, only_path: true)
  end
end

#position=(value) ⇒ Object

Alchemy's own entries still need their positions for sorting, so only positions of other entries are deprecated.



32
33
34
35
36
37
38
39
40
41
# File 'lib/alchemy/configurations/menu_item.rb', line 32

def position=(value)
  if value && Menu::DEFAULT.none? { |entry| entry[:name] == name }
    Alchemy::Deprecation.warn(
      "The `position` of menu entries is deprecated and will be removed in Alchemy 9.0, " \
      "which lists the entries in the order of `Alchemy.config.menu`. " \
      "Use `Alchemy.config.menu.insert` to place the #{name.inspect} entry instead."
    )
  end
  set_position(value)
end

#validate_controller_existence! ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/alchemy/configurations/menu_item.rb', line 127

def validate_controller_existence!
  [controller, *children.map(&:controller)].compact.each do |controller|
    controller_const_name = "#{controller.camelize}Controller"
    controller_const_name.constantize
  rescue NameError
    raise "Error in AlchemyCMS module definition: '#{name}'. Could not find the " \
      "matching controller class #{controller_const_name} for the specified controller: '#{controller}'"
  end
end

#visible?(view_context) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
# File 'lib/alchemy/configurations/menu_item.rb', line 91

def visible?(view_context)
  if condition
    view_context.instance_exec(&condition)
  elsif controller
    view_context.can?(action.to_sym, controller.delete_prefix("/").tr("/", "_").to_sym)
  else
    true
  end
end