Module: Spree::BaseHelper

Included in:
ProductsHelper, VariantPresenter
Defined in:
app/helpers/spree/base_helper.rb

Instance Method Summary collapse

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



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

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



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

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.primary_media instead')

  product.primary_media
end

#default_image_for_product_or_variant(product_or_variant) ⇒ Object



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

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.primary_media instead')

  product_or_variant.primary_media
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)


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

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



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

def maximum_quantity
  Spree::Deprecation.warn('BaseHelper#maximum_quantity is deprecated and will be removed in Spree 5.5')
  Spree::DatabaseTypeUtilities::INTEGER_MAX
end

#objectObject



71
72
73
# File 'app/helpers/spree/base_helper.rb', line 71

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

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



186
187
188
189
190
191
192
193
194
195
# File 'app/helpers/spree/base_helper.rb', line 186

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



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

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



75
76
77
78
79
80
81
# File 'app/helpers/spree/base_helper.rb', line 75

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



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

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



168
169
170
171
172
173
174
175
# File 'app/helpers/spree/base_helper.rb', line 168

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



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

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_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:

  • resource (Spree::Product, Spree::Taxon, Spree::Page)

    the resource to get the URL for

  • options (Hash) (defaults to: {})

    the options for the URL

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/helpers/spree/base_helper.rb', line 110

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]
               ''
             else
               store.storefront_url
             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?(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