Module: Spree::ProductsHelper
- Defined in:
- app/helpers/spree/products_helper.rb
Instance Method Summary collapse
-
#cache_key_for_products ⇒ String
A cache invalidation key for products.
-
#line_item_description_text(description_text) ⇒ String
Filters and truncates the given description.
-
#product_description(product) ⇒ String
Converts line breaks in product description into <p> tags.
-
#variant_full_price(variant) ⇒ Spree::Money
Returns the formatted full price for the variant, if at least one variant price differs from product price.
-
#variant_price(variant) ⇒ Spree::Money
Returns the formatted price for the specified variant as a full price or a difference depending on configuration.
-
#variant_price_diff(variant) ⇒ String
Returns the formatted price for the specified variant as a difference from product price.
Instance Method Details
#cache_key_for_products ⇒ String
Returns a cache invalidation key for products.
68 69 70 71 72 |
# File 'app/helpers/spree/products_helper.rb', line 68 def cache_key_for_products count = @products.count max_updated_at = (@products.maximum(:updated_at) || Date.today).to_s(:number) "#{I18n.locale}/#{current_currency}/spree/products/all-#{params[:page]}-#{max_updated_at}-#{count}" end |
#line_item_description_text(description_text) ⇒ String
Filters and truncates the given description.
59 60 61 62 63 64 65 |
# File 'app/helpers/spree/products_helper.rb', line 59 def line_item_description_text(description_text) if description_text.present? truncate((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.
47 48 49 50 51 52 53 |
# File 'app/helpers/spree/products_helper.rb', line 47 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.
36 37 38 39 40 41 |
# File 'app/helpers/spree/products_helper.rb', line 36 def variant_full_price(variant) product = variant.product unless product.variants.active(current_currency).all? { |v| v.price == product.price } Spree::Money.new(variant.price, { currency: current_currency }).to_html end end |
#variant_price(variant) ⇒ Spree::Money
Returns the formatted price for the specified variant as a full price or a difference depending on configuration
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
21 22 23 24 25 26 27 28 29 |
# File 'app/helpers/spree/products_helper.rb', line 21 def variant_price_diff(variant) variant_amount = variant.amount_in(current_currency) product_amount = variant.product.amount_in(current_currency) return if variant_amount == product_amount || product_amount.nil? diff = variant.amount_in(current_currency) - product_amount amount = Spree::Money.new(diff.abs, currency: current_currency).to_html label = diff > 0 ? :add : :subtract "(#{Spree.t(label)}: #{amount})".html_safe end |