product.rb
1.59 KB
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
class Product < ActiveRecord::Base
belongs_to :enterprise
belongs_to :product_category
has_many :product_categorizations
validates_presence_of :name
validates_uniqueness_of :name, :scope => :enterprise_id
validates_numericality_of :price, :allow_nil => true
after_update :save_image
after_create do |p|
if p.enterprise
p['lat'] = p.enterprise.lat
p['lng'] = p.enterprise.lng
end
end
after_save do |p|
p.enterprise.product_updated if p.enterprise
end
after_save do |p|
if (p.product_category && !ProductCategorization.find(:first, :conditions => {:category_id => p.product_category.id, :product_id => p.id})) || (!p.product_category)
ProductCategorization.remove_all_for(p)
if p.product_category
ProductCategorization.add_category_to_product(p.product_category, p)
end
end
end
acts_as_searchable :fields => [ :name, :description, :category_full_name ]
xss_terminate :only => [ :name, :description ]
acts_as_mappable
def category_full_name
product_category ? product_category.full_name.split('/') : nil
end
acts_as_having_image
def save_image
image.save if image
end
def category_name
product_category ? product_category.name : _('Uncategorized product')
end
def self.recent(limit = nil)
self.find(:all, :order => 'id desc', :limit => limit)
end
def enterprise_updated(e)
self.lat = e.lat
self.lng = e.lng
save!
end
def url
enterprise.generate_url(:controller => 'catalog', :action => 'show', :id => id)
end
def public?
enterprise.public_profile
end
end