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



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

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

#all_countriesObject



12
13
14
15
16
17
18
19
# File 'app/helpers/spree/base_helper.rb', line 12

def all_countries
  countries = Spree::Country.all

  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

#available_countriesObject



3
4
5
6
7
8
9
10
# File 'app/helpers/spree/base_helper.rb', line 3

def available_countries
  countries = current_store.countries_available_for_checkout

  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



227
228
229
230
# File 'app/helpers/spree/base_helper.rb', line 227

def base_cache_key
  [I18n.locale, current_currency, defined?(try_spree_current_user) && try_spree_current_user.present?,
   defined?(try_spree_current_user) && try_spree_current_user.try(:has_spree_role?, 'admin')]
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



203
204
205
206
207
208
209
210
211
# File 'app/helpers/spree/base_helper.rb', line 203

def default_image_for_product(product)
  if product.images.any?
    product.images.first
  elsif 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



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'app/helpers/spree/base_helper.rb', line 213

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_compare_at_price(product_or_variant) ⇒ Object



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

def display_compare_at_price(product_or_variant)
  product_or_variant.
    price_in(current_currency).
    display_compare_at_price_including_vat_for(current_price_options).
    to_html
end

#display_price(product_or_variant) ⇒ Object



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

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)


173
174
175
# File 'app/helpers/spree/base_helper.rb', line 173

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


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

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 = nil, options = {}) ⇒ Object



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

def (image_path = nil, options = {})
  Spree::Deprecation.warn(<<-DEPRECATION, caller)
    `BaseHelper#logo` is deprecated and will be removed in Spree 5.0.
    Please use `FrontendHelper#logo` instead
  DEPRECATION

  image_path ||= if current_store..attached? && current_store..variable?
                   main_app.cdn_image_url(current_store..variant(resize_to_limit: [244, 104]))
                 elsif current_store..attached? && current_store..image?
                   main_app.cdn_image_url(current_store.)
                 else
                   'logo/spree_50.png'
                 end

  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

#maximum_quantityObject



232
233
234
# File 'app/helpers/spree/base_helper.rb', line 232

def maximum_quantity
  Spree::DatabaseTypeUtilities.maximum_value_for(:integer)
end

#meta_dataObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/helpers/spree/base_helper.rb', line 112

def 
  meta = {}

  if object.is_a? ApplicationRecord
    meta[:keywords] = object.meta_keywords if object.try(:meta_keywords).present?
    meta[:description] = object.meta_description if object.try(: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.homepage(I18n.locale)&.seo_meta_description || current_store.seo_meta_description))
    end
  end
  meta
end

#meta_data_tagsObject



142
143
144
145
146
# File 'app/helpers/spree/base_helper.rb', line 142

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

#objectObject



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

def object
  instance_variable_get('@' + controller_name.singularize)
end

#og_meta_dataObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/helpers/spree/base_helper.rb', line 90

def 
  og_meta = {}

  if object.is_a? Spree::Product
    image                             = default_image_for_product_or_variant(object)
    og_meta['og:image']               = main_app.cdn_image_url(image.attachment) if image&.attachment

    og_meta['og:url']                 = spree.url_for(object) if frontend_available? # url_for product needed
    og_meta['og:type']                = object.class.name.demodulize.downcase
    og_meta['og:title']               = object.name
    og_meta['og:description']         = object.description

    price = object.price_in(current_currency)
    if price
      og_meta['product:price:amount']   = price.amount
      og_meta['product:price:currency'] = current_currency
    end
  end

  og_meta
end

#og_meta_data_tagsObject



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

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

#pretty_date(date) ⇒ Object



163
164
165
166
167
# File 'app/helpers/spree/base_helper.rb', line 163

def pretty_date(date)
  return '' if date.blank?

  [I18n.l(date.to_date, format: :long)].join(' ')
end

#pretty_time(time) ⇒ Object



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

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

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

#seo_url(taxon, options = {}) ⇒ Object



169
170
171
# File 'app/helpers/spree/base_helper.rb', line 169

def seo_url(taxon, options = {})
  spree.nested_taxons_path(taxon.permalink, options.merge(locale: locale_param))
end

#spree_class_name_as_path(class_name) ⇒ Object



27
28
29
# File 'app/helpers/spree/base_helper.rb', line 27

def spree_class_name_as_path(class_name)
  class_name.underscore.humanize.parameterize(separator: '_')
end

#spree_favicon_pathObject



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

def spree_favicon_path
  if current_store.favicon.present?
    main_app.cdn_image_url(current_store.favicon)
  else
    url_for('favicon.ico')
  end
end

#spree_resource_path(resource) ⇒ Object



21
22
23
24
25
# File 'app/helpers/spree/base_helper.rb', line 21

def spree_resource_path(resource)
  last_word = resource.class.name.split('::', 10).last

  spree_class_name_as_path(last_word)
end

#spree_storefront_resource_url(resource, options = {}) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'app/helpers/spree/base_helper.rb', line 177

def spree_storefront_resource_url(resource, options = {})
  if defined?(locale_param) && locale_param.present?
    options.merge!(locale: locale_param)
  end

  localize = if options[:locale].present?
               "/#{options[:locale]}"
             else
               ''
             end

  if resource.instance_of?(Spree::Product)
    "#{current_store.formatted_url + localize}/#{Spree::Config[:storefront_products_path]}/#{resource.slug}"
  elsif resource.instance_of?(Spree::Taxon)
    "#{current_store.formatted_url + localize}/#{Spree::Config[:storefront_taxons_path]}/#{resource.permalink}"
  elsif resource.instance_of?(Spree::Cms::Pages::FeaturePage) || resource.instance_of?(Spree::Cms::Pages::StandardPage)
    "#{current_store.formatted_url + localize}/#{Spree::Config[:storefront_pages_path]}/#{resource.slug}"
  elsif localize.blank?
    current_store.formatted_url
  else
    current_store.formatted_url + localize
  end
end