Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ordering to menu items #2939

Merged
merged 3 commits into from
Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions backend/app/models/spree/backend_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class BackendConfiguration < Preferences::Configuration
class MenuItem
attr_reader :icon, :label, :partial, :condition, :sections, :url

attr_accessor :position

# @param sections [Array<Symbol>] The sections which are contained within
# this admin menu section.
# @param icon [String] The icon to draw for this menu item
Expand All @@ -35,13 +37,16 @@ class MenuItem
# @param partial [String] A partial to draw within this menu item for use
# in declaring a submenu
# @param url [String] A url where this link should send the user to
# @param position [Integer] The position in which the menu item should render
# nil will cause the item to render last
def initialize(
sections,
icon,
condition: nil,
label: nil,
partial: nil,
url: nil
url: nil,
position: nil
)

@condition = condition || -> { true }
Expand All @@ -50,6 +55,7 @@ def initialize(
@label = label || sections.first
@partial = partial
@url = url
@position = position
end
end

Expand All @@ -65,6 +71,11 @@ def initialize(
#
# @!attribute menu_items
# @return [Array<Spree::BackendConfiguration::MenuItem>]
#
# Positioning can be determined by setting the position attribute to
# an Integer or nil. Menu Items will be rendered with smaller lower values
# first and higher values last. A position value of nil will cause the menu
# item to be rendered at the end of the list.
attr_writer :menu_items

# Return the menu items which should be drawn in the menu
Expand All @@ -77,40 +88,46 @@ def menu_items
ORDER_TABS,
'shopping-cart',
condition: -> { can?(:admin, Spree::Order) },
position: 0
),
MenuItem.new(
PRODUCT_TABS,
'th-large',
condition: -> { can?(:admin, Spree::Product) },
partial: 'spree/admin/shared/product_sub_menu'
),
MenuItem.new(
CONFIGURATION_TABS,
'wrench',
condition: -> { can?(:admin, Spree::Store) },
label: :settings,
partial: 'spree/admin/shared/settings_sub_menu',
url: :admin_stores_path
partial: 'spree/admin/shared/product_sub_menu',
position: 1
),
MenuItem.new(
PROMOTION_TABS,
'bullhorn',
partial: 'spree/admin/shared/promotion_sub_menu',
condition: -> { can?(:admin, Spree::Promotion) },
url: :admin_promotions_path
url: :admin_promotions_path,
position: 2
),
MenuItem.new(
STOCK_TABS,
'cubes',
condition: -> { can?(:admin, Spree::StockItem) },
label: :stock,
url: :admin_stock_items_path
url: :admin_stock_items_path,
position: 3
),
MenuItem.new(
USER_TABS,
'user',
condition: -> { Spree.user_class && can?(:admin, Spree.user_class) },
url: :admin_users_path
url: :admin_users_path,
position: 4
),
MenuItem.new(
CONFIGURATION_TABS,
'wrench',
condition: -> { can?(:admin, Spree::Store) },
label: :settings,
partial: 'spree/admin/shared/settings_sub_menu',
url: :admin_stores_path,
position: 5
)
]
end
Expand Down
2 changes: 1 addition & 1 deletion backend/app/views/spree/admin/shared/_tabs.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% Spree::Backend::Config.menu_items.each do |menu_item| %>
<% Spree::Backend::Config.menu_items.sort_by { |item| item.position || Float::INFINITY }.each do |menu_item| %>
<% if instance_exec(&menu_item.condition) %>
<%=
tab(
Expand Down