PageRenderTime 893ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/GeekOnCoffee/spree
Ruby | 144 lines | 115 code | 24 blank | 5 comment | 12 complexity | c141955175200a035ed65cc32f318280 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. module Spree
  2. module Admin
  3. class ProductsController < ResourceController
  4. helper 'spree/products'
  5. before_action :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. invoke_callbacks(:update, :before)
  25. if @object.update_attributes(permitted_resource_params)
  26. invoke_callbacks(:update, :after)
  27. flash[:success] = flash_message_for(@object, :successfully_updated)
  28. respond_with(@object) do |format|
  29. format.html { redirect_to location_after_save }
  30. format.js { render layout: false }
  31. end
  32. else
  33. # Stops people submitting blank slugs, causing errors when they try to
  34. # update the product again
  35. @product.slug = @product.slug_was if @product.slug.blank?
  36. invoke_callbacks(:update, :fails)
  37. respond_with(@object)
  38. end
  39. end
  40. def destroy
  41. @product = Product.friendly.find(params[:id])
  42. @product.destroy
  43. flash[:success] = Spree.t('notice_messages.product_deleted')
  44. respond_with(@product) do |format|
  45. format.html { redirect_to collection_url }
  46. format.js { render_js_for_destroy }
  47. end
  48. end
  49. def clone
  50. @new = @product.duplicate
  51. if @new.save
  52. flash[:success] = Spree.t('notice_messages.product_cloned')
  53. else
  54. flash[:error] = Spree.t('notice_messages.product_not_cloned')
  55. end
  56. redirect_to edit_admin_product_url(@new)
  57. end
  58. def stock
  59. @variants = @product.variants.includes(*variant_stock_includes)
  60. @variants = [@product.master] if @variants.empty?
  61. @stock_locations = StockLocation.accessible_by(current_ability, :read)
  62. if @stock_locations.empty?
  63. flash[:error] = Spree.t(:stock_management_requires_a_stock_location)
  64. redirect_to admin_stock_locations_path
  65. end
  66. end
  67. protected
  68. def find_resource
  69. Product.with_deleted.friendly.find(params[:id])
  70. end
  71. def location_after_save
  72. spree.edit_admin_product_url(@product)
  73. end
  74. def load_data
  75. @taxons = Taxon.order(:name)
  76. @option_types = OptionType.order(:name)
  77. @tax_categories = TaxCategory.order(:name)
  78. @shipping_categories = ShippingCategory.order(:name)
  79. end
  80. def collection
  81. return @collection if @collection.present?
  82. params[:q] ||= {}
  83. params[:q][:deleted_at_null] ||= "1"
  84. params[:q][:s] ||= "name asc"
  85. @collection = super
  86. if params[:q].delete(:deleted_at_null) == '0'
  87. @collection = @collection.with_deleted
  88. end
  89. # @search needs to be defined as this is passed to search_form_for
  90. @search = @collection.ransack(params[:q])
  91. @collection = @search.result.
  92. distinct_by_product_ids(params[:q][:s]).
  93. includes(product_includes).
  94. page(params[:page]).
  95. per(params[:per_page] || Spree::Config[:admin_products_per_page])
  96. @collection
  97. end
  98. def create_before
  99. return if params[:product][:prototype_id].blank?
  100. @prototype = Spree::Prototype.find(params[:product][:prototype_id])
  101. end
  102. def update_before
  103. # note: we only reset the product properties if we're receiving a post
  104. # from the form on that tab
  105. return unless params[:clear_product_properties]
  106. params[:product] ||= {}
  107. end
  108. def product_includes
  109. [{ variants: [:images], master: [:images, :default_price] }]
  110. end
  111. def clone_object_url(resource)
  112. clone_admin_product_url resource
  113. end
  114. private
  115. def variant_stock_includes
  116. [:images, stock_items: :stock_location, option_values: :option_type]
  117. end
  118. end
  119. end
  120. end