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.

Improve asset type handling in `Admin::AssetsController`

Damian Legawiecb420b5cbf188
diff --git a/admin/app/controllers/spree/admin/assets_controller.rb b/admin/app/controllers/spree/admin/assets_controller.rb
index 10319abcac..d92fc825ca 100644
--- a/admin/app/controllers/spree/admin/assets_controller.rb
+++ b/admin/app/controllers/spree/admin/assets_controller.rb
@@ -3,14 +3,8 @@ module Spree
class AssetsController < ResourceController
include Spree::Admin::SessionAssetsHelper
- ALLOWED_ASSET_TYPES = ['Spree::Asset', 'Spree::Image'].freeze
-
def create
- if ALLOWED_ASSET_TYPES.include?(asset_type)
- @asset = asset_type.constantize.new(permitted_resource_params)
- else
- raise "Invalid asset type"
- end
+ @asset = asset_type.new(permitted_resource_params)
# we only should check this for vendor users
authorize! :update, @asset.viewable if @asset.viewable.present? && current_vendor
@@ -47,7 +41,7 @@ module Spree
private
def permitted_resource_params
- params.require(:asset).permit(:type, :viewable_id, :viewable_type, :attachment, :alt, :position)
+ params.require(:asset).permit(Spree::PermittedAttributes.asset_attributes)
end
def create_turbo_stream_enabled?
@@ -63,7 +57,11 @@ module Spree
end
def asset_type
- permitted_resource_params[:type] || model_class.to_s
+ allowed_asset_types.find { |type| type.to_s == permitted_resource_params[:type] } || model_class
+ end
+
+ def allowed_asset_types
+ [Spree::Asset, Spree::Image]
end
end
end
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index c1952f06b0..ead3298de2 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -2,6 +2,7 @@ module Spree
module PermittedAttributes
ATTRIBUTES = [
:address_attributes,
+ :asset_attributes,
:checkout_attributes,
:classification_attributes,
:cms_page_attributes,
@@ -47,6 +48,8 @@ module Spree
state: [:name, :abbr] }
]
+ @@asset_attributes = [:type, :viewable_id, :viewable_type, :attachment, :alt, :position]
+
@@checkout_attributes = [
:coupon_code, :email, :shipping_method_id, :special_instructions, :use_billing, :use_shipping,
:user_id, :bill_address_id, :ship_address_id, :accept_marketing, :signup_for_an_account