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

https://github.com/crystalneth/spree · Ruby · 123 lines · 95 code · 23 blank · 5 comment · 6 complexity · 9c96f965700dc075a9be92b73f9a6426 MD5 · raw file

  1. module Spree
  2. module Admin
  3. class ProductsController < ResourceController
  4. helper 'spree/products'
  5. before_filter :check_json_authenticity, :only => :index
  6. before_filter :load_data, :except => :index
  7. create.before :create_before
  8. update.before :update_before
  9. def show
  10. redirect_to( :action => :edit )
  11. end
  12. def index
  13. respond_with(@collection) do |format|
  14. format.html
  15. format.json { render :json => json_data }
  16. end
  17. end
  18. def destroy
  19. @product = Product.where(:permalink => params[:id]).first!
  20. @product.delete
  21. flash.notice = I18n.t('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.notice = I18n.t('notice_messages.product_cloned')
  31. else
  32. flash.notice = I18n.t('notice_messages.product_not_cloned')
  33. end
  34. respond_with(@new) { |format| format.html { redirect_to edit_admin_product_url(@new) } }
  35. end
  36. protected
  37. def find_resource
  38. Product.find_by_permalink!(params[:id])
  39. end
  40. def location_after_save
  41. edit_admin_product_url(@product)
  42. end
  43. # Allow different formats of json data to suit different ajax calls
  44. def json_data
  45. json_format = params[:json_format] or 'default'
  46. case json_format
  47. when 'basic'
  48. collection.map {|p| {'id' => p.id, 'name' => p.name}}.to_json
  49. else
  50. collection.to_json(:include => {:variants => {:include => {:option_values => {:include => :option_type},
  51. :images => {:only => [:id], :methods => :mini_url}}},
  52. :images => {:only => [:id], :methods => :mini_url}, :master => {}})
  53. end
  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. unless request.xhr?
  64. params[:q] ||= {}
  65. params[:q][:deleted_at_null] ||= "1"
  66. params[:q][:s] ||= "name asc"
  67. @search = super.ransack(params[:q])
  68. @collection = @search.result.
  69. group_by_products_id.
  70. includes([:master, {:variants => [:images, :option_values]}]).
  71. page(params[:page]).
  72. per(Spree::Config[:admin_products_per_page])
  73. if params[:q][:s].include?("master_price")
  74. # By applying the group in the main query we get an undefined method gsub for Arel::Nodes::Descending
  75. # It seems to only work when the price is actually being sorted in the query
  76. # To be investigated later.
  77. @collection = @collection.group("spree_variants.price")
  78. end
  79. else
  80. includes = [{:variants => [:images, {:option_values => :option_type}]}, {:master => :images}]
  81. @collection = super.where(["name #{LIKE} ?", "%#{params[:q]}%"])
  82. @collection = @collection.includes(includes).limit(params[:limit] || 10)
  83. tmp = super.where(["#{Variant.table_name}.sku #{LIKE} ?", "%#{params[:q]}%"])
  84. tmp = tmp.includes(:variants_including_master).limit(params[:limit] || 10)
  85. @collection.concat(tmp)
  86. end
  87. @collection
  88. end
  89. def create_before
  90. return if params[:product][:prototype_id].blank?
  91. @prototype = Spree::Prototype.find(params[:product][:prototype_id])
  92. end
  93. def update_before
  94. # note: we only reset the product properties if we're receiving a post from the form on that tab
  95. return unless params[:clear_product_properties]
  96. params[:product] ||= {}
  97. end
  98. end
  99. end
  100. end