Module: Dugway::Filters::CoreFilters

Defined in:
lib/dugway/liquid/filters/core_filters.rb

Instance Method Summary collapse

Instance Method Details

#contact_input(contact, field, id = nil, class_name = nil) ⇒ Object

Contact Filters



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/dugway/liquid/filters/core_filters.rb', line 125

def contact_input(contact, field, id = nil, class_name = nil)
  field = field.downcase
  id    ||= field
  case field
  when 'message'
    text_area_tag(field, contact['message'], :id => id, :class => class_name, :tabindex => contact_tab_index)
  when 'captcha'
    text_field_tag('captcha', '', :id => id, :class => class_name, :tabindex => contact_tab_index)
  else
    text_field_tag(field, contact[field], :id => id, :class => class_name, :tabindex => contact_tab_index)
  end
end

#country_select(country, label = 'Select your country...', id = 'country', class_name = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/dugway/liquid/filters/core_filters.rb', line 107

def country_select(country, label = 'Select your country...', id = 'country', class_name = nil)
  divider = '------------'
  countries = [label, divider, country['name'], divider, 'Aland Islands', 'Afghanistan', 'Albania', 'Algeria', 'American Samoa', 'Andorra', 'Angola', 'Anguilla', 'Antarctica', 'Antigua And Barbuda', 'Argentina', 'Armenia', 'Aruba', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas', 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin', 'Bermuda', 'Bhutan', 'Bolivia', 'Bosnia and Herzegovina', 'Botswana', 'Bouvet Island', 'Brazil', 'British Indian Ocean Territory', 'British Virgin Islands', 'Brunei Darussalam', 'Bulgaria', 'Burkina Faso', 'Burundi', 'Cambodia', 'Cameroon', 'Canada', 'Cape Verde', 'Cayman Islands', 'Central African Republic', 'Chad', 'Chile', 'China', 'Christmas Island', 'Cocos (Keeling) Islands', 'Colombia', 'Comoros', 'Congo', 'Cook Islands', 'Costa Rica', 'Croatia (Hrvatska)', 'Cuba', 'Cyprus', 'Czech Republic', 'Democratic Republic of the Congo', 'Denmark', 'Djibouti', 'Dominica', 'Dominican Republic', 'East Timor', 'Ecuador', 'Egypt', 'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Estonia', 'Ethiopia', 'Falkland Islands (Malvinas)', 'Faroe Islands', 'Fiji', 'Finland', 'France', 'France, Metropolitan', 'French Guiana', 'French Polynesia', 'French Southern Territories', 'Gabon', 'Gambia', 'Georgia', 'Germany', 'Ghana', 'Gibraltar', 'Greece', 'Greenland', 'Grenada', 'Guadeloupe', 'Guam', 'Guatemala', 'Guernsey', 'Guinea', 'Guinea-Bissau', 'Guyana', 'Haiti', 'Heard and McDonald Islands', 'Honduras', 'Hong Kong', 'Hungary', 'Iceland', 'India', 'Indonesia', 'Iran', 'Iraq', 'Ireland', 'Isle of Man', 'Israel', 'Italy', 'Jamaica', 'Japan', 'Jersey', 'Jordan', 'Kazakhstan', 'Kenya', 'Kiribati', 'Kuwait', 'Kyrgyzstan (Kyrgyz Republic)', 'Laos', 'Latvia', 'Lebanon', 'Lesotho', 'Liberia', 'Libya', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Macau', 'Macedonia', 'Madagascar', 'Malaysia', 'Maldives', 'Mali', 'Malta', 'Marshall Islands', 'Martinique', 'Mauritania', 'Mauritius', 'Mayotte', 'Mexico', 'Micronesia', 'Moldova', 'Monaco', 'Mongolia', 'Montserrat', 'Morocco', 'Mozambique', 'Myanmar', 'Namibia', 'Nauru', 'Nepal', 'Netherlands', 'Netherlands Antilles', 'Neutral Zone (Saudia Arabia/Iraq)', 'New Caledonia', 'New Zealand', 'Nicaragua', 'Niger', 'Nigeria', 'Niue', 'Norfolk Island', 'Northern Mariana Islands', 'Norway', 'Oman', 'Pakistan', 'Palau', 'Palestine', 'Panama', 'Papua New Guinea', 'Paraguay', 'Peru', 'Philippines', 'Pitcairn', 'Poland', 'Portugal', 'Puerto Rico', 'Qatar', 'Reunion', 'Romania', 'Russian Federation', 'Rwanda', 'S. Georgia and S. Sandwich Isls.', 'Saint Kitts and Nevis', 'Saint Lucia', 'Saint Vincent and The Grenadines', 'Samoa', 'San Marino', 'Sao Tome and Principe', 'Saudi Arabia', 'Senegal', 'Seychelles', 'Sierra Leone', 'Singapore', 'Slovakia (Slovak Republic)', 'Slovenia', 'Solomon Islands', 'Somalia', 'South Africa', 'South Korea', 'Soviet Union (former)', 'Spain', 'Sri Lanka', 'St. Helena', 'St. Pierre and Miquelon', 'Sudan', 'Suriname', 'Svalbard and Jan Mayen Islands', 'Swaziland', 'Sweden', 'Switzerland', 'Syria', 'Taiwan', 'Tajikistan', 'Tanzania', 'Thailand', 'Timore-Leste', 'Togo', 'Tokelau', 'Tonga', 'Trinidad and Tobago', 'Tunisia', 'Turkey', 'Turkmenistan', 'Turks and Caicos Islands', 'Tuvalu', 'Uganda', 'Ukraine', 'United Arab Emirates', 'United Kingdom', 'United States', 'United States Minor Outlying Islands', 'Uruguay', 'Uzbekistan', 'Vanuatu', 'Vatican City State (Holy See)', 'Venezuela', 'Viet Nam', 'Virgin Islands (US)', 'Wallis and Futuna Islands', 'Western Sahara', 'Yemen', 'Yugoslavia', 'Zaire', 'Zambia', 'Zimbabwe']

  options = countries.inject('') do |t, c|
    v = c == label || c == divider ? '' : Digest::SHA1.hexdigest(c) # give it a random ID that'll be consistent
    t += option_tag(c, v)
  end

  select_tag('cart[country_id]', options, { :id => id, :class => class_name })
end

#discount_code_input(discount, id = 'cart_discount_code', class_name = nil) ⇒ Object



119
120
121
# File 'lib/dugway/liquid/filters/core_filters.rb', line 119

def discount_code_input(discount, id = 'cart_discount_code', class_name = nil)
  text_field_tag('cart[discount_code]', nil, { :id => id, :class => class_name, :autocomplete => 'off' })
end

#hidden_option_input(option, id = 'option', class_name = nil) ⇒ Object

Input Filters



74
75
76
# File 'lib/dugway/liquid/filters/core_filters.rb', line 74

def hidden_option_input(option, id = 'option', class_name = nil)
  hidden_field_tag('cart[add][id]', option['id'], { :id => id, :class => class_name })
end

#item_quantity_input(item, id = nil, class_name = nil) ⇒ Object



103
104
105
# File 'lib/dugway/liquid/filters/core_filters.rb', line 103

def item_quantity_input(item, id = nil, class_name = nil)
  text_field_tag("cart[update][#{ item['id'] }]", item['quantity'], { :id => id || "item_#{ item['id'] }_qty", :class => class_name, :autocomplete => 'off' })
end

#money(amount, format = nil) ⇒ Object

Money Filters



38
39
40
41
42
43
44
45
# File 'lib/dugway/liquid/filters/core_filters.rb', line 38

def money(amount, format = nil)
  case format
  when 'sign' then money_with_sign(amount)
  when 'code' then money_with_code(amount)
  when 'sign_and_code' then money_with_sign_and_code(amount)
  else number_to_currency(amount, :unit => '').strip
  end
end

#money_with_code(amount) ⇒ Object



52
53
54
# File 'lib/dugway/liquid/filters/core_filters.rb', line 52

def money_with_code(amount)
  %{#{ money(amount) } <span class="currency_code">#{ currency['code'] }</span>}
end

#money_with_sign(amount) ⇒ Object



47
48
49
50
# File 'lib/dugway/liquid/filters/core_filters.rb', line 47

def money_with_sign(amount)
  unit = I18n.translate('number.currency.format.unit')
  number_to_currency(amount).gsub(unit, %{<span class="currency_sign">#{ HTMLEntities.new.encode(unit, :named) }</span>})
end

#money_with_sign_and_code(amount) ⇒ Object



56
57
58
# File 'lib/dugway/liquid/filters/core_filters.rb', line 56

def money_with_sign_and_code(amount)
  %{#{ money_with_sign(amount) } <span class="currency_code">#{ currency['code'] }</span>}
end

#options_radio(options, id = 'option', class_name = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dugway/liquid/filters/core_filters.rb', line 84

def options_radio(options, id = 'option', class_name = nil)
  list = ''

  for o in options
    list += '<li>'
    list += radio_button_tag('cart[add][id]', o['id'], o['default'], { :id => "option_#{o['id']}", :class => class_name })
    list += %{ <label for="option_#{o['id']}">#{o['name']}}
    list += %{ - #{ money_with_sign(o['price']) }} if o['has_custom_price']
    list += %{</label>}
    list += '</li>'
  end

  %{<ul id="#{id}">#{list}</ul>}
end

#options_select(options, id = 'option', class_name = nil) ⇒ Object



78
79
80
81
82
# File 'lib/dugway/liquid/filters/core_filters.rb', line 78

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

#paragraphs(text) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dugway/liquid/filters/core_filters.rb', line 20

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<br />')  # 1 newline -> br
      result << "<p>#{block}</p>"
    end
    result << "\n"
  end
end

#pluralize(count, singular, plural = nil) ⇒ Object

Text Filters



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/dugway/liquid/filters/core_filters.rb', line 8

def pluralize(count, singular, plural = nil)
  "#{count} " + if count == 1
    singular
  elsif plural
    plural
  elsif ActiveSupport.const_defined?(:Inflector)
    ActiveSupport::Inflector.pluralize(singular)
  else
    singular + "s"
  end
end

#product_quantity_input(product, quantity = 1, id = nil, class_name = nil) ⇒ Object



99
100
101
# File 'lib/dugway/liquid/filters/core_filters.rb', line 99

def product_quantity_input(product, quantity = 1, id = nil, class_name = nil)
  text_field_tag("cart[add][quantity]", quantity, { :id => id || "quantity_#{product['id']}", :class => class_name, :autocomplete => 'off' })
end

#shipping_name(area, everywhere = 'Everywhere', everywhere_else = 'Everywhere else') ⇒ Object

Shipping Filters



62
63
64
65
66
67
68
69
70
# File 'lib/dugway/liquid/filters/core_filters.rb', line 62

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