PageRenderTime 1691ms CodeModel.GetById 42ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/BDQ/spree
Ruby | 127 lines | 96 code | 24 blank | 7 comment | 6 complexity | 6f57e810cbfbc3ce9998e955790fd6c7 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. # override the destory method to set deleted_at value
  19. # instead of actually deleting the product.
  20. def destroy
  21. @product = Product.find_by_permalink!(params[:id])
  22. @product.update_column(:deleted_at, Time.now)
  23. @product.variants_including_master.update_all(:deleted_at => Time.now)
  24. flash.notice = I18n.t('notice_messages.product_deleted')
  25. respond_with(@product) do |format|
  26. format.html { redirect_to collection_url }
  27. format.js { render_js_for_destroy }
  28. end
  29. end
  30. def clone
  31. @new = @product.duplicate
  32. if @new.save
  33. flash.notice = I18n.t('notice_messages.product_cloned')
  34. else
  35. flash.notice = I18n.t('notice_messages.product_not_cloned')
  36. end
  37. respond_with(@new) { |format| format.html { redirect_to edit_admin_product_url(@new) } }
  38. end
  39. protected
  40. def find_resource
  41. Product.find_by_permalink!(params[:id])
  42. end
  43. def location_after_save
  44. edit_admin_product_url(@product)
  45. end
  46. # Allow different formats of json data to suit different ajax calls
  47. def json_data
  48. json_format = params[:json_format] or 'default'
  49. case json_format
  50. when 'basic'
  51. collection.map {|p| {'id' => p.id, 'name' => p.name}}.to_json
  52. else
  53. collection.to_json(:include => {:variants => {:include => {:option_values => {:include => :option_type},
  54. :images => {:only => [:id], :methods => :mini_url}}},
  55. :images => {:only => [:id], :methods => :mini_url}, :master => {}})
  56. end
  57. end
  58. def load_data
  59. @taxons = Taxon.order(:name)
  60. @option_types = OptionType.order(:name)
  61. @tax_categories = TaxCategory.order(:name)
  62. @shipping_categories = ShippingCategory.order(:name)
  63. end
  64. def collection
  65. return @collection if @collection.present?
  66. unless request.xhr?
  67. params[:q] ||= {}
  68. params[:q][:deleted_at_null] ||= "1"
  69. params[:q][:s] ||= "name asc"
  70. @search = super.ransack(params[:q])
  71. @collection = @search.result.
  72. group_by_products_id.
  73. includes([:master, {:variants => [:images, :option_values]}]).
  74. page(params[:page]).
  75. per(Spree::Config[:admin_products_per_page])
  76. if params[:q][:s].include?("master_price")
  77. # By applying the group in the main query we get an undefined method gsub for Arel::Nodes::Descending
  78. # It seems to only work when the price is actually being sorted in the query
  79. # To be investigated later.
  80. @collection = @collection.group("spree_variants.price")
  81. end
  82. else
  83. includes = [{:variants => [:images, {:option_values => :option_type}]}, {:master => :images}]
  84. @collection = super.where(["name #{LIKE} ?", "%#{params[:q]}%"])
  85. @collection = @collection.includes(includes).limit(params[:limit] || 10)
  86. tmp = super.where(["#{Variant.table_name}.sku #{LIKE} ?", "%#{params[:q]}%"])
  87. tmp = tmp.includes(:variants_including_master).limit(params[:limit] || 10)
  88. @collection.concat(tmp)
  89. end
  90. @collection
  91. end
  92. def create_before
  93. return if params[:product][:prototype_id].blank?
  94. @prototype = Spree::Prototype.find(params[:product][:prototype_id])
  95. end
  96. def update_before
  97. # note: we only reset the product properties if we're receiving a post from the form on that tab
  98. return unless params[:clear_product_properties]
  99. params[:product] ||= {}
  100. end
  101. end
  102. end
  103. end