Module: Spree::BaseHelper

Defined in:
app/helpers/spree/base_helper.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



171
172
173
174
175
176
177
178
# File 'app/helpers/spree/base_helper.rb', line 171

def method_missing(method_name, *args, &block)
  if image_style = image_style_from_method_name(method_name)
    define_image_method(image_style)
    self.send(method_name, *args)
  else
    super
  end
end

Instance Method Details

#available_countriesObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/helpers/spree/base_helper.rb', line 128

def available_countries
  checkout_zone = Zone.find_by_name(Spree::Config[:checkout_zone])

  if checkout_zone && checkout_zone.kind == 'country'
    countries = checkout_zone.country_list
  else
    countries = Country.all
  end

  countries.collect do |country|
    country.name = I18n.t(country.iso, :scope => 'countries', :default => country.name)
    country
  end.sort { |a, b| a.name <=> b.name }
end

#body_classObject



80
81
82
83
# File 'app/helpers/spree/base_helper.rb', line 80

def body_class
  @body_class ||= content_for?(:sidebar) ? 'two-col' : 'one-col'
  @body_class
end


100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/helpers/spree/base_helper.rb', line 100

def breadcrumbs(taxon, separator="&nbsp;&raquo;&nbsp;")
  return "" if current_page?("/") || taxon.nil?
  separator = raw(separator)
  crumbs = [(:li, link_to(t(:home) , root_path) + separator)]
  if taxon
    crumbs << (:li, link_to(t(:products) , products_path) + separator)
    crumbs << taxon.ancestors.collect { |ancestor| (:li, link_to(ancestor.name , seo_url(ancestor)) + separator) } unless taxon.ancestors.empty?
    crumbs << (:li, (:span, link_to(taxon.name , seo_url(taxon))))
  else
    crumbs << (:li, (:span, t(:products)))
  end
  crumb_list = (:ul, raw(crumbs.flatten.map{|li| li.mb_chars}.join), :class => 'inline')
  (:nav, crumb_list, :id => 'breadcrumbs')
end

#current_orders_product_countObject



151
152
153
154
155
156
157
# File 'app/helpers/spree/base_helper.rb', line 151

def current_orders_product_count
  if current_order.blank? || current_order.item_count < 1
    return 0
  else
    return current_order.item_count
  end
end

#current_spree_page?(url) ⇒ Boolean

Defined because Rails’ current_page? helper is not working when Spree is mounted at root.

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
# File 'app/helpers/spree/base_helper.rb', line 5

def current_spree_page?(url)
  path = request.fullpath.gsub(/^\/\//, '/')
  if url.is_a?(String)
    return path == url
  elsif url.is_a?(Hash)
    return path == spree.url_for(url)
  end
  return false
end

#flash_messages(opts = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'app/helpers/spree/base_helper.rb', line 89

def flash_messages(opts = {})
  opts[:ignore_types] = [:commerce_tracking].concat(opts[:ignore_types] || [])

  flash.each do |msg_type, text|
    unless opts[:ignore_types].include?(msg_type)
      concat( :div, text, :class => "flash #{msg_type}")
    end
  end
  nil
end

#gem_available?(name) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
164
165
# File 'app/helpers/spree/base_helper.rb', line 159

def gem_available?(name)
   Gem::Specification.find_by_name(name)
rescue Gem::LoadError
   false
rescue
   Gem.available?(name)
end


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/spree/base_helper.rb', line 15

def link_to_cart(text = nil)
  return "" if current_spree_page?(cart_path)

  text = text ? h(text) : t('cart')
  css_class = nil

  if current_order.nil? or current_order.line_items.empty?
    text = "#{text}: (#{t('empty')})"
    css_class = 'empty'
  else
    text = "#{text}: (#{current_order.item_count})  <span class='amount'>#{current_order.display_total}</span>".html_safe
    css_class = 'full'
  end

  link_to text, cart_path, :class => css_class
end

#logo(image_path = ) ⇒ Object



85
86
87
# File 'app/helpers/spree/base_helper.rb', line 85

def (image_path=Spree::Config[:logo])
  link_to image_tag(image_path), root_path
end

#meta_data_tagsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/helpers/spree/base_helper.rb', line 57

def 
  object = instance_variable_get('@'+controller_name.singularize)
  meta = {}

  if object.kind_of? ActiveRecord::Base
    meta[:keywords] = object.meta_keywords if object[:meta_keywords].present?
    meta[:description] = object.meta_description if object[:meta_description].present?
  end

  if meta[:description].blank? && object.kind_of?(Spree::Product)
    meta[:description] = strip_tags(object.description)
  end

  meta.reverse_merge!({
    :keywords => Spree::Config[:default_meta_keywords],
    :description => Spree::Config[:default_meta_description]
  })

  meta.map do |name, content|
    tag('meta', :name => name, :content => content)
  end.join("\n")
end

#money(amount) ⇒ Object



167
168
169
# File 'app/helpers/spree/base_helper.rb', line 167

def money(amount)
  Spree::Money.new(amount)
end

#seo_url(taxon, product = nil) ⇒ Object

generates nested url to product based on supplied taxon



144
145
146
147
148
149
# File 'app/helpers/spree/base_helper.rb', line 144

def seo_url(taxon, product = nil)
  return spree.nested_taxons_path(taxon.permalink) if product.nil?
  warn "DEPRECATION: the /t/taxon-permalink/p/product-permalink urls are "+
    "not used anymore. Use product_url instead. (called from #{caller[0]})"
  return product_url(product)
end

#taxons_tree(root_taxon, current_taxon, max_level = 1) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/helpers/spree/base_helper.rb', line 115

def taxons_tree(root_taxon, current_taxon, max_level = 1)
  return '' if max_level < 1 || root_taxon.children.empty?
   :ul, :class => 'taxons-list' do
    root_taxon.children.map do |taxon|
      css_class = (current_taxon && current_taxon.self_and_ancestors.include?(taxon)) ? 'current' : nil
       :li, :class => css_class do
       link_to(taxon.name, seo_url(taxon)) +
       taxons_tree(taxon, current_taxon, max_level - 1)
      end
    end.join("\n").html_safe
  end
end

#todays_short_dateObject



32
33
34
# File 'app/helpers/spree/base_helper.rb', line 32

def todays_short_date
  utc_to_local(Time.now.utc).to_ordinalized_s(:stub)
end

#variant_options(v, allow_back_orders = Spree::Config[:allow_backorders], include_style = true) ⇒ Object

human readable list of variant options



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/helpers/spree/base_helper.rb', line 41

def variant_options(v, allow_back_orders = Spree::Config[:allow_backorders], include_style = true)
  list = v.options_text

  # We shouldn't show out of stock if the product is infact in stock
  # or when we're not allowing backorders.
  unless (allow_back_orders || v.in_stock?)
    list = if include_style
      (:span, "(#{t(:out_of_stock)}) #{list}", :class => 'out-of-stock')
    else
      "#{t(:out_of_stock)} #{list}"
    end
  end

  list
end

#yesterdays_short_dateObject



36
37
38
# File 'app/helpers/spree/base_helper.rb', line 36

def yesterdays_short_date
  utc_to_local(Time.now.utc.yesterday).to_ordinalized_s(:stub)
end