Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Admin::ProductsController to use ResourcesController#update #3603

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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