products_block.rb
1.44 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
class ProductsBlock < Block
attr_accessible :product_ids
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
include ActionView::Helpers
include Rails.application.routes.url_helpers
def self.description
_('Products')
end
def default_title
_('Products')
end
def help
_('This block presents a list of your products.')
end
def content(args={})
block_title(title) +
content_tag(
'ul',
products.map {|product|
content_tag('li',
link_to( product.name,
product.url,
:style => 'background-image:url(%s)' % product.default_image('minor')
),
:class => 'product'
)
}.join
)
end
def footer
link_to(_('View all products'), owner.public_profile_url.merge(:controller => 'catalog', :action => 'index'))
end
settings_items :product_ids, Array
def product_ids=(array)
self.settings[:product_ids] = array
if self.settings[:product_ids]
self.settings[:product_ids] = self.settings[:product_ids].map(&:to_i)
end
end
def products(reload = false)
if product_ids.blank?
products_list = owner.products(reload)
result = []
[4, products_list.size].min.times do
p = products_list.sample
result << p
products_list -= [p]
end
result
else
product_ids.map {|item| owner.products.find(item) }
end
end
end