require 'htmlentities'
module Dugway
module Filters
module CoreFilters
# Text Filters
def pluralize(count, singular, plural = nil)
"#{count} " + if count == 1
singular
elsif plural
plural
else
singular + "s"
end
end
def paragraphs(text)
tags = '(table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr|script|embed|object|param|code|center|dir|fieldset|menu|noframes|noscript|frameset|applet|button|del|iframe|ins)'
text = text.to_s.gsub(/\r\n?/, "\n") # \r\n and \r -> \n
blocks = text.split(/\n\n+/) # split on double newlines
blocks.inject('') do | result, block |
block.strip!
if block =~ /<\s*(\/)?\s*#{tags}\s*(\/)?\s*>.*(<\s*\/\s*\1\s*>)?/i
result << block
else
block.gsub!(/([^\n]\n)(?=[^\n])/, '\1
') # 1 newline -> br
result << "
#{block}
" end result << "\n" end end # Money Filters def money(amount) number_to_currency(amount, :locale => nil, :precision => I18n.translate('number.currency.format.precision'), :unit => '', :separator => I18n.translate('number.currency.format.separator'), :delimiter => I18n.translate('number.currency.format.delimiter'), :format => I18n.translate('number.currency.format.format')).strip end def money_with_sign(amount) unit = I18n.translate('number.currency.format.unit') number_to_currency(amount).try(:gsub, unit, "#{ HTMLEntities.new.encode(unit, :named) }") end def money_with_code(amount) %{#{ money(amount) } #{ currency.code }} end def money_with_sign_and_code(amount) %{#{ money_with_sign(amount) } #{ currency.code }} end # Shipping Filters def shipping_name(area, everywhere = 'Everywhere', everywhere_else = 'Everywhere else') if area.country.name.present? area.country['name'] elsif area.strict everywhere_else else everywhere end end # Input Filters def hidden_option_input(option, id = 'option', class_name = nil) hidden_field_tag('cart[add][id]', option['id'], { :id => id, :class => class_name }) end def options_select(options, id = 'option', class_name = nil) options = options.present? ? options : [] option_tags = options.inject('') { |t,o| t += option_tag((o['name'] + (o['has_custom_price'] ? " - #{ money_with_sign(o['price']) }" : '')), o['id']) } select_tag('cart[add][id]', option_tags, { :id => id, :class => class_name }) end def options_radio(options, id = 'option', class_name = nil) list = '' for o in options list += '