lib/active_admin/menu.rb in activeadmin-3.2.5 vs lib/active_admin/menu.rb in activeadmin-4.0.0.beta1

- old
+ new

@@ -47,14 +47,11 @@ # menu.add label: 'Dashboard' # menu.add parent: 'Dashboard', label: 'My Child Dashboard' # def add(options) options = options.dup # Make sure parameter is not modified - parent_chain = Array.wrap(options.delete(:parent)) - - item = if parent = parent_chain.shift - options[:parent] = parent_chain if parent_chain.any? + item = if parent = options.delete(:parent) (self[parent] || add(label: parent)).add options else _add options.merge parent: self end @@ -63,19 +60,25 @@ item end # Whether any children match the given item. def include?(item) - @children.values.include?(item) || @children.values.any? { |child| child.include?(item) } + @children.value?(item) end # Used in the UI to visually distinguish which menu item is selected. def current?(item) self == item || include?(item) end - def items - @children.values + # Returns sorted array of menu items that should be displayed in this context. + # Sorts by priority first, then alphabetically by label if needed. + def items(context = nil) + @children.values.select { |i| i.display?(context) }.sort do |a, b| + result = a.priority <=> b.priority + result = a.label(context) <=> b.label(context) if result == 0 + result + end end attr_reader :children private attr_writer :children