/backend/app/controllers/spree/admin/products_controller.rb

https://github.com/solidusio/solidus · Ruby · 137 lines · 106 code · 25 blank · 6 comment · 6 complexity · c267c5aae7b6e2f63431cad09b84a526 MD5 · raw file

  1. # frozen_string_literal: true
  2. module Spree
  3. module Admin
  4. class ProductsController < ResourceController
  5. helper 'spree/products'
  6. before_action :load_data, except: [:index]
  7. update.before :update_before
  8. helper_method :clone_object_url
  9. before_action :split_params, only: [:create, :update]
  10. before_action :normalize_variant_property_rules, only: [:update]
  11. def show
  12. redirect_to action: :edit
  13. end
  14. def index
  15. session[:return_to] = request.url
  16. respond_with(@collection)
  17. end
  18. def destroy
  19. @product = Spree::Product.friendly.find(params[:id])
  20. @product.discard
  21. flash[:success] = t('spree.notice_messages.product_deleted')
  22. respond_with(@product) do |format|
  23. format.html { redirect_to collection_url }
  24. format.js { render_js_for_destroy }
  25. end
  26. end
  27. def clone
  28. @new = @product.duplicate
  29. if @new.save
  30. flash[:success] = t('spree.notice_messages.product_cloned')
  31. else
  32. flash[:error] = t('spree.notice_messages.product_not_cloned')
  33. end
  34. redirect_to edit_admin_product_url(@new)
  35. end
  36. private
  37. def split_params
  38. if params[:product][:taxon_ids].present?
  39. params[:product][:taxon_ids] = params[:product][:taxon_ids].split(',')
  40. end
  41. if params[:product][:option_type_ids].present?
  42. params[:product][:option_type_ids] = params[:product][:option_type_ids].split(',')
  43. end
  44. end
  45. def find_resource
  46. Spree::Product.with_discarded.friendly.find(params[:id])
  47. end
  48. def location_after_save
  49. if updating_variant_property_rules?
  50. url_params = {}
  51. url_params[:ovi] = []
  52. params[:product][:variant_property_rules_attributes].each do |_index, param_attrs|
  53. url_params[:ovi] += param_attrs[:option_value_ids]
  54. end
  55. spree.admin_product_product_properties_url(@product, url_params)
  56. else
  57. spree.edit_admin_product_url(@product)
  58. end
  59. end
  60. def load_data
  61. @tax_categories = Spree::TaxCategory.order(:name)
  62. @default_tax_category = @tax_categories.detect(&:is_default)
  63. @shipping_categories = Spree::ShippingCategory.order(:name)
  64. end
  65. def collection
  66. return @collection if @collection
  67. params[:q] ||= {}
  68. params[:q][:s] ||= "name asc"
  69. # @search needs to be defined as this is passed to search_form_for
  70. @search = super.ransack(params[:q])
  71. @collection = @search.result.
  72. order(id: :asc).
  73. includes(product_includes).
  74. page(params[:page]).
  75. per(Spree::Config[:admin_products_per_page])
  76. end
  77. def update_before
  78. # note: we only reset the product properties if we're receiving a post
  79. # from the form on that tab
  80. return unless params[:clear_product_properties]
  81. params[:product] ||= {}
  82. end
  83. def product_includes
  84. [:variant_images, { variants: [:images], master: [:images, :default_price] }]
  85. end
  86. def clone_object_url(resource)
  87. clone_admin_product_url resource
  88. end
  89. def variant_stock_includes
  90. [:images, stock_items: :stock_location, option_values: :option_type]
  91. end
  92. def variant_scope
  93. @product.variants
  94. end
  95. def updating_variant_property_rules?
  96. params[:product][:variant_property_rules_attributes].present?
  97. end
  98. def render_after_update_error
  99. # Stops people submitting blank slugs, causing errors when they try to
  100. # update the product again
  101. @product.slug = @product.slug_was if @product.slug.blank?
  102. render action: 'edit'
  103. end
  104. def normalize_variant_property_rules
  105. return unless updating_variant_property_rules?
  106. params[:product][:variant_property_rules_attributes].each do |_index, param_attrs|
  107. param_attrs[:option_value_ids] = param_attrs[:option_value_ids].split(',')
  108. end
  109. end
  110. end
  111. end
  112. end