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



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

def method_missing(method_name, *args, &block)
  if image_style = image_style_from_method_name(method_name)
    Spree::Deprecation.warn("#{BaseHelper.name}##{method_name} is deprecated and will be removed in Spree 5.5. Please use spree_image_tag instead - https://spreecommerce.org/docs/developer/core-concepts/images-assets#preprocessed-named-variants")
    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



188
189
190
191
# File 'app/helpers/spree/base_helper.rb', line 188

def base_cache_key
  Spree::Deprecation.warn('`base_cache_key` is deprecated and will be removed in Spree 5.5. 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



176
177
178
179
180
# File 'app/helpers/spree/base_helper.rb', line 176

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

  product.default_image
end

#default_image_for_product_or_variant(product_or_variant) ⇒ Object



182
183
184
185
186
# File 'app/helpers/spree/base_helper.rb', line 182

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 5.5. Please use product_or_variant.default_image instead')

  product_or_variant.default_image
end

#display_compare_at_price(product_or_variant) ⇒ Object



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

def display_compare_at_price(product_or_variant)
  Spree::Deprecation.warn('display_compare_at_price is deprecated and will be removed in Spree 5.5. Use variant.price_for(context).display_compare_at_amount instead.')

  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



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

def display_price(product_or_variant)
  Spree::Deprecation.warn('display_price is deprecated and will be removed in Spree 5.5. Use variant.price_for(context).display_amount instead.')

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

#frontend_available?Boolean

Returns:

  • (Boolean)


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

def frontend_available?
  Spree::Deprecation.warn('BaseHelper#frontend_available? is deprecated and will be removed in Spree 5.5')
  Spree::Core::Engine.frontend_available?
end


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

def link_to_tracking(shipment, options = {})
  Spree::Deprecation.warn('BaseHelper#link_to_tracking is deprecated and will be removed in Spree 5.5. Please use shipment.tracking_url instead.')

  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



206
207
208
# File 'app/helpers/spree/base_helper.rb', line 206

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

#objectObject



81
82
83
# File 'app/helpers/spree/base_helper.rb', line 81

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

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



210
211
212
213
214
215
216
217
218
219
# File 'app/helpers/spree/base_helper.rb', line 210

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]

  opts[:fallback] = 'payment_icons/storecredit.svg'

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

#pretty_date(date) ⇒ Object



103
104
105
106
107
108
109
# File 'app/helpers/spree/base_helper.rb', line 103

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

  Spree::Deprecation.warn('BaseHelper#pretty_date is deprecated and will be removed in Spree 5.5. 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



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

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

  Spree::Deprecation.warn('BaseHelper#pretty_time is deprecated and will be removed in Spree 5.5. 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



111
112
113
114
# File 'app/helpers/spree/base_helper.rb', line 111

def seo_url(taxon, options = {})
  Spree::Deprecation.warn('BaseHelper#seo_url is deprecated and will be removed in Spree 5.5. Please use spree_storefront_resource_url')
  spree.nested_taxons_path(taxon.permalink, options.merge(locale: locale_param))
end

#spree_base_cache_keyObject



193
194
195
196
197
198
199
200
# File 'app/helpers/spree/base_helper.rb', line 193

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



202
203
204
# File 'app/helpers/spree/base_helper.rb', line 202

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

#spree_class_name_as_path(class_name) ⇒ Object



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

def spree_class_name_as_path(class_name)
  Spree::Deprecation.warn('BaseHelper#spree_class_name_as_path is deprecated and will be removed in Spree 5.5')

  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



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

def spree_favicon_path
  Spree::Deprecation.warn('BaseHelper#spree_favicon_path is deprecated and will be removed in Spree 5.5. Please use Active Storage URL helpers instead.')

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

def spree_resource_path(resource)
  Spree::Deprecation.warn('BaseHelper#spree_resource_path is deprecated and will be removed in Spree 5.5')

  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

Parameters:

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)

Returns:

  • (String)

    the URL of the resource



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/helpers/spree/base_helper.rb', line 130

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 defined?(Spree::Post) && resource.is_a?(Spree::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 defined?(Spree::Page) && (resource.is_a?(Spree::Page) || resource.is_a?(Spree::Policy))
    "#{base_url + localize}#{resource.page_builder_url}"
  elsif defined?(Spree::PageLink) && resource.is_a?(Spree::PageLink)
    resource.linkable_url
  elsif localize.blank?
    base_url
  else
    base_url + localize
  end
end