product.rb
883 Bytes
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
class Product < ActiveRecord::Base
belongs_to :enterprise
belongs_to :product_category
validates_presence_of :name
validates_uniqueness_of :name, :scope => :enterprise_id
validates_numericality_of :price, :allow_nil => true
has_one :image, :as => :owner
after_update :save_image
acts_as_searchable :fields => [ :name, :description, :category_full_name ]
xss_terminate :only => [ :name, :description ]
def category_full_name
product_category.full_name(" ")
end
def image_builder=(img)
if image && image.id == img[:id]
image.attributes = img
else
build_image(img)
end
end
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
end