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.

Remove duplications for fetching accessible Stores in Admin Panel

Damian Legawiec678a135082a3
diff --git a/backend/app/controllers/spree/admin/base_controller.rb b/backend/app/controllers/spree/admin/base_controller.rb
index 78d783163d..79f60f38b9 100644
--- a/backend/app/controllers/spree/admin/base_controller.rb
+++ b/backend/app/controllers/spree/admin/base_controller.rb
@@ -50,8 +50,12 @@ module Spree
Spree::Backend::Config[:locale]
end
+ def stores_scope
+ Spree::Store.accessible_by(current_ability, :show)
+ end
+
def load_stores
- @stores = Spree::Store.accessible_by(current_ability, :show).order(default: :desc)
+ @stores = stores_scope.order(default: :desc)
end
def can_not_transition_without_customer_info
diff --git a/backend/app/controllers/spree/admin/orders_controller.rb b/backend/app/controllers/spree/admin/orders_controller.rb
index 8080554a3e..e2049316a0 100644
--- a/backend/app/controllers/spree/admin/orders_controller.rb
+++ b/backend/app/controllers/spree/admin/orders_controller.rb
@@ -78,9 +78,7 @@ module Spree
end
end
- def store
- @stores = Spree::Store.all
- end
+ def store; end
def update
if @order.update(params[:order]) && @order.line_items.present?
diff --git a/backend/app/controllers/spree/admin/payment_methods_controller.rb b/backend/app/controllers/spree/admin/payment_methods_controller.rb
index 4088395e6b..1268acd22d 100644
--- a/backend/app/controllers/spree/admin/payment_methods_controller.rb
+++ b/backend/app/controllers/spree/admin/payment_methods_controller.rb
@@ -54,7 +54,6 @@ module Spree
end
def load_data
- @stores = Spree::Store.all
@providers = Gateway.providers.sort_by(&:name)
end
diff --git a/backend/app/controllers/spree/admin/stores_controller.rb b/backend/app/controllers/spree/admin/stores_controller.rb
index 23c9bcea4b..df20a59490 100644
--- a/backend/app/controllers/spree/admin/stores_controller.rb
+++ b/backend/app/controllers/spree/admin/stores_controller.rb
@@ -15,8 +15,6 @@ module Spree
load_stores_by_ids
elsif params[:q]
load_stores_by_query
- else
- @stores = Spree::Store.all
end
respond_with(@stores) do |format|
@@ -25,21 +23,20 @@ module Spree
end
def load_stores_by_ids
- ids = params[:ids].split(',')
- @stores = Spree::Store.where(id: ids)
+ @stores = stores_scope.where(id: params[:ids].split(','))
end
def load_stores_by_query
@stores = if defined?(SpreeGlobalize)
- Spree::Store.joins(:translations).where("LOWER(#{Store::Translation.table_name}.name) LIKE LOWER(:query)",
+ stores_scope.joins(:translations).where("LOWER(#{Store::Translation.table_name}.name) LIKE LOWER(:query)",
query: "%#{params[:q]}%")
else
- Spree::Store.where('LOWER(name) LIKE LOWER(:query)', query: "%#{params[:q]}%")
+ stores_scope.where('LOWER(name) LIKE LOWER(:query)', query: "%#{params[:q]}%")
end
end
def create
- @store = Spree::Store.new(permitted_store_params)
+ @store = stores_scope.new(permitted_store_params)
if @store.save
flash[:success] = flash_message_for(@store, :successfully_created)
@@ -63,7 +60,7 @@ module Spree
end
def destroy
- @store = Spree::Store.find(params[:id])
+ @store = stores_scope.find(params[:id])
if @store.destroy
flash[:success] = flash_message_for(@store, :successfully_removed)
@@ -77,8 +74,8 @@ module Spree
end
def set_default
- store = Spree::Store.find(params[:id])
- stores = Spree::Store.where.not(id: params[:id])
+ store = stores_scope.find(params[:id])
+ stores = stores_scope.where.not(id: params[:id])
ApplicationRecord.transaction do
store.update(default: true)
@@ -103,7 +100,7 @@ module Spree
private
def load_store
- @store = Spree::Store.find_by(id: params[:id]) || Spree::Store.new
+ @store = stores_scope.find_by(id: params[:id]) || stores_scope.new
end
def load_all_countries
diff --git a/backend/app/views/spree/admin/orders/index.html.erb b/backend/app/views/spree/admin/orders/index.html.erb
index 0c621e1fd8..16959e1258 100644
--- a/backend/app/views/spree/admin/orders/index.html.erb
+++ b/backend/app/views/spree/admin/orders/index.html.erb
@@ -133,7 +133,7 @@
<div class="col-12 col-lg-4">
<div class="form-group">
<%= label_tag :q_store_id_in, Spree.t(:store) %>
- <%= f.select :store_id_in, Spree::Store.order(:name).pluck(:name, :id), { include_blank: true }, class: 'select2-clear js-filterable' %>
+ <%= f.select :store_id_in, @stores.pluck(:name, :id), { include_blank: true }, class: 'select2-clear js-filterable' %>
</div>
</div>