Module: Spree::ProductsHelper
- Defined in:
- app/helpers/spree/products_helper.rb
Instance Method Summary collapse
-
#available_status(product) ⇒ Object
will return a human readable string.
- #cache_key_for_product(product = @product) ⇒ Object
- #cache_key_for_products ⇒ Object
- #line_item_description_text(description_text) ⇒ Object
-
#product_description(product) ⇒ Object
converts line breaks in product description into <p> tags (for html display purposes).
-
#variant_full_price(variant) ⇒ Object
returns the formatted full price for the variant, if at least one variant price differs from product price.
-
#variant_price(variant) ⇒ Object
returns the formatted price for the specified variant as a full price or a difference depending on configuration.
-
#variant_price_diff(variant) ⇒ Object
returns the formatted price for the specified variant as a difference from product price.
Instance Method Details
#available_status(product) ⇒ Object
will return a human readable string
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/helpers/spree/products_helper.rb', line 61 def available_status(product) # will return a human readable string return Spree.t(:discontinued) if product.discontinued? return Spree.t(:deleted) if product.deleted? if product.available? Spree.t(:available) elsif product.available_on&.future? Spree.t(:pending_sale) else Spree.t(:no_available_date_set) end end |
#cache_key_for_product(product = @product) ⇒ Object
57 58 59 |
# File 'app/helpers/spree/products_helper.rb', line 57 def cache_key_for_product(product = @product) (common_product_cache_keys + [product.cache_key_with_version, product.possible_promotions]).compact.join('/') end |
#cache_key_for_products ⇒ Object
50 51 52 53 54 55 |
# File 'app/helpers/spree/products_helper.rb', line 50 def cache_key_for_products count = @products.count max_updated_at = (@products.maximum(:updated_at) || Date.today).to_s(:number) products_cache_keys = "spree/products/all-#{params[:page]}-#{max_updated_at}-#{count}" (common_product_cache_keys + [products_cache_keys]).compact.join('/') end |
#line_item_description_text(description_text) ⇒ Object
42 43 44 45 46 47 48 |
# File 'app/helpers/spree/products_helper.rb', line 42 def line_item_description_text(description_text) if description_text.present? truncate((description_text.gsub(' ', ' ').squish), length: 100) else Spree.t(:product_has_no_description) end end |
#product_description(product) ⇒ Object
converts line breaks in product description into <p> tags (for html display purposes)
33 34 35 36 37 38 39 40 |
# File 'app/helpers/spree/products_helper.rb', line 33 def product_description(product) description = if Spree::Config[:show_raw_product_description] product.description else product.description.to_s.gsub(/(.*?)\r?\n\r?\n/m, '<p>\1</p>') end description.blank? ? Spree.t(:product_has_no_description) : description end |
#variant_full_price(variant) ⇒ Object
returns the formatted full price for the variant, if at least one variant price differs from product price
25 26 27 28 29 30 |
# File 'app/helpers/spree/products_helper.rb', line 25 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) ⇒ Object
returns the formatted price for the specified variant as a full price or a difference depending on configuration
4 5 6 7 8 9 10 |
# File 'app/helpers/spree/products_helper.rb', line 4 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) ⇒ Object
returns the formatted price for the specified variant as a difference from product price
13 14 15 16 17 18 19 20 21 22 |
# File 'app/helpers/spree/products_helper.rb', line 13 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 |