PageRenderTime 1283ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/jsqu99/spree
Ruby | 138 lines | 112 code | 23 blank | 3 comment | 10 complexity | 58f03970d7cb7cc32d0c1bb2bc47f64a 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. 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 update the product again
  34. @product.slug = @product.slug_was if @product.slug.blank?
  35. invoke_callbacks(:update, :fails)
  36. respond_with(@object)
  37. end
  38. end
  39. def destroy
  40. @product = Product.friendly.find(params[:id])
  41. @product.destroy
  42. flash[:success] = Spree.t('notice_messages.product_deleted')
  43. respond_with(@product) do |format|
  44. format.html { redirect_to collection_url }
  45. format.js { render_js_for_destroy }
  46. end
  47. end
  48. def clone
  49. @new = @product.duplicate
  50. if @new.save
  51. flash[:success] = Spree.t('notice_messages.product_cloned')
  52. else
  53. flash[:success] = Spree.t('notice_messages.product_not_cloned')
  54. end
  55. redirect_to edit_admin_product_url(@new)
  56. end
  57. def stock
  58. @variants = @product.variants
  59. @variants = [@product.master] if @variants.empty?
  60. @stock_locations = StockLocation.accessible_by(current_ability, :read)
  61. if @stock_locations.empty?
  62. flash[:error] = Spree.t(:stock_management_requires_a_stock_location)
  63. redirect_to admin_stock_locations_path
  64. end
  65. end
  66. protected
  67. def find_resource
  68. Product.with_deleted.friendly.find(params[:id])
  69. end
  70. def location_after_save
  71. spree.edit_admin_product_url(@product)
  72. end
  73. def load_data
  74. @taxons = Taxon.order(:name)
  75. @option_types = OptionType.order(:name)
  76. @tax_categories = TaxCategory.order(:name)
  77. @shipping_categories = ShippingCategory.order(:name)
  78. end
  79. def collection
  80. return @collection if @collection.present?
  81. params[:q] ||= {}
  82. params[:q][:deleted_at_null] ||= "1"
  83. params[:q][:s] ||= "name asc"
  84. @collection = super
  85. @collection = @collection.with_deleted if params[:q].delete(:deleted_at_null).blank?
  86. # @search needs to be defined as this is passed to search_form_for
  87. @search = @collection.ransack(params[:q])
  88. @collection = @search.result.
  89. distinct_by_product_ids(params[:q][:s]).
  90. includes(product_includes).
  91. page(params[:page]).
  92. per(Spree::Config[:admin_products_per_page])
  93. @collection
  94. end
  95. def create_before
  96. return if params[:product][:prototype_id].blank?
  97. @prototype = Spree::Prototype.find(params[:product][:prototype_id])
  98. end
  99. def update_before
  100. # note: we only reset the product properties if we're receiving a post from the form on that tab
  101. return unless params[:clear_product_properties]
  102. params[:product] ||= {}
  103. end
  104. def product_includes
  105. [{ :variants => [:images, { :option_values => :option_type }], :master => [:images, :default_price]}]
  106. end
  107. def clone_object_url resource
  108. clone_admin_product_url resource
  109. end
  110. def permit_attributes
  111. params.require(:product).permit!
  112. end
  113. end
  114. end
  115. end