Class: Spree::BackendConfiguration::MenuItem

Inherits:
Object
  • Object
show all
Defined in:
lib/spree/backend_configuration/menu_item.rb

Overview

An item which should be drawn in the admin menu

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, icon: nil, condition: nil, label: nil, partial: nil, children: [], url: nil, position: nil, data_hook: nil, match_path: nil) ⇒ MenuItem

Returns a new instance of MenuItem.

Parameters:

  • icon (String) (defaults to: nil)

    The icon to draw for this menu item

  • condition (Proc) (defaults to: nil)

    A proc which returns true if this menu item should be drawn. If nil, it will be replaced with a proc which always returns true.

  • label (Symbol) (defaults to: nil)

    The translation key for a label to use for this menu item.

  • children (Array<Spree::BackendConfiguration::MenuItem>) (defaults to: [])

    An array

  • url (String|Symbol) (defaults to: nil)

    A url where this link should send the user to or a Symbol representing a route name

  • match_path (String, Regexp, callable) (defaults to: nil)

    (nil) If the #url to determine the active tab is ambigous you can pass a String, Regexp or callable to identify this menu item. The callable accepts a request object and returns a Boolean value.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/spree/backend_configuration/menu_item.rb', line 29

def initialize(
  *args,
  icon: nil,
  condition: nil,
  label: nil,
  partial: nil,
  children: [],
  url: nil,
  position: nil,
  data_hook: nil,
  match_path: nil
)
  if args.length == 2
    sections, icon = args
    label ||= sections.first.to_s
    Spree.deprecator.warn "Passing sections to #{self.class.name} is deprecated. Please pass a label instead."
    Spree.deprecator.warn "Passing icon to #{self.class.name} is deprecated. Please use the keyword argument instead."
  elsif args.any?
    raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0..2)"
  end

  if partial.present? && children.blank?
    # We only show the deprecation if there are no children, because if there are children,
    # then the menu item is already future-proofed.
    Spree.deprecator.warn "Passing a partial to #{self.class.name} is deprecated. Please use the children keyword argument instead."
  end

  @condition = condition || -> { true }
  @sections = sections || []
  @icon = icon
  @label = label
  @partial = partial
  @children = children
  @url = url
  @data_hook = data_hook
  @match_path = match_path

  self.position = position if position # Use the setter to deprecate
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



7
8
9
# File 'lib/spree/backend_configuration/menu_item.rb', line 7

def children
  @children
end

#conditionObject (readonly)

Returns the value of attribute condition.



7
8
9
# File 'lib/spree/backend_configuration/menu_item.rb', line 7

def condition
  @condition
end

#data_hookObject (readonly)

Returns the value of attribute data_hook.



7
8
9
# File 'lib/spree/backend_configuration/menu_item.rb', line 7

def data_hook
  @data_hook
end

#iconObject (readonly)

Returns the value of attribute icon.



7
8
9
# File 'lib/spree/backend_configuration/menu_item.rb', line 7

def icon
  @icon
end

#labelObject (readonly)

Returns the value of attribute label.



7
8
9
# File 'lib/spree/backend_configuration/menu_item.rb', line 7

def label
  @label
end

#match_pathObject (readonly)

Returns the value of attribute match_path.



7
8
9
# File 'lib/spree/backend_configuration/menu_item.rb', line 7

def match_path
  @match_path
end

#partialObject (readonly)

Returns the value of attribute partial.



7
8
9
# File 'lib/spree/backend_configuration/menu_item.rb', line 7

def partial
  @partial
end

#positionObject

rubocop:disable Layout/EmptyLinesAroundAttributeAccessor



14
15
16
# File 'lib/spree/backend_configuration/menu_item.rb', line 14

def position
  @position
end

Instance Method Details

#match_path?(request) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/spree/backend_configuration/menu_item.rb', line 80

def match_path?(request)
  matches =
    if match_path.is_a? Regexp
      request.fullpath =~ match_path
    elsif match_path.respond_to?(:call)
      match_path.call(request)
    elsif match_path
      request.fullpath.starts_with?("#{spree.admin_path}#{match_path}")
    end
  matches ||= request.fullpath.to_s.starts_with?(url.to_s) if url.present?
  matches ||= @sections.include?(request.controller_class.controller_name.to_sym) if @sections.present?

  matches
end

#render_in?(view_context) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/spree/backend_configuration/menu_item.rb', line 69

def render_in?(view_context)
  view_context.instance_exec(&@condition) ||
    children.any? { |child| child.render_in?(view_context) }
end

#render_partial?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
# File 'lib/spree/backend_configuration/menu_item.rb', line 74

def render_partial?
  return false if partial.blank?

  children.blank? || Spree::Backend::Config.prefer_menu_item_partials
end

#sectionsObject

rubocop:disable Style/TrivialAccessors



9
10
11
# File 'lib/spree/backend_configuration/menu_item.rb', line 9

def sections # rubocop:disable Style/TrivialAccessors
  @sections
end

#urlObject



95
96
97
98
99
100
101
# File 'lib/spree/backend_configuration/menu_item.rb', line 95

def url
  url = @url.call if @url.respond_to?(:call)
  url ||= spree.public_send(@url) if @url.is_a?(Symbol) && spree.respond_to?(@url)
  url ||= spree.send("admin_#{@label}_path") if @url.nil? && @label && spree.respond_to?("admin_#{@label}_path")
  url ||= @url.to_s
  url
end