Class: Workarea::Storefront::OrderItemViewModel

Inherits:
ApplicationViewModel
  • Object
show all
Defined in:
app/view_models/workarea/storefront/order_item_view_model.rb

Direct Known Subclasses

CartItemViewModel

Instance Method Summary collapse

Instance Method Details

#catalog_productObject



40
41
42
43
44
45
46
# File 'app/view_models/workarea/storefront/order_item_view_model.rb', line 40

def catalog_product
  @catalog_product ||= if product_attributes.present?
                         Mongoid::Factory.from_db(Catalog::Product, product_attributes)
                       else
                         Catalog::Product.find_by_sku(sku)
                       end
end

#customizations_unit_priceObject



61
62
63
64
65
# File 'app/view_models/workarea/storefront/order_item_view_model.rb', line 61

def customizations_unit_price
  (price_adjustments.detect do |adjustment|
    adjustment.description =~ /customizations/i
  end.try(:unit) || 0).to_m
end

#default_category_nameObject



85
86
87
88
# File 'app/view_models/workarea/storefront/order_item_view_model.rb', line 85

def default_category_name
  @default_category_name =
    Categorization.new(catalog_product).default_model.try(:name)
end

#detailsObject



17
18
19
20
21
22
23
24
25
26
# File 'app/view_models/workarea/storefront/order_item_view_model.rb', line 17

def details
  @details ||=
    begin
      tmp = variant.details.map do |k, v|
        [k.to_s.titleize, [v].flatten.join(', ')]
      end

      Hash[tmp]
    end
end

#fulfillment_skuObject

Fulfillment



75
76
77
78
# File 'app/view_models/workarea/storefront/order_item_view_model.rb', line 75

def fulfillment_sku
  @fulfillment_sku ||=
    Fulfillment::Sku.find_or_initialize_by(id: model.sku)
end

#has_options?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/view_models/workarea/storefront/order_item_view_model.rb', line 36

def has_options?
  product.sku_options.length > 1
end

#imageObject



13
14
15
# File 'app/view_models/workarea/storefront/order_item_view_model.rb', line 13

def image
  product.primary_image
end

#multiple?Boolean

Pricing

Returns:

  • (Boolean)


53
54
55
# File 'app/view_models/workarea/storefront/order_item_view_model.rb', line 53

def multiple?
  quantity > 1
end

#original_priceObject



57
58
59
# File 'app/view_models/workarea/storefront/order_item_view_model.rb', line 57

def original_price
  price_adjustments.first.data['original_price'].to_m
end

#productObject

Catalog details



9
10
11
# File 'app/view_models/workarea/storefront/order_item_view_model.rb', line 9

def product
  @product ||= ProductViewModel.wrap(catalog_product, sku: sku)
end

#product_nameObject



28
29
30
# File 'app/view_models/workarea/storefront/order_item_view_model.rb', line 28

def product_name
  product.name
end

#sku_nameObject



32
33
34
# File 'app/view_models/workarea/storefront/order_item_view_model.rb', line 32

def sku_name
  variant.name
end

#tokenObject



80
81
82
83
# File 'app/view_models/workarea/storefront/order_item_view_model.rb', line 80

def token
  return unless fulfillment_sku.download?
  @token ||= Fulfillment::Token.for_order_item(model.order.id, model.id)
end

#total_adjustmentsObject



67
68
69
# File 'app/view_models/workarea/storefront/order_item_view_model.rb', line 67

def total_adjustments
  @total_adjustments ||= price_adjustments.reduce_by_description('item')
end

#variantObject



90
91
92
93
94
# File 'app/view_models/workarea/storefront/order_item_view_model.rb', line 90

def variant
  catalog_product.variants.detect do |variant|
    variant.sku.downcase == model.sku.downcase
  end
end