PageRenderTime 1583ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/jmbejar/spree
Ruby | 115 lines | 90 code | 23 blank | 2 comment | 7 complexity | 87a437a4bc973b2106f64dd821dab3d2 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. def show
  9. session[:return_to] ||= request.referer
  10. redirect_to( :action => :edit )
  11. end
  12. def index
  13. session[:return_to] = request.url
  14. respond_with(@collection)
  15. end
  16. def update
  17. if params[:product][:taxon_ids].present?
  18. params[:product][:taxon_ids] = params[:product][:taxon_ids].split(',')
  19. end
  20. if params[:product][:option_type_ids].present?
  21. params[:product][:option_type_ids] = params[:product][:option_type_ids].split(',')
  22. end
  23. super
  24. end
  25. def destroy
  26. @product = Product.find_by_permalink!(params[:id])
  27. @product.delete
  28. flash[:success] = Spree.t('notice_messages.product_deleted')
  29. respond_with(@product) do |format|
  30. format.html { redirect_to collection_url }
  31. format.js { render_js_for_destroy }
  32. end
  33. end
  34. def clone
  35. @new = @product.duplicate
  36. if @new.save
  37. flash[:success] = Spree.t('notice_messages.product_cloned')
  38. else
  39. flash[:success] = Spree.t('notice_messages.product_not_cloned')
  40. end
  41. redirect_to edit_admin_product_url(@new)
  42. end
  43. def stock
  44. @variants = @product.variants
  45. @variants = [@product.master] if @variants.empty?
  46. @stock_locations = StockLocation.accessible_by(current_ability, :read)
  47. end
  48. protected
  49. def find_resource
  50. Product.find_by_permalink!(params[:id])
  51. end
  52. def location_after_save
  53. spree.edit_admin_product_url(@product)
  54. end
  55. def load_data
  56. @taxons = Taxon.order(:name)
  57. @option_types = OptionType.order(:name)
  58. @tax_categories = TaxCategory.order(:name)
  59. @shipping_categories = ShippingCategory.order(:name)
  60. end
  61. def collection
  62. return @collection if @collection.present?
  63. params[:q] ||= {}
  64. params[:q][:deleted_at_null] ||= "1"
  65. params[:q][:s] ||= "name asc"
  66. @search = super.ransack(params[:q])
  67. @collection = @search.result.
  68. group_by_products_id.
  69. includes(product_includes).
  70. page(params[:page]).
  71. per(Spree::Config[:admin_products_per_page])
  72. if params[:q][:s].include?("master_default_price_amount")
  73. # PostgreSQL compatibility
  74. @collection = @collection.group("spree_prices.amount")
  75. end
  76. @collection
  77. end
  78. def create_before
  79. return if params[:product][:prototype_id].blank?
  80. @prototype = Spree::Prototype.find(params[:product][:prototype_id])
  81. end
  82. def update_before
  83. # note: we only reset the product properties if we're receiving a post from the form on that tab
  84. return unless params[:clear_product_properties]
  85. params[:product] ||= {}
  86. end
  87. def product_includes
  88. [{:variants => [:images, {:option_values => :option_type}]}, {:master => [:images, :default_price]}]
  89. end
  90. end
  91. end
  92. end