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



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

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/helpers/spree/base_helper.rb', line 109

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 = Spree.t(country.iso, scope: 'country_names', default: country.name)
    country
  end.sort_by { |c| c.name.parameterize }
end

#body_classObject



61
62
63
64
# File 'app/helpers/spree/base_helper.rb', line 61

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


81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/helpers/spree/base_helper.rb', line 81

def breadcrumbs(taxon, separator=" » ")
  return "" if current_page?("/") || taxon.nil?
  separator = raw(separator)
  crumbs = [(:li, link_to(Spree.t(:home), spree.root_path) + separator)]
  if taxon
    crumbs << (:li, link_to(Spree.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, Spree.t(:products)))
  end
  crumb_list = (:ul, raw(crumbs.flatten.map{|li| li.mb_chars}.join), class: 'inline')
  (:nav, crumb_list, id: 'breadcrumbs', class: 'sixteen columns')
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

#display_price(product_or_variant) ⇒ Object



136
137
138
# File 'app/helpers/spree/base_helper.rb', line 136

def display_price(product_or_variant)
  product_or_variant.price_in(current_currency).display_price.to_html
end

#flash_messages(opts = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'app/helpers/spree/base_helper.rb', line 70

def flash_messages(opts = {})
  opts[:ignore_types] = [:commerce_tracking].concat(Array(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)


128
129
130
131
132
133
134
# File 'app/helpers/spree/base_helper.rb', line 128

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
# File 'app/helpers/spree/base_helper.rb', line 15

def link_to_cart(text = nil)
  text = text ? h(text) : Spree.t('cart')
  css_class = nil

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

  link_to text.html_safe, spree.cart_path, :class => "cart-info #{css_class}"
end


154
155
156
157
158
159
160
161
162
# File 'app/helpers/spree/base_helper.rb', line 154

def link_to_tracking(shipment, options = {})
  return unless shipment.tracking

  if shipment.tracking_url
    link_to(shipment.tracking, shipment.tracking_url, options)
  else
    (:span, shipment.tracking)
  end
end

#logo(image_path = ) ⇒ Object



66
67
68
# File 'app/helpers/spree/base_helper.rb', line 66

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

#meta_dataObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/helpers/spree/base_helper.rb', line 35

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(truncate(object.description, length: 160, separator: ' '))
  end

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

#meta_data_tagsObject



55
56
57
58
59
# File 'app/helpers/spree/base_helper.rb', line 55

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

#pretty_time(time) ⇒ Object



140
141
142
143
# File 'app/helpers/spree/base_helper.rb', line 140

def pretty_time(time)
  [I18n.l(time.to_date, format: :long),
    time.strftime("%l:%M %p")].join(" ")
end

#seo_url(taxon) ⇒ Object



124
125
126
# File 'app/helpers/spree/base_helper.rb', line 124

def seo_url(taxon)
  return spree.nested_taxons_path(taxon.permalink)
end

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



96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/helpers/spree/base_helper.rb', line 96

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

#variant_options(v, options = {}) ⇒ Object

human readable list of variant options



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

def variant_options(v, options={})
  v.options_text
end