/
products_controller.rb
128 lines (104 loc) 路 4.1 KB
/
products_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
module Spree
module Admin
class ProductsController < ResourceController
helper 'spree/products'
before_filter :check_json_authenticity, :only => :index
before_filter :load_data, :except => :index
create.before :create_before
update.before :update_before
def show
redirect_to( :action => :edit )
end
def index
respond_with(@collection) do |format|
format.html
format.json { render :json => json_data }
end
end
def update
if params[:product][:taxon_ids].present?
params[:product][:taxon_ids] = params[:product][:taxon_ids].split(',')
end
super
end
def destroy
@product = Product.where(:permalink => params[:id]).first!
@product.delete
flash.notice = I18n.t('notice_messages.product_deleted')
respond_with(@product) do |format|
format.html { redirect_to collection_url }
format.js { render_js_for_destroy }
end
end
def clone
@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
respond_with(@new) { |format| format.html { redirect_to edit_admin_product_url(@new) } }
end
protected
def find_resource
Product.find_by_permalink!(params[:id])
end
def location_after_save
edit_admin_product_url(@product)
end
# Allow different formats of json data to suit different ajax calls
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 => {:only => [:id], :methods => :mini_url}}},
:images => {:only => [:id], :methods => :mini_url}, :master => {}})
end
end
def load_data
@taxons = Taxon.order(:name)
@option_types = OptionType.order(:name)
@tax_categories = TaxCategory.order(:name)
@shipping_categories = ShippingCategory.order(:name)
end
def collection
return @collection if @collection.present?
unless request.xhr?
params[:q] ||= {}
params[:q][:deleted_at_null] ||= "1"
params[:q][:s] ||= "name asc"
@search = super.ransack(params[:q])
@collection = @search.result.
group_by_products_id.
includes([:master, {:variants => [:images, :option_values]}]).
page(params[:page]).
per(Spree::Config[:admin_products_per_page])
# PostgreSQL compatibility
if params[:q][:s].include?("master_price")
@collection = @collection.group("spree_variants.price")
end
else
includes = [{:variants => [:images, {:option_values => :option_type}]}, {:master => :images}]
@collection = super.where(["name #{LIKE} ?", "%#{params[:q]}%"])
@collection = @collection.includes(includes).limit(params[:limit] || 10)
tmp = super.where(["#{Variant.table_name}.sku #{LIKE} ?", "%#{params[:q]}%"])
tmp = tmp.includes(:variants_including_master).limit(params[:limit] || 10)
@collection.concat(tmp).uniq!
end
@collection
end
def create_before
return if params[:product][:prototype_id].blank?
@prototype = Spree::Prototype.find(params[:product][:prototype_id])
end
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
end
end
end