Skip to content

Commit d067005

Browse files
authored
Revert "Prevent admins to be able to view carousel in shops"
1 parent c8e478d commit d067005

13 files changed

Lines changed: 55 additions & 208 deletions

File tree

app/controllers/api/v0/product_images_controller.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@ def update_product_image
99
product = Spree::Product.find(params[:product_id])
1010
authorize! :update, product
1111

12-
previous_image = product.image
13-
image = Spree::Image.new(
12+
image = product.image || Spree::Image.new(
1413
viewable_id: product.id,
1514
viewable_type: 'Spree::Product'
1615
)
17-
image.attachment.attach(params[:file])
1816

19-
success_status = previous_image.present? ? :ok : :created
17+
success_status = image.persisted? ? :ok : :created
2018

21-
if image.save
22-
previous_image&.destroy!
19+
if image.update(attachment: params[:file])
2320
render json: image, serializer: ImageSerializer, status: success_status
2421
else
2522
error_json = { errors: image.errors.full_messages }

app/controllers/spree/admin/images_controller.rb

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
# rubocop:disable Metrics/ClassLength
43
module Spree
54
module Admin
65
class ImagesController < ::Admin::ResourceController
@@ -44,20 +43,15 @@ def create
4443
format.turbo_stream { render :update }
4544
end
4645
else
47-
respond_with_error((@error_target || @object).errors)
46+
respond_with_error(@object.errors)
4847
end
4948
end
5049

5150
def update
5251
@url_filters = ::ProductFilters.new.extract(request.query_parameters)
53-
update_successful = if permitted_resource_params[:attachment].present?
54-
replace_image_without_destroy
55-
else
56-
set_viewable
57-
@object.update(permitted_resource_params)
58-
end
59-
60-
if update_successful
52+
set_viewable
53+
54+
if @object.update(permitted_resource_params)
6155
flash[:success] = flash_message_for(@object, :successfully_updated)
6256

6357
respond_to do |format|
@@ -120,33 +114,10 @@ def permitted_resource_params
120114
def respond_with_error(errors)
121115
@errors = errors.map(&:full_message)
122116
respond_to do |format|
123-
format.html {
124-
render action_name == 'create' ? :new : :edit, status: :unprocessable_entity
125-
}
126-
format.turbo_stream { render :edit, status: :unprocessable_entity }
127-
end
128-
end
129-
130-
def replace_image_without_destroy
131-
previous_image = @object
132-
replacement_image = Spree::Image.new(viewable: previous_image.viewable)
133-
134-
replacement_image.alt = previous_image.alt
135-
replacement_image.position = previous_image.position
136-
replacement_image.attributes = permitted_resource_params.except(:attachment, :viewable_id)
137-
replacement_image.viewable_type = 'Spree::Product'
138-
replacement_image.viewable_id = params[:image][:viewable_id]
139-
replacement_image.attachment.attach(permitted_resource_params[:attachment])
140-
141-
unless replacement_image.save
142-
@error_target = replacement_image
143-
return false
117+
format.html { respond_with(@object) }
118+
format.turbo_stream { render :edit }
144119
end
145-
146-
previous_image.destroy!
147-
@object = @image = replacement_image
148120
end
149121
end
150122
end
151123
end
152-
# rubocop:enable Metrics/ClassLength

app/models/spree/image.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,13 @@ module Spree
44
class Image < Asset
55
ACCEPTED_CONTENT_TYPES = %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z}
66

7-
acts_as_paranoid
8-
9-
has_one_attached :attachment, service: image_service, dependent: false do |attachment|
7+
has_one_attached :attachment, service: image_service do |attachment|
108
attachment.variant :mini, resize_to_fill: [48, 48]
119
attachment.variant :small, resize_to_fill: [227, 227]
1210
attachment.variant :product, resize_to_limit: [240, 240]
1311
attachment.variant :large, resize_to_limit: [600, 600]
1412
end
1513

16-
# Prevent ActiveStorage from purging attachments when a Spree::Image is soft-deleted.
17-
attachment_reflection = reflect_on_association(:attachment_attachment)
18-
attachment_reflection.options[:dependent] = nil
19-
2014
validates :attachment,
2115
attached: true,
2216
processable_file: true,

app/views/spree/admin/images/edit.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
= render partial: 'spree/admin/shared/product_sub_menu'
22
= render partial: 'spree/admin/shared/product_tabs', locals: { current: 'Images' }
3-
= render partial: 'spree/shared/error_messages', locals: { target: @error_target || @image }
3+
= render partial: 'spree/shared/error_messages', locals: { target: @image }
44

55
- content_for :page_actions do
66
%li= button_link_to t('spree.back_to_images_list'), admin_product_images_url(@product, @url_filters), icon: 'icon-arrow-left'

app/views/spree/admin/images/edit.turbo_stream.haml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
= turbo_stream.update "edit_image_modal" do
22
= render ModalComponent.new id: "#modal_edit_product_image", instant: true, close_button: false, modal_class: :fit do
33
%h2= t(".title")
4-
- error_target = @error_target || @image
54

65
-# Display image in the same way it appears in the shopfront popup
7-
- if error_target&.errors&.any?
8-
- error_target.errors.full_messages.each do |error|
6+
- if defined?(@errors) && !@errors.empty?
7+
- @errors.each do |error|
98
%p
109
= error
1110
- else

app/views/spree/admin/images/index.html.haml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
= render partial: 'spree/admin/shared/product_sub_menu'
22
= render partial: 'spree/admin/shared/product_tabs', locals: { current: 'Images'}
33

4+
- content_for :page_actions do
5+
%li= link_to_with_icon('icon-plus', t('spree.new_image'), new_admin_product_image_url(@product, @url_filters), id: 'new_image_link', class: 'button')
6+
47
#images
58

69
- unless @product.image.present?

db/migrate/20260602222924_add_deleted_at_to_assets.rb

Lines changed: 0 additions & 8 deletions
This file was deleted.

db/migrate/20260611223814_ensure_single_product_image.rb

Lines changed: 0 additions & 36 deletions
This file was deleted.

db/schema.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.1].define(version: 2026_06_11_223814) do
13+
ActiveRecord::Schema[7.2].define(version: 2026_06_01_032848) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "pg_stat_statements"
1616
enable_extension "plpgsql"
@@ -500,8 +500,6 @@
500500
t.string "viewable_type", limit: 50
501501
t.string "type", limit: 75
502502
t.text "alt"
503-
t.datetime "deleted_at"
504-
t.index ["deleted_at"], name: "index_spree_assets_on_deleted_at"
505503
t.index ["viewable_id"], name: "index_assets_on_viewable_id"
506504
t.index ["viewable_type", "type"], name: "index_assets_on_viewable_type_and_type"
507505
end

spec/migrations/ensure_single_product_image_spec.rb

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)