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

#default_image_for_product(product) ⇒ Object

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



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

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



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

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

#objectObject



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

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

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



104
105
106
107
108
109
110
111
112
113
# File 'app/helpers/spree/base_helper.rb', line 104

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

#spree_base_cache_keyObject



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

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



100
101
102
# File 'app/helpers/spree/base_helper.rb', line 100

def spree_base_cache_scope
  ->(record = nil) { [*spree_base_cache_key, record].compact_blank }
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_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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/spree/base_helper.rb', line 38

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