Module: Spree::BaseHelper

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



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

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



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

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



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

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



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

def base_cache_key
  Spree::Deprecation.warn('`base_cache_key` is deprecated and will be removed in Spree 6. Please use `spree_base_cache_key` instead')
  spree_base_cache_key
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



219
220
221
222
223
# File 'app/helpers/spree/base_helper.rb', line 219

def default_image_for_product(product)
  Spree::Deprecation.warn('BaseHelper#default_image_for_product is deprecated and will be removed in Spree 6.0. Please use product.default_image instead')

  product.default_image
end

#default_image_for_product_or_variant(product_or_variant) ⇒ Object



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

def default_image_for_product_or_variant(product_or_variant)
  Spree::Deprecation.warn('BaseHelper#default_image_for_product_or_variant is deprecated and will be removed in Spree 6.0. Please use product_or_variant.default_image instead')

  product_or_variant.default_image
end

#display_compare_at_price(product_or_variant) ⇒ Object



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

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



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

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



160
161
162
# File 'app/helpers/spree/base_helper.rb', line 160

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


49
50
51
52
53
54
55
56
57
58
59
# File 'app/helpers/spree/base_helper.rb', line 49

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

#maximum_quantityObject



249
250
251
# File 'app/helpers/spree/base_helper.rb', line 249

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

#meta_dataObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/helpers/spree/base_helper.rb', line 95

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.seo_meta_description)
    end
  end
  meta
end

#meta_data_tagsObject



125
126
127
128
129
# File 'app/helpers/spree/base_helper.rb', line 125

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

#objectObject



69
70
71
# File 'app/helpers/spree/base_helper.rb', line 69

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

#og_meta_dataObject



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 73

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



119
120
121
122
123
# File 'app/helpers/spree/base_helper.rb', line 119

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

#payment_method_icon_tag(payment_method, opts = {}) ⇒ Object



253
254
255
256
257
258
259
260
# File 'app/helpers/spree/base_helper.rb', line 253

def payment_method_icon_tag(payment_method, opts = {})
  return '' unless defined?(inline_svg)

  opts[:width] ||= opts[:height] * 1.5 if opts[:height]
  opts[:size] = "#{opts[:width]}x#{opts[:height]}" if opts[:width] && opts[:height]

  inline_svg "payment_icons/#{payment_method}.svg", opts
end

#pretty_date(date) ⇒ Object



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

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

  Spree::Deprecation.warn('BaseHelper#pretty_date is deprecated and will be removed in Spree 6.0. Please add `local_time` gem to your Gemfile and use `local_date(date)` instead')

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

#pretty_time(time) ⇒ Object



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

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

  Spree::Deprecation.warn('BaseHelper#pretty_time is deprecated and will be removed in Spree 6.0. Please add `local_time` gem to your Gemfile and use `local_time(time)` instead')

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

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



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

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

#spree_base_cache_keyObject



236
237
238
239
240
241
242
243
# File 'app/helpers/spree/base_helper.rb', line 236

def spree_base_cache_key
  @spree_base_cache_key ||= [
    I18n.locale,
    (current_currency if defined?(current_currency)),
    defined?(try_spree_current_user) && try_spree_current_user.present?,
    defined?(try_spree_current_user) && try_spree_current_user.respond_to?(:role_users) && try_spree_current_user.role_users.cache_key_with_version
  ].compact
end

#spree_base_cache_scopeObject



245
246
247
# File 'app/helpers/spree/base_helper.rb', line 245

def spree_base_cache_scope
  ->(record = nil) { [*spree_base_cache_key, record].compact_blank }
end

#spree_class_name_as_path(class_name) ⇒ Object



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

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

#spree_dom_id(record) ⇒ Object



3
4
5
# File 'app/helpers/spree/base_helper.rb', line 3

def spree_dom_id(record)
  dom_id(record, 'spree')
end

#spree_favicon_pathObject



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

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



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

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 = {}) ⇒ String

returns the URL of an object on the storefront

Options Hash (options):

  • :locale (String)

    the locale of the resource, defaults to I18n.locale

  • :store (String)

    the store of the resource, defaults to current_store

  • :relative (String)

    whether to use the relative URL, defaults to false

  • :preview_id (String)

    the preview ID of the resource, usually the ID of the resource

  • :variant_id (String)

    the variant ID of the resource, usually the ID of the variant (only used for products)



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/helpers/spree/base_helper.rb', line 173

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

  store = options[:store] || current_store

  base_url = if options[:relative]
               ''
             elsif store.formatted_custom_domain.blank?
               store.formatted_url
             else
               store.formatted_custom_domain
             end

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

  if resource.instance_of?(Spree::Product)
    preview_id = ("preview_id=#{options[:preview_id]}" if options[:preview_id].present?)

    variant_id = ("variant_id=#{options[:variant_id]}" if options[:variant_id].present?)

    params = [preview_id, variant_id].compact_blank.join('&')
    params = "?#{params}" if params.present?

    "#{base_url + localize}/products/#{resource.slug}#{params}"
  elsif resource.is_a?(Post)
    preview_id = options[:preview_id].present? ? "?preview_id=#{options[:preview_id]}" : ''
    "#{base_url + localize}/posts/#{resource.slug}#{preview_id}"
  elsif resource.is_a?(Spree::Taxon)
    "#{base_url + localize}/t/#{resource.permalink}"
  elsif resource.is_a?(Spree::Page) || resource.is_a?(Spree::Policy)
    "#{base_url + localize}#{resource.page_builder_url}"
  elsif resource.is_a?(Spree::PageLink)
    resource.linkable_url
  elsif localize.blank?
    base_url
  else
    base_url + localize
  end
end