Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Tidy code and add more indexes.

Matthew Kennedy399d2f9d1d68
diff --git a/backend/app/controllers/spree/admin/base_controller.rb b/backend/app/controllers/spree/admin/base_controller.rb
index 2a1c5e122c..1a39979bc8 100644
--- a/backend/app/controllers/spree/admin/base_controller.rb
+++ b/backend/app/controllers/spree/admin/base_controller.rb
@@ -8,6 +8,7 @@ module Spree
before_action :authorize_admin
before_action :generate_admin_api_key
+ before_action :load_data
protected
@@ -47,6 +48,10 @@ module Spree
Spree::Backend::Config[:locale]
end
+ def load_data
+ @stores = Spree::Store.accessible_by(current_ability, :show)
+ end
+
def can_not_transition_without_customer_info
unless @order.billing_address.present?
flash[:notice] = Spree.t(:fill_in_customer_info)
diff --git a/backend/app/controllers/spree/admin/menu_items_controller.rb b/backend/app/controllers/spree/admin/menu_items_controller.rb
index 66647d24d6..19f4229066 100644
--- a/backend/app/controllers/spree/admin/menu_items_controller.rb
+++ b/backend/app/controllers/spree/admin/menu_items_controller.rb
@@ -3,6 +3,8 @@ module Spree
class MenuItemsController < ResourceController
belongs_to 'spree/menu'
+ before_action :load_data
+
def index; end
def collection_url
@@ -12,6 +14,13 @@ module Spree
def location_after_save
spree.edit_admin_menu_menu_item_path(@menu, @menu_item)
end
+
+ def load_data
+ @menu_item_types = Spree::MenuItem::ITEM_TYPE
+ @linked_resource_types = Spree::MenuItem::LINKED_RESOURCE_TYPE
+ @taxons = Spree::Taxon.all
+ @products = Spree::Product.all
+ end
end
end
end
diff --git a/backend/app/controllers/spree/admin/menus_controller.rb b/backend/app/controllers/spree/admin/menus_controller.rb
index 28ab2d169b..dc63c74f63 100644
--- a/backend/app/controllers/spree/admin/menus_controller.rb
+++ b/backend/app/controllers/spree/admin/menus_controller.rb
@@ -1,7 +1,6 @@
module Spree
module Admin
class MenusController < ResourceController
- before_action :load_data
before_action :load_menu_items_ordered, only: :edit
def index; end
@@ -12,14 +11,8 @@ module Spree
spree.edit_admin_menu_path(@menu)
end
- private
-
- def load_data
- @stores = Spree::Store.all
- end
-
def load_menu_items_ordered
- @menu_items_in_order = @menu.menu_items.order('lft ASC')
+ @menu_items_in_order = @menu.menu_items.order(:lft)
end
end
end
diff --git a/backend/app/views/spree/admin/menu_items/_form.html.erb b/backend/app/views/spree/admin/menu_items/_form.html.erb
index 8ecf277599..5e1507113c 100644
--- a/backend/app/views/spree/admin/menu_items/_form.html.erb
+++ b/backend/app/views/spree/admin/menu_items/_form.html.erb
@@ -8,7 +8,7 @@
<div class="card-body">
<%= f.field_container :item_type, class: ['form-group'] do %>
<%= f.label :item_type, Spree.t('admin.navigation.item_type') %>
- <%= f.select(:item_type, Spree::MenuItem::ITEM_TYPE, {include_blank: false}, { class: 'select2' }) %>
+ <%= f.select(:item_type, @menu_item_types, {include_blank: false}, { class: 'select2' }) %>
<%= f.error_message_on :item_type %>
<% end %>
@@ -75,7 +75,7 @@
<div class="col-6">
<%= f.field_container :linked_resource_type, class: ['form-group'] do %>
<%= f.label :linked_resource_type, Spree.t('admin.navigation.link_to') %>
- <%= f.select(:linked_resource_type, Spree::MenuItem::LINKED_RESOURCE_TYPE, {include_blank: false}, { class: 'selec2', id: 'LinkToSelect2' }) %>
+ <%= f.select(:linked_resource_type, @linked_resource_types, {include_blank: false}, { class: 'selec2', id: 'LinkToSelect2' }) %>
<%= f.error_message_on :linked_resource_type %>
<% end %>
</div>
diff --git a/backend/app/views/spree/admin/menu_items/link_inputs/_product.html.erb b/backend/app/views/spree/admin/menu_items/link_inputs/_product.html.erb
index 14b47af16e..180fcb6f13 100644
--- a/backend/app/views/spree/admin/menu_items/link_inputs/_product.html.erb
+++ b/backend/app/views/spree/admin/menu_items/link_inputs/_product.html.erb
@@ -1,6 +1,6 @@
<%= f.field_container :linked_resource_id, class: ['form-group'] do %>
<%= f.label :linked_resource_id, Spree.t('product') %>
- <%= f.select :linked_resource_id, options_from_collection_for_select(Spree::Product.all, :id, :name, @menu_item.linked_resource_id), {include_blank: true}, multiple: false, class: 'select2-clear'%>
+ <%= f.select :linked_resource_id, options_from_collection_for_select(@products, :id, :name, @menu_item.linked_resource_id), {include_blank: true}, multiple: false, class: 'select2-clear'%>
<%= f.error_message_on :linked_resource_id %>
<small class="form-text text-muted"><%= @menu_item.destination %></small>
<% end %>
diff --git a/backend/app/views/spree/admin/menu_items/link_inputs/_taxon.html.erb b/backend/app/views/spree/admin/menu_items/link_inputs/_taxon.html.erb
index cb5daee3ec..bcdbe66b99 100644
--- a/backend/app/views/spree/admin/menu_items/link_inputs/_taxon.html.erb
+++ b/backend/app/views/spree/admin/menu_items/link_inputs/_taxon.html.erb
@@ -1,6 +1,6 @@
<%= f.field_container :linked_resource_id, class: ['form-group'] do %>
<%= f.label :linked_resource_id, Spree.t('taxon') %>
- <%= f.select :linked_resource_id, options_from_collection_for_select(Spree::Taxon.all, :id, :pretty_name, @menu_item.linked_resource_id), {include_blank: true}, multiple: false, class: 'select2-clear' %>
+ <%= f.select :linked_resource_id, options_from_collection_for_select(@taxons, :id, :pretty_name, @menu_item.linked_resource_id), {include_blank: true}, multiple: false, class: 'select2-clear' %>
<%= f.error_message_on :linked_resource_id %>
<small class="form-text text-muted"><%= @menu_item.destination %></small>
<% end %>
diff --git a/core/db/migrate/20210407200948_create_spree_menus.rb b/core/db/migrate/20210407200948_create_spree_menus.rb
index c237ef7107..30c75603e6 100644
--- a/core/db/migrate/20210407200948_create_spree_menus.rb
+++ b/core/db/migrate/20210407200948_create_spree_menus.rb
@@ -5,5 +5,7 @@ class CreateSpreeMenus < ActiveRecord::Migration[6.0]
t.timestamps
end
+
+ add_index :spree_menus, :name
end
end
diff --git a/core/db/migrate/20210408092939_create_spree_menu_items.rb b/core/db/migrate/20210408092939_create_spree_menu_items.rb
index 424af8a55f..f8e5f6ff2a 100644
--- a/core/db/migrate/20210408092939_create_spree_menu_items.rb
+++ b/core/db/migrate/20210408092939_create_spree_menu_items.rb
@@ -17,8 +17,10 @@ class CreateSpreeMenuItems < ActiveRecord::Migration[6.0]
t.timestamps
end
- add_index :spree_menu_items, [:parent_id]
- add_index :spree_menu_items, [:lft]
- add_index :spree_menu_items, [:rgt]
+ add_index :spree_menu_items, :lft
+ add_index :spree_menu_items, :rgt
+ add_index :spree_menu_items, :parent_id
+ add_index :spree_menu_items, :item_type
+ add_index :spree_menu_items, [:linked_resource_type, :linked_resource_id], name: 'index_on_linked_resource_type_and_linked_resource_id'
end
end