manage_products_helper.rb
8.36 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
module ManageProductsHelper
def remote_function_to_update_categories_selection(container_id, options = {})
remote_function({
:update => container_id,
:url => { :action => "categories_for_selection" },
:loading => "loading('hierarchy_navigation', '#{ _('loading…') }'); loading('#{container_id}', ' ')",
:complete => "loading_done('hierarchy_navigation'); loading_done('#{container_id}')"
}.merge(options))
end
def hierarchy_category_item(category, make_links, title = nil)
title ||= category.name
if make_links
link_to(title, '#',
:title => title,
:onclick => remote_function_to_update_categories_selection("categories_container_level#{ category.level + 1 }",
:with => "'category_id=#{ category.id }'"
)
)
else
title
end
end
def hierarchy_category_navigation(current_category, options = {})
hierarchy = []
if current_category
hierarchy << current_category.name
count_chars = current_category.name.length
ancestors = current_category.ancestors
toplevel = ancestors.pop
if toplevel
count_chars += toplevel.name.length
end
ancestors.each do |category|
if count_chars > 55
hierarchy << hierarchy_category_item(category, options[:make_links], '( … )')
break
else
hierarchy << hierarchy_category_item(category, options[:make_links])
end
count_chars += category.name.length
end
if toplevel
hierarchy << hierarchy_category_item(toplevel, options[:make_links])
end
end
hierarchy.reverse.join(options[:separator] || ' → ')
end
def options_for_select_categories(categories, selected = nil)
categories.sort_by{|cat| cat.name.transliterate}.map do |category|
selected_attribute = selected.nil? ? '' : (category == selected ? "selected='selected'" : '')
"<option value='#{category.id}' #{selected_attribute}>#{category.name + (category.leaf? ? '': ' »')}</option>"
end.join("\n")
end
def build_selects_for_ancestors(ancestors, last_level)
current_category = ancestors.shift
if current_category.nil?
content_tag('div', '<!-- no categories -->',
:class => 'categories_container',
:id => "categories_container_level#{ last_level }"
)
else
content_tag('div',
select_tag('category_id',
options_for_select_categories(current_category.siblings + [current_category], current_category),
:size => 10,
:onchange => remote_function_to_update_categories_selection("categories_container_level#{ current_category.level + 1 }", :with => "'category_id=' + this.value")
) +
build_selects_for_ancestors(ancestors, last_level),
:class => 'categories_container',
:id => "categories_container_level#{ current_category.level }"
)
end
end
def selects_for_all_ancestors(current_category)
build_selects_for_ancestors(current_category.ancestors.reverse + [current_category], current_category.level + 1)
end
def select_for_new_category
content_tag('div',
render(:partial => 'categories_for_selection'),
:class => 'categories_container',
:id => 'categories_container_level0'
)
end
def categories_container(categories_selection_html, hierarchy_html = '')
hidden_field_tag('selected_category_id') +
content_tag('div', hierarchy_html, :id => 'hierarchy_navigation') +
content_tag('div', categories_selection_html, :id => 'categories_container_wrapper')
end
def select_for_categories(categories, level = 0)
if categories.empty?
content_tag('div', '', :id => 'no_subcategories')
else
select_tag('category_id',
options_for_select_categories(categories),
:size => 10,
:onchange => remote_function_to_update_categories_selection("categories_container_level#{ level + 1 }", :with => "'category_id=' + this.value")
) +
content_tag('div', '', :class => 'categories_container', :id => "categories_container_level#{ level + 1 }")
end
end
def edit_product_link(field, label, url, html_options = {})
return '' unless (user && user.has_permission?('manage_products', profile))
options = html_options.merge(:id => "link-edit-#{field}")
link_to(label, url, options)
end
def edit_product_link_to_remote(product, field, label, html_options = {})
return '' unless (user && user.has_permission?('manage_products', profile))
options = html_options.merge(:id => 'link-edit-product-' + field)
link_to_remote(label,
{:update => "product-#{field}",
:url => { :controller => 'manage_products', :action => "edit", :id => product.id, :field => field },
:method => :get},
options)
end
def edit_product_button(product, field, label, html_options = {})
the_class = 'button with-text icon-edit'
if html_options.has_key?(:class)
the_class << ' ' << html_options[:class]
end
edit_product_link_to_remote(product, field, label, html_options.merge(:class => the_class))
end
def edit_product_ui_button(field, label, url, html_options = {})
return '' unless (user && user.has_permission?('manage_products', profile))
options = html_options.merge(:id => 'edit-product-button-ui-' + field)
ui_button(label, url, options)
end
def edit_product_ui_button_to_remote(product, field, label, html_options = {})
return '' unless (user && user.has_permission?('manage_products', profile))
options = html_options.merge(:id => 'edit-product-remote-button-ui-' + field)
ui_button_to_remote(label,
{:update => "product-#{field}",
:url => { :controller => 'manage_products', :action => "edit", :id => product.id, :field => field },
:complete => "$('edit-product-button-ui-#{field}').hide()",
:method => :get},
options)
end
def cancel_edit_product_link(product, field, html_options = {})
return '' unless (user && user.has_permission?('manage_products', profile))
button_to_function(:cancel, _('Cancel'), nil, html_options) do |page|
page.replace_html "product-#{field}", :partial => "display_#{field}", :locals => {:product => product}
end
end
def float_to_currency(value)
number_to_currency(value, :unit => environment.currency_unit, :separator => environment.currency_separator, :delimiter => environment.currency_delimiter, :format => "%u %n")
end
def display_value(product)
price = product.price
unless price.blank? || price.zero?
unit = product.unit
return '' if unit.blank?
discount = product.discount
if discount.blank? || discount.zero?
display_price(_('Price: '), price, unit)
else
display_price_with_discount(price, unit, product.price_with_discount)
end
else
''
end
end
def display_price(label, price, unit)
content_tag('span', label, :class => 'field-name') +
content_tag('span', float_to_currency(price), :class => 'field-value') +
' (%s)' % unit
end
def display_price_with_discount(price, unit, price_with_discount)
original_value = content_tag('span', float_to_currency(price), :class => 'field-value')
discount_value = display_price(_('On sale: '), price_with_discount, unit)
content_tag('span', _('List price: '), :class => 'field-name') + original_value + "<p/>" + discount_value
end
def display_qualifiers(product)
data = ''
product.product_qualifiers.each do |pq|
certified_by = pq.certifier ? _(' certified by %s') % link_to(pq.certifier.name, pq.certifier.link) : ''
data << content_tag('li', '✔ ' + pq.qualifier.name + certified_by, :class => 'product-qualifiers-item')
end
content_tag('ul', data, :id => 'product-qualifiers')
end
def checkboxes_qualifiers(product, qualifier)
check_box_tag("product[qualifiers_list][#{qualifier.id}][qualifier_id]", qualifier.id, product.qualifiers.include?(qualifier)) + qualifier.name
end
def select_certifiers(product, qualifier, certifiers)
relation = product.product_qualifiers.find(:first, :conditions => {:qualifier_id => qualifier.id})
selected = relation.nil? ? 0 : relation.certifier_id
select_tag("product[qualifiers_list][#{qualifier.id}][certifier_id]", options_for_select([[_('Select...') , 0 ]] + certifiers.map {|c|[c.name, c.id]}, selected))
end
end