Skip to content

Commit

Permalink
Merge pull request #3603 from softr8/refactor-admin-products-controller
Browse files Browse the repository at this point in the history
Refactoring Admin::ProductsController to use ResourcesController#update
  • Loading branch information
aldesantis authored May 6, 2020
2 parents 3883757 + 969676f commit b539d47
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions backend/app/controllers/spree/admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ProductsController < ResourceController
update.before :update_before
helper_method :clone_object_url
before_action :split_params, only: [:create, :update]
before_action :normalize_variant_property_rules, only: [:update]

def show
redirect_to action: :edit
Expand All @@ -19,29 +20,6 @@ def index
respond_with(@collection)
end

def update
if updating_variant_property_rules?
params[:product][:variant_property_rules_attributes].each do |_index, param_attrs|
param_attrs[:option_value_ids] = param_attrs[:option_value_ids].split(',')
end
end
invoke_callbacks(:update, :before)
if @object.update(permitted_resource_params)
invoke_callbacks(:update, :after)
flash[:success] = flash_message_for(@object, :successfully_updated)
respond_with(@object) do |format|
format.html { redirect_to location_after_save }
format.js { render layout: false }
end
else
# Stops people submitting blank slugs, causing errors when they try to
# update the product again
@product.slug = @product.slug_was if @product.slug.blank?
invoke_callbacks(:update, :fails)
respond_with(@object)
end
end

def destroy
@product = Spree::Product.friendly.find(params[:id])
@product.discard
Expand Down Expand Up @@ -138,6 +116,21 @@ def variant_scope
def updating_variant_property_rules?
params[:product][:variant_property_rules_attributes].present?
end

def render_after_update_error
# Stops people submitting blank slugs, causing errors when they try to
# update the product again
@product.slug = @product.slug_was if @product.slug.blank?
render action: 'edit'
end

def normalize_variant_property_rules
return unless updating_variant_property_rules?

params[:product][:variant_property_rules_attributes].each do |_index, param_attrs|
param_attrs[:option_value_ids] = param_attrs[:option_value_ids].split(',')
end
end
end
end
end

0 comments on commit b539d47

Please sign in to comment.