|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -# rubocop:disable Metrics/ClassLength |
4 | 3 | module Spree |
5 | 4 | module Admin |
6 | 5 | class ImagesController < ::Admin::ResourceController |
@@ -44,20 +43,15 @@ def create |
44 | 43 | format.turbo_stream { render :update } |
45 | 44 | end |
46 | 45 | else |
47 | | - respond_with_error((@error_target || @object).errors) |
| 46 | + respond_with_error(@object.errors) |
48 | 47 | end |
49 | 48 | end |
50 | 49 |
|
51 | 50 | def update |
52 | 51 | @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) |
61 | 55 | flash[:success] = flash_message_for(@object, :successfully_updated) |
62 | 56 |
|
63 | 57 | respond_to do |format| |
@@ -120,33 +114,10 @@ def permitted_resource_params |
120 | 114 | def respond_with_error(errors) |
121 | 115 | @errors = errors.map(&:full_message) |
122 | 116 | 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 } |
144 | 119 | end |
145 | | - |
146 | | - previous_image.destroy! |
147 | | - @object = @image = replacement_image |
148 | 120 | end |
149 | 121 | end |
150 | 122 | end |
151 | 123 | end |
152 | | -# rubocop:enable Metrics/ClassLength |
0 commit comments