Class: Spree::VariantPresenter

Inherits:
Object
  • Object
show all
Includes:
BaseHelper
Defined in:
app/presenters/spree/variant_presenter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BaseHelper

#available_countries, #base_cache_key, #default_image_for_product, #default_image_for_product_or_variant, #display_price, #frontend_available?, #link_to_tracking, #logo, #maximum_quantity, #meta_data, #meta_data_tags, #meta_image_data_tag, #meta_image_url_path, #method_missing, #pretty_time, #seo_url

Constructor Details

#initialize(opts = {}) ⇒ VariantPresenter

Returns a new instance of VariantPresenter.



8
9
10
11
12
13
# File 'app/presenters/spree/variant_presenter.rb', line 8

def initialize(opts = {})
  @variants = opts[:variants]
  @is_product_available_in_currency = opts[:is_product_available_in_currency]
  @current_currency = opts[:current_currency]
  @current_price_options = opts[:current_price_options]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Spree::BaseHelper

Instance Attribute Details

#current_currencyObject (readonly)

Returns the value of attribute current_currency.



6
7
8
# File 'app/presenters/spree/variant_presenter.rb', line 6

def current_currency
  @current_currency
end

#current_price_optionsObject (readonly)

Returns the value of attribute current_price_options.



6
7
8
# File 'app/presenters/spree/variant_presenter.rb', line 6

def current_price_options
  @current_price_options
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/presenters/spree/variant_presenter.rb', line 15

def call
  @variants.map do |variant|
    {
      display_price: display_price(variant),
      is_product_available_in_currency: @is_product_available_in_currency,
      backorderable: backorderable?(variant),
      in_stock: in_stock?(variant),
      images: images(variant),
      option_values: option_values(variant),
    }.merge(
      variant_attributes(variant)
    )
  end
end

#images(variant) ⇒ Object



30
31
32
33
34
35
36
37
# File 'app/presenters/spree/variant_presenter.rb', line 30

def images(variant)
  variant.images.map do |image|
    {
      alt: image.alt,
      url_product: rails_representation_url(image.url(:product), only_path: true)
    }
  end
end

#option_values(variant) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'app/presenters/spree/variant_presenter.rb', line 39

def option_values(variant)
  variant.option_values.map do |option_value|
    {
      id: option_value.id,
      name: option_value.name,
      presentation: option_value.presentation,
    }
  end
end