Module: Spree::BaseHelper

Included in:
ProductsHelper, VariantPresenter
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



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

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



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

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

  countries = if checkout_zone && checkout_zone.kind == 'country'
                checkout_zone.country_list
              else
                Spree::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

#base_cache_keyObject



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

def base_cache_key
  [I18n.locale, current_currency]
end

#default_image_for_product(product) ⇒ Object

we should always try to render image of the default variant same as it’s done on PDP



113
114
115
116
117
118
119
# File 'app/helpers/spree/base_helper.rb', line 113

def default_image_for_product(product)
  if product.default_variant.images.any?
    product.default_variant.images.first
  elsif product.variant_images.any?
    product.variant_images.first
  end
end

#default_image_for_product_or_variant(product_or_variant) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/helpers/spree/base_helper.rb', line 121

def default_image_for_product_or_variant(product_or_variant)
  Rails.cache.fetch("spree/default-image/#{product_or_variant.cache_key_with_version}") do
    if product_or_variant.is_a?(Spree::Product)
      default_image_for_product(product_or_variant)
    elsif product_or_variant.is_a?(Spree::Variant)
      if product_or_variant.images.any?
        product_or_variant.images.first
      else
        default_image_for_product(product_or_variant.product)
      end
    end
  end
end

#display_price(product_or_variant) ⇒ Object



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

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

#frontend_available?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'app/helpers/spree/base_helper.rb', line 107

def frontend_available?
  Spree::Core::Engine.frontend_available?
end


25
26
27
28
29
30
31
32
33
34
35
# File 'app/helpers/spree/base_helper.rb', line 25

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

  options[:target] ||= :blank

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

#logo(image_path = , options = {}) ⇒ Object



37
38
39
40
41
42
43
# File 'app/helpers/spree/base_helper.rb', line 37

def (image_path = Spree::Config[:logo], options = {})
  path = spree.respond_to?(:root_path) ? spree.root_path : main_app.root_path

  link_to path, 'aria-label': current_store.name, method: options[:method] do
    image_tag image_path, alt: current_store.name, title: current_store.name
  end
end

#meta_dataObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/helpers/spree/base_helper.rb', line 45

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

  if object.is_a? ApplicationRecord
    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

  if meta[:keywords].blank? || meta[:description].blank?
    if object && object[:name].present?
      meta.reverse_merge!(keywords: [object.name, current_store.meta_keywords].reject(&:blank?).join(', '),
                          description: [object.name, current_store.meta_description].reject(&:blank?).join(', '))
    else
      meta.reverse_merge!(keywords: (current_store.meta_keywords || current_store.seo_title),
                          description: (current_store.meta_description || current_store.seo_title))
    end
  end
  meta
end

#meta_data_tagsObject



82
83
84
85
86
# File 'app/helpers/spree/base_helper.rb', line 82

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

#meta_image_data_tagObject



78
79
80
# File 'app/helpers/spree/base_helper.rb', line 78

def meta_image_data_tag
  tag('meta', property: 'og:image', content: meta_image_url_path) if meta_image_url_path
end

#meta_image_url_pathObject



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

def meta_image_url_path
  object = instance_variable_get('@' + controller_name.singularize)
  return unless object.is_a?(Spree::Product)

  image = default_image_for_product_or_variant(object)
  image&.attachment.present? ? main_app.url_for(image.attachment) : asset_path(Spree::Config[:logo])
end

#pretty_time(time) ⇒ Object



97
98
99
100
101
# File 'app/helpers/spree/base_helper.rb', line 97

def pretty_time(time)
  return '' if time.blank?

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

#seo_url(taxon, options = nil) ⇒ Object



103
104
105
# File 'app/helpers/spree/base_helper.rb', line 103

def seo_url(taxon, options = nil)
  spree.nested_taxons_path(taxon.permalink, options)
end