Skip to content

Commit

Permalink
Merge pull request #274 from spree/feature/product-taxon-tabs-extensi…
Browse files Browse the repository at this point in the history
…bility

SP-62 Feature/product taxon tabs extensibility
  • Loading branch information
rafalcymerys authored Oct 23, 2023
2 parents 9ce8b91 + 21e4db5 commit 027e49a
Show file tree
Hide file tree
Showing 29 changed files with 1,214 additions and 423 deletions.
12 changes: 12 additions & 0 deletions app/helpers/spree/admin/navigation_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,18 @@ def page_header_back_button(url)
def main_menu
Rails.application.config.spree_backend.main_menu
end

def order_tabs
Rails.application.config.spree_backend.tabs[:order]
end

def user_tabs
Rails.application.config.spree_backend.tabs[:user]
end

def product_tabs
Rails.application.config.spree_backend.tabs[:product]
end
end
end
end
50 changes: 50 additions & 0 deletions app/models/spree/admin/item_manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Spree
module Admin
module ItemManager
def add(item)
raise KeyError, "Item with key #{item.key} already exists" if index_for_key(item.key)

@items << item
end

def child_with_key?(key)
index_for_key(key).present?
end

def remove(item_key)
item_index = index_for_key!(item_key)

@items.delete_at(item_index)
end

def item_for_key(key)
@items.find { |e| e.key == key }
end

def insert_before(item_key, item_to_add)
item_index = index_for_key!(item_key)

@items.insert(item_index, item_to_add)
end

def insert_after(item_key, item_to_add)
item_index = index_for_key!(item_key)

@items.insert(item_index + 1, item_to_add)
end

private

def index_for_key(key)
@items.index { |e| e.key == key }
end

def index_for_key!(key)
item_index = index_for_key(key)
raise KeyError, "Item not found for key #{key}" unless item_index

item_index
end
end
end
end
22 changes: 0 additions & 22 deletions app/models/spree/admin/main_menu/availability_builder_methods.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/spree/admin/main_menu/item_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Spree
module Admin
module MainMenu
class ItemBuilder
include AvailabilityBuilderMethods
include ::Spree::Admin::PermissionChecks

def initialize(key, url)
@key = key
Expand Down
2 changes: 1 addition & 1 deletion app/models/spree/admin/main_menu/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize
end

def add_to_section(section_key, item)
@items.find { |e| e.key == section_key }.add_item(item)
@items.find { |e| e.key == section_key }.add(item)
end
end
end
Expand Down
47 changes: 2 additions & 45 deletions app/models/spree/admin/main_menu/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Spree
module Admin
module MainMenu
class Section
include ::Spree::Admin::ItemManager

attr_reader :key, :label_translation_key, :icon_key, :items

def initialize(key, label_translation_key, icon_key, availability_check, items)
Expand All @@ -12,38 +14,6 @@ def initialize(key, label_translation_key, icon_key, availability_check, items)
@items = items
end

def add(item)
raise KeyError, "Item with key #{key} already exists" if index_for_key(item.key)

@items << item
end

def child_with_key?(key)
index_for_key(key).present?
end

def remove(item_key)
item_index = index_for_key!(item_key)

@items.delete_at(item_index)
end

def item_for_key(key)
@items.find { |e| e.key == key }
end

def insert_before(item_key, item_to_add)
item_index = index_for_key!(item_key)

@items.insert(item_index, item_to_add)
end

def insert_after(item_key, item_to_add)
item_index = index_for_key!(item_key)

@items.insert(item_index + 1, item_to_add)
end

def available?(current_ability, current_store)
return true unless @availability_check.present?

Expand All @@ -53,19 +23,6 @@ def available?(current_ability, current_store)
def children?
@items.any?
end

private

def index_for_key(key)
@items.index { |e| e.key == key }
end

def index_for_key!(key)
item_index = index_for_key(key)
raise KeyError, "Item not found for key #{key}" unless item_index

item_index
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/spree/admin/main_menu/section_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Spree
module Admin
module MainMenu
class SectionBuilder
include AvailabilityBuilderMethods
include ::Spree::Admin::PermissionChecks

def initialize(key, icon_key)
@key = key
Expand Down
30 changes: 30 additions & 0 deletions app/models/spree/admin/permission_checks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Spree
module Admin
module PermissionChecks
def with_availability_check(availability_check)
@availability_check = availability_check
self
end

def with_manage_ability_check(*classes)
@availability_check = ->(ability, _current) { classes.any? { |c| ability.can?(:manage, c) } }
self
end

def with_admin_ability_check(*classes)
@availability_check = ->(ability, _current) { classes.any? { |c| ability.can?(:admin, c) } }
self
end

def with_index_ability_check(*classes)
@availability_check = ->(ability, _current) { classes.any? { |c| ability.can?(:index, c) } }
self
end

def with_update_ability_check
@availability_check = ->(ability, resource) { ability.can?(:update, resource) }
self
end
end
end
end
17 changes: 17 additions & 0 deletions app/models/spree/admin/tabs/conditional_checker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Spree
module Admin
module Tabs
module ConditionalChecker
def with_active_check
@active_check = ->(current_tab, text) { current_tab == text }
self
end

def with_completed_check
@completed_check = ->(resource) { resource.completed? }
self
end
end
end
end
end
12 changes: 12 additions & 0 deletions app/models/spree/admin/tabs/data_hook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Spree
module Admin
module Tabs
module DataHook
def with_data_hook(data_hook)
@data_hook = data_hook
self
end
end
end
end
end
Loading

0 comments on commit 027e49a

Please sign in to comment.