This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
multi_site_extension.rb
182 lines (157 loc) · 7.44 KB
/
multi_site_extension.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
require_dependency 'application'
class MultiSiteExtension < Spree::Extension
version "1.0"
description "Extention that will allow the store to support multiple sites each having their own taxonomies, products and orders"
url "git://github.com/tunagami/spree-multi-site.git"
def activate
# admin.tabs.add "Multi Site", "/admin/multi_site", :after => "Layouts", :visibility => [:all]
#############################################################################
# Overriding Spree Core Models
Taxonomy.class_eval do
belongs_to :site
named_scope :by_site_with_children, lambda {|site| {:conditions => ["taxonomies.site_id in (?)", site.self_and_children]}}
end
Product.class_eval do
belongs_to :site
named_scope :by_site, lambda {|site| {:conditions => ["products.site_id = ?", site.id]}}
named_scope :by_site_with_children, lambda {|site| {:conditions => ["products.site_id in (?)", site.self_and_children]}}
end
Order.class_eval do
belongs_to :site
named_scope :by_site_with_children, lambda {|site| {:conditions => ["orders.site_id in (?)", site.self_and_children]}}
end
#############################################################################
#############################################################################
# Overriding Spree Controllers
ApplicationController.class_eval do
include MultiSiteSystem
def instantiate_controller_and_action_names
@current_action = action_name
@current_controller = controller_name
end
end
Spree::BaseController.class_eval do
before_filter :get_site_and_products
layout :get_layout
def get_layout
@site.layout.empty? ? "application" : @site.layout
end
def find_order
unless session[:order_id].blank?
@order = Order.find_or_create_by_id(session[:order_id])
else
@order = Order.create
end
@order.site = @site
session[:order_id] = @order.id
@order
end
end
Admin::TaxonomiesController.class_eval do
before_filter :load_data
private
def load_data
@sites = @site.self_and_children
end
def collection
@collection = Taxonomy.by_site_with_children(@site)
end
end
Admin::TaxonsController.class_eval do
def available
if params[:q].blank?
@available_taxons = []
else
@available_taxons = @site.taxons.scoped(:conditions => ['lower(taxons.name) LIKE ?', "%#{params[:q].downcase}%"])
end
@available_taxons.delete_if { |taxon| @product.taxons.include?(taxon) }
respond_to do |format|
format.html
format.js {render :layout => false}
end
end
end
Admin::OrdersController.class_eval do
private
def collection
default_stop = (Date.today + 1).to_s(:db)
@filter = params.has_key?(:filter) ? OrderFilter.new(params[:filter]) : OrderFilter.new
scope = Order.by_site_with_children(@site).scoped(:include => [:shipments, {:creditcards => :address}])
scope = scope.by_number @filter.number unless @filter.number.blank?
scope = scope.by_customer @filter.customer unless @filter.customer.blank?
scope = scope.between(@filter.start, (@filter.stop.blank? ? default_stop : @filter.stop)) unless @filter.start.blank?
scope = scope.by_state @filter.state.classify.downcase.gsub(" ", "_") unless @filter.state.blank?
scope = scope.conditions "lower(addresses.firstname) LIKE ?", "%#{@filter.firstname.downcase}%" unless @filter.firstname.blank?
scope = scope.conditions "lower(addresses.lastname) LIKE ?", "%#{@filter.lastname.downcase}%" unless @filter.lastname.blank?
scope = scope.checkout_completed(@filter.checkout == '1' ? false : true)
@collection = scope.find(:all, :order => 'orders.created_at DESC', :include => :user, :page => {:size => Spree::Config[:orders_per_page], :current =>params[:p], :first => 1})
end
end
Admin::ProductsController.class_eval do
before_filter :load_data
private
def load_data
@sites = @site.self_and_children
@tax_categories = TaxCategory.find(:all, :order=>"name")
@shipping_categories = ShippingCategory.find(:all, :order=>"name")
end
def collection
@name = params[:name] || ""
@sku = params[:sku] || ""
@deleted = (params.key?(:deleted) && params[:deleted] == "on") ? "checked" : ""
if @sku.blank?
if @deleted.blank?
@collection ||= end_of_association_chain.by_site_with_children(@site).active.by_name(@name).find(:all, :order => :name, :page => {:start => 1, :size => Spree::Config[:admin_products_per_page], :current => params[:p]})
else
@collection ||= end_of_association_chain.by_site_with_children(@site).deleted.by_name(@name).find(:all, :order => :name, :page => {:start => 1, :size => Spree::Config[:admin_products_per_page], :current => params[:p]})
end
else
if @deleted.blank?
@collection ||= end_of_association_chain.by_site_with_children(@site).active.by_name(@name).by_sku(@sku).find(:all, :order => :name, :page => {:start => 1, :size => Spree::Config[:admin_products_per_page], :current => params[:p]})
else
@collection ||= end_of_association_chain.by_site_with_children(@site).deleted.by_name(@name).by_sku(@sku).find(:all, :order => :name, :page => {:start => 1, :size => Spree::Config[:admin_products_per_page], :current => params[:p]})
end
end
end
end
ProductsController.class_eval do
private
def collection
if params[:taxon]
@taxon = Taxon.find(params[:taxon])
@collection ||= Product.by_site(@site).active.find(
:all,
:conditions => ["products.id in (select product_id from products_taxons where taxon_id in (" + @taxon.descendents.inject( @taxon.id.to_s) { |clause, t| clause += ', ' + t.id.to_s} + "))" ],
:page => {:start => 1, :size => Spree::Config[:products_per_page], :current => params[:p]},
:include => :images)
else
@collection ||= Product.by_site(@site).active.find(:all, :page => {:start => 1, :size => Spree::Config[:products_per_page], :current => params[:p]}, :include => :images)
end
end
end
TaxonsController.class_eval do
private
def load_data
@products ||= object.products.by_site(@site).active.find(:all, :page => {:start => 1, :size => Spree::Config[:products_per_page], :current => params[:p]}, :include => :images)
@product_cols = 3
end
end
#############################################################################
#############################################################################
# Overriding Spree Helpers
TaxonsHelper.class_eval do
def taxon_preview(taxon)
products = taxon.products.by_site(@site).active[0..4]
return products unless products.size < 5
if Spree::Config[:show_descendents]
taxon.descendents.each do |taxon|
products += taxon.products.by_site(@site).active[0..4]
break if products.size >= 5
end
end
products[0..4]
end
end
#############################################################################
end
end