Class: Admin::ProductsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/admin/products_controller.rb

Instance Method Summary collapse

Instance Method Details

#cloneObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/admin/products_controller.rb', line 51

def clone
  load_object
  @new = @product.duplicate

  if @new.save
    flash.notice = I18n.t("notice_messages.product_cloned")
  else
    flash.notice = I18n.t("notice_messages.product_not_cloned")
  end

  redirect_to edit_admin_product_url(@new)
end

#collectionObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/admin/products_controller.rb', line 80

def collection
  return @collection if @collection.present?
  scopes = ['group_by_products_id']

  unless request.xhr?
    # Note: the SL scopes are on/off switches, so we need to select "not_deleted" explicitly if the switch is off
    # QUERY - better as named scope or as SL scope?
    if params[:search].nil? || params[:search][:deleted_at_not_null].blank?
      scopes << 'not_deleted'
    end

    @search = end_of_association_chain.searchlogic(params[:search] ? params[:search].except(:deleted_at_not_null) : nil)
    @search.order ||= "ascend_by_name"

    @collection = @search.do_search.instance_eval(scopes.join(".")).paginate(:include   => {:variants => [:images, :option_values]},
                                   :per_page  => Spree::Config[:admin_products_per_page],
                                   :page      => params[:page])
  else
    includes = [{:variants => [:images,  {:option_values => :option_type}]}, :master, :images]

    @collection = end_of_association_chain.where(["name LIKE ?", "%#{params[:q]}%"]).includes(includes).limit(params[:limit] || 10)
    @collection.concat end_of_association_chain.where(["variants.sku LIKE ?", "%#{params[:q]}%"]).includes(:variants_including_master).limit(params[:limit] || 10)

    @collection.uniq
  end

end

#destroyObject

override the destory method to set deleted_at value instead of actually deleting the product.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/admin/products_controller.rb', line 30

def destroy
  @product = Product.find_by_permalink(params[:id])
  @product.deleted_at = Time.now()

  @product.variants.each do |v|
    v.deleted_at = Time.now()
    v.save
  end

  if @product.save
    flash.notice = I18n.t("notice_messages.product_deleted")
  else
    flash.notice = I18n.t("notice_messages.product_not_deleted")
  end

  respond_to do |format|
    format.html { redirect_to collection_url }
    format.js  { render_js_for_destroy }
  end
end

#json_dataObject

Allow different formats of json data to suit different ajax calls



65
66
67
68
69
70
71
72
73
# File 'app/controllers/admin/products_controller.rb', line 65

def json_data
  json_format = params[:json_format] or 'default'
  case json_format
  when 'basic'
    collection.map {|p| {'id' => p.id, 'name' => p.name}}.to_json
  else
    collection.to_json(:include => {:variants => {:include => {:option_values => {:include => :option_type}, :images => {}}}, :images => {}, :master => {}})
  end
end

#load_dataObject



75
76
77
78
# File 'app/controllers/admin/products_controller.rb', line 75

def load_data
  @tax_categories = TaxCategory.order(:name)
  @shipping_categories = ShippingCategory.order(:name)
end

#update_beforeObject



108
109
110
111
112
# File 'app/controllers/admin/products_controller.rb', line 108

def update_before
  # note: we only reset the product properties if we're receiving a post from the form on that tab
  return unless params[:clear_product_properties]
  params[:product] ||= {}
end