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

https://github.com/chienyuan/spree · Ruby · 129 lines · 103 code · 23 blank · 3 comment · 9 complexity · 68f09cae29a1fabd652d93a45629a0f5 MD5 · raw file

  1. module Spree
  2. module Admin
  3. class ProductsController < ResourceController
  4. helper 'spree/products'
  5. before_filter :load_data, :except => :index
  6. create.before :create_before
  7. update.before :update_before
  8. helper_method :clone_object_url
  9. def show
  10. session[:return_to] ||= request.referer
  11. redirect_to( :action => :edit )
  12. end
  13. def index
  14. session[:return_to] = request.url
  15. respond_with(@collection)
  16. end
  17. def update
  18. if params[:product][:taxon_ids].present?
  19. params[:product][:taxon_ids] = params[:product][:taxon_ids].split(',')
  20. end
  21. if params[:product][:option_type_ids].present?
  22. params[:product][:option_type_ids] = params[:product][:option_type_ids].split(',')
  23. end
  24. super
  25. end
  26. def destroy
  27. @product = Product.find_by_permalink!(params[:id])
  28. @product.destroy
  29. flash[:success] = Spree.t('notice_messages.product_deleted')
  30. respond_with(@product) do |format|
  31. format.html { redirect_to collection_url }
  32. format.js { render_js_for_destroy }
  33. end
  34. end
  35. def clone
  36. @new = @product.duplicate
  37. if @new.save
  38. flash[:success] = Spree.t('notice_messages.product_cloned')
  39. else
  40. flash[:success] = Spree.t('notice_messages.product_not_cloned')
  41. end
  42. redirect_to edit_admin_product_url(@new)
  43. end
  44. def stock
  45. @variants = @product.variants
  46. @variants = [@product.master] if @variants.empty?
  47. @stock_locations = StockLocation.accessible_by(current_ability, :read)
  48. if @stock_locations.empty?
  49. flash[:error] = Spree.t(:stock_management_requires_a_stock_location)
  50. redirect_to admin_stock_locations_path
  51. end
  52. end
  53. protected
  54. def find_resource
  55. Product.find_by_permalink!(params[:id])
  56. end
  57. def location_after_save
  58. spree.edit_admin_product_url(@product)
  59. end
  60. def load_data
  61. @taxons = Taxon.order(:name)
  62. @option_types = OptionType.order(:name)
  63. @tax_categories = TaxCategory.order(:name)
  64. @shipping_categories = ShippingCategory.order(:name)
  65. end
  66. def collection
  67. return @collection if @collection.present?
  68. params[:q] ||= {}
  69. params[:q][:deleted_at_null] ||= "1"
  70. params[:q][:s] ||= "name asc"
  71. @collection = super
  72. @collection = @collection.with_deleted if params[:q].delete(:deleted_at_null).blank?
  73. # @search needs to be defined as this is passed to search_form_for
  74. @search = @collection.ransack(params[:q])
  75. @collection = @search.result.
  76. group_by_products_id.
  77. includes(product_includes).
  78. page(params[:page]).
  79. per(Spree::Config[:admin_products_per_page])
  80. if params[:q][:s].include?("master_default_price_amount")
  81. # PostgreSQL compatibility
  82. @collection = @collection.group("spree_prices.amount")
  83. end
  84. @collection
  85. end
  86. def create_before
  87. return if params[:product][:prototype_id].blank?
  88. @prototype = Spree::Prototype.find(params[:product][:prototype_id])
  89. end
  90. def update_before
  91. # note: we only reset the product properties if we're receiving a post from the form on that tab
  92. return unless params[:clear_product_properties]
  93. params[:product] ||= {}
  94. end
  95. def product_includes
  96. [{:variants => [:images, {:option_values => :option_type}]}, {:master => [:images, :default_price]}]
  97. end
  98. def clone_object_url resource
  99. clone_admin_product_url resource
  100. end
  101. def permit_attributes
  102. params.require(:product).permit!
  103. end
  104. end
  105. end
  106. end