-
Notifications
You must be signed in to change notification settings - Fork 42
/
ordering_in_taxons_extension.rb
67 lines (54 loc) · 1.91 KB
/
ordering_in_taxons_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
# Uncomment this if you reference any of your controllers in activate
# require_dependency 'application'
class OrderingInTaxonsExtension < Spree::Extension
version "1.0"
description "Describe your extension here"
url "http://yourwebsite.com/ordering_in_taxons"
# Please use ordering_in_taxons/config/routes.rb instead for extension routes.
# def self.require_gems(config)
# config.gem "gemname-goes-here", :version => '1.2.3'
# end
def activate
Product.class_eval do
has_many :product_taxons
has_many :taxons, :through=>:product_taxons
#default_scope :include=>:product_taxons, :order=>"product_taxons.position"
named_scope :ordered, {:include=>:product_taxons, :order=>"product_taxons.position"}
named_scope :available, lambda { |*args|
{ :conditions => ["products.available_on <= ?", args.first || Time.zone.now],
:order => 'product_taxons.position',
:include=>:product_taxons
}
}
def in_taxon?(taxon)
case taxon
when String
self.taxons.map{|t| [t.name.downcase,t.permalink.downcase]}.flatten.include?(taxon.strip.downcase)
when Integer
self.taxons.map{|t| t.id}.include?(taxon)
when Taxon
self.taxons.include?(taxon)
else
false
end
end
end
Taxon.class_eval do
has_many :product_taxons,:order=>'product_taxons.position'
has_many :products, :through=>:product_taxons, :order=>'product_taxons.position'
end
Admin::TaxonsController.class_eval do
def reorder_products
params[:product_taxons].each_with_index do |ptid, idx|
pt = ProductTaxon.find(ptid.to_i)
pt.insert_at(idx)
end
head :created
end
end
# make your helper avaliable in all views
# Spree::BaseController.class_eval do
# helper YourHelper
# end
end
end