Class: Spree::BackendConfiguration::MenuItem
- Inherits:
-
Object
- Object
- Spree::BackendConfiguration::MenuItem
- Defined in:
- lib/spree/backend_configuration/menu_item.rb
Overview
An item which should be drawn in the admin menu
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#data_hook ⇒ Object
readonly
Returns the value of attribute data_hook.
-
#icon ⇒ Object
readonly
Returns the value of attribute icon.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#match_path ⇒ Object
readonly
Returns the value of attribute match_path.
-
#partial ⇒ Object
readonly
Returns the value of attribute partial.
-
#position ⇒ Object
rubocop:disable Layout/EmptyLinesAroundAttributeAccessor.
Instance Method Summary collapse
-
#initialize(*args, icon: nil, condition: nil, label: nil, partial: nil, children: [], url: nil, position: nil, data_hook: nil, match_path: nil) ⇒ MenuItem
constructor
A new instance of MenuItem.
- #match_path?(request) ⇒ Boolean
- #render_in?(view_context) ⇒ Boolean
- #render_partial? ⇒ Boolean
-
#sections ⇒ Object
rubocop:disable Style/TrivialAccessors.
- #url ⇒ Object
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.
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
#children ⇒ Object (readonly)
Returns the value of attribute children.
7 8 9 |
# File 'lib/spree/backend_configuration/menu_item.rb', line 7 def children @children end |
#condition ⇒ Object (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_hook ⇒ Object (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 |
#icon ⇒ Object (readonly)
Returns the value of attribute icon.
7 8 9 |
# File 'lib/spree/backend_configuration/menu_item.rb', line 7 def icon @icon end |
#label ⇒ Object (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_path ⇒ Object (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 |
#partial ⇒ Object (readonly)
Returns the value of attribute partial.
7 8 9 |
# File 'lib/spree/backend_configuration/menu_item.rb', line 7 def partial @partial end |
#position ⇒ Object
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
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/spree/backend_configuration/menu_item.rb', line 80 def match_path?(request) 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}") elsif url.present? request.fullpath.to_s.starts_with?(url.to_s) elsif @sections.present? @sections.include?(request.controller_class.controller_name.to_sym) end end |
#render_in?(view_context) ⇒ 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
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. end |
#sections ⇒ Object
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 |
#url ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/spree/backend_configuration/menu_item.rb', line 94 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 |