Module: Spree::ProductsHelper

Defined in:
app/helpers/spree/products_helper.rb

Instance Method Summary collapse

Instance Method Details

#cache_key_for_productsString

Returns a cache invalidation key for products.

Returns:

  • (String)

    a cache invalidation key for products



66
67
68
69
70
# File 'app/helpers/spree/products_helper.rb', line 66

def cache_key_for_products
  count = @products.count
  max_updated_at = (@products.maximum(:updated_at) || Date.today).to_s(:number)
  "#{I18n.locale}/#{current_pricing_options.cache_key}/spree/products/all-#{params[:page]}-#{max_updated_at}-#{count}"
end

#line_item_description_text(description_text) ⇒ String

Filters and truncates the given description.

Parameters:

  • description_text (String)

    the text to filter

Returns:

  • (String)

    the filtered text



57
58
59
60
61
62
63
# File 'app/helpers/spree/products_helper.rb', line 57

def line_item_description_text(description_text)
  if description_text.present?
    truncate(strip_tags(description_text.gsub(' ', ' ')), length: 100)
  else
    Spree.t(:product_has_no_description)
  end
end

#product_description(product) ⇒ String

Converts line breaks in product description into <p> tags.

Parameters:

  • product (Spree::Product)

    the product whose description you want to filter

Returns:

  • (String)

    the generated HTML



45
46
47
48
49
50
51
# File 'app/helpers/spree/products_helper.rb', line 45

def product_description(product)
  if Spree::Config[:show_raw_product_description]
    raw(product.description)
  else
    raw(product.description.gsub(/(.*?)\r?\n\r?\n/m, '<p>\1</p>'))
  end
end

#variant_full_price(variant) ⇒ Spree::Money

Returns the formatted full price for the variant, if at least one variant price differs from product price.

Parameters:

Returns:



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

def variant_full_price(variant)
  return if variant.product.variants
              .with_prices(current_pricing_options)
              .all? { |v| v.price_same_as_master?(current_pricing_options) }
  variant.price_for(current_pricing_options).to_html
end

#variant_price(variant) ⇒ Spree::Money

Returns the formatted price for the specified variant as a full price or a difference depending on configuration

Parameters:

Returns:



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

def variant_price(variant)
  if Spree::Config[:show_variant_full_price]
    variant_full_price(variant)
  else
    variant_price_diff(variant)
  end
end

#variant_price_diff(variant) ⇒ String

Returns the formatted price for the specified variant as a difference from product price

Parameters:

Returns:

  • (String)

    formatted string with label and amount



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

def variant_price_diff(variant)
  return if variant.price_same_as_master?(current_pricing_options)
  difference = variant.price_difference_from_master(current_pricing_options)
  absolute_amount = Spree::Money.new(difference.to_d.abs, currency: difference.currency.iso_code)
  i18n_key = difference.to_d > 0 ? :price_diff_add_html : :price_diff_subtract_html
  Spree.t(i18n_key, scope: [:helpers, :products], amount_html: absolute_amount.to_html)
end