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



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

def method_missing(method_name, *args, &block)
  if image_style = image_style_from_method_name(method_name)
    define_image_method(image_style)
    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



49
50
51
52
# File 'app/helpers/spree/base_helper.rb', line 49

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

#display_price(product_or_variant) ⇒ Object



128
129
130
# File 'app/helpers/spree/base_helper.rb', line 128

def display_price(product_or_variant)
  product_or_variant.price_for(current_pricing_options).to_html
end

#flash_messages(opts = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'app/helpers/spree/base_helper.rb', line 58

def flash_messages(opts = {})
  ignore_types = ["order_completed"].concat(Array(opts[:ignore_types]).map(&:to_s) || [])

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


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

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

  if simple_current_order.nil? || 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


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

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

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

#logo(image_path = ) ⇒ Object



54
55
56
# File 'app/helpers/spree/base_helper.rb', line 54

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

#meta_dataObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/spree/base_helper.rb', line 23

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

  if object.is_a? 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.is_a?(Spree::Product)
    meta[:description] = truncate(strip_tags(object.description), length: 160, separator: ' ')
  end

  meta.reverse_merge!({
    keywords: current_store.meta_keywords,
    description: current_store.meta_description
  }) if meta[:keywords].blank? || meta[:description].blank?
  meta
end

#meta_data_tagsObject



43
44
45
46
47
# File 'app/helpers/spree/base_helper.rb', line 43

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

#plural_resource_name(resource_class) ⇒ Object



156
157
158
# File 'app/helpers/spree/base_helper.rb', line 156

def plural_resource_name(resource_class)
  resource_class.model_name.human(count: Spree::I18N_GENERIC_PLURAL)
end

#pretty_time(time) ⇒ Object



132
133
134
135
# File 'app/helpers/spree/base_helper.rb', line 132

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)
  spree.nested_taxons_path(taxon.permalink)
end

#taxon_breadcrumbs(taxon, separator = "&nbsp;&raquo;&nbsp;", breadcrumb_class = "inline") ⇒ Object



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

def taxon_breadcrumbs(taxon, separator = "&nbsp;&raquo;&nbsp;", breadcrumb_class = "inline")
  return "" if current_page?("/") || taxon.nil?

  crumbs = [[Spree.t(:home), spree.root_path]]

  if taxon
    crumbs << [Spree.t(:products), products_path]
    crumbs += taxon.ancestors.collect { |a| [a.name, spree.nested_taxons_path(a.permalink)] } unless taxon.ancestors.empty?
    crumbs << [taxon.name, spree.nested_taxons_path(taxon.permalink)]
  else
    crumbs << [Spree.t(:products), products_path]
  end

  separator = raw(separator)

  crumbs.map! do |crumb|
    (:li, itemscope: "itemscope", itemtype: "http://data-vocabulary.org/Breadcrumb") do
      link_to(crumb.last, itemprop: "url") do
        (:span, crumb.first, itemprop: "title")
      end + (crumb == crumbs.last ? '' : separator)
    end
  end

  (:nav, (:ul, raw(crumbs.map(&:mb_chars).join), class: breadcrumb_class), id: 'breadcrumbs', class: 'sixteen columns')
end

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



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

def taxons_tree(root_taxon, current_taxon, max_level = 1)
  return '' if max_level < 1 || root_taxon.children.empty?
   :ul, class: 'taxons-list' do
    taxons = 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
    safe_join(taxons, "\n")
  end
end

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

human readable list of variant options



19
20
21
# File 'app/helpers/spree/base_helper.rb', line 19

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