Module: SpreeCmCommissioner::VariantDecorator

Defined in:
app/models/spree_cm_commissioner/variant_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object

rubocop:disable Metrics/AbcSize



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/spree_cm_commissioner/variant_decorator.rb', line 3

def self.prepended(base) # rubocop:disable Metrics/AbcSize
  base.include SpreeCmCommissioner::ProductType
  base.include SpreeCmCommissioner::ProductDelegation
  base.include SpreeCmCommissioner::VariantOptionsConcern
  base.include SpreeCmCommissioner::KycBitwise

  base.after_commit :update_vendor_price
  base.validate     :validate_option_types
  base.before_save -> { self.track_inventory = false }, if: :subscribable?

  base.belongs_to :vendor, class_name: 'Spree::Vendor'
  base.has_one :event, class_name: 'Spree::Taxon', through: :product

  base.has_many :taxons, class_name: 'Spree::Taxon', through: :product
  base.has_many :visible_option_values, lambda {
                                          joins(:option_type).where(spree_option_types: { hidden: false })
                                        }, through: :option_value_variants, source: :option_value

  base.has_many :video_on_demands, class_name: 'SpreeCmCommissioner::VideoOnDemand', dependent: :destroy
  base.has_many :complete_line_items, -> { complete }, class_name: 'Spree::LineItem'

  base.has_many :variant_blocks, class_name: 'SpreeCmCommissioner::VariantBlock', dependent: :destroy
  base.has_many :blocks, class_name: 'SpreeCmCommissioner::Block', through: :variant_blocks

  base.has_many :variant_guest_card_class, class_name: 'SpreeCmCommissioner::VariantGuestCardClass'
  base.has_many :guest_card_classes, class_name: 'SpreeCmCommissioner::GuestCardClass', through: :variant_guest_card_class

  base.has_many :inventory_items, class_name: 'SpreeCmCommissioner::InventoryItem'

  base.scope :subscribable, -> { active.joins(:product).where(product: { subscribable: true, status: :active }) }
  base.scope :with_permanent_stock, -> { where(product_type: base::PERMANENT_STOCK_PRODUCT_TYPES) }
  base.scope :with_non_permanent_stock, -> { where.not(product_type: base::PERMANENT_STOCK_PRODUCT_TYPES) }

  base.accepts_nested_attributes_for :option_values

  base.before_save -> { self.product_type = product.product_type }, if: -> { product_type.nil? }
end

Instance Method Details

#create_default_non_permanent_inventory_item!(quantity_available: nil, max_capacity: nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/spree_cm_commissioner/variant_decorator.rb', line 58

def create_default_non_permanent_inventory_item!(quantity_available: nil, max_capacity: nil)
  return if product_type.blank? # handle in case product not exist for variant.
  return unless should_track_inventory?
  return if default_inventory_item_exist?

  inventory_items.create!(
    product_type: product_type,
    quantity_available: [0, quantity_available || total_on_hand].max,
    max_capacity: [0, max_capacity || total_on_hand].max
  )
end

#default_inventory_item_exist?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/models/spree_cm_commissioner/variant_decorator.rb', line 54

def default_inventory_item_exist?
  inventory_items.exists?(inventory_date: nil)
end

#delivery_required?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/models/spree_cm_commissioner/variant_decorator.rb', line 41

def delivery_required?
  delivery_option == 'delivery'
end

#discontinued?Boolean

override

Returns:

  • (Boolean)


50
51
52
# File 'app/models/spree_cm_commissioner/variant_decorator.rb', line 50

def discontinued?
  super || product.discontinued?
end

#display_variantObject



79
80
81
82
83
# File 'app/models/spree_cm_commissioner/variant_decorator.rb', line 79

def display_variant
  display_sku = sku.split('-').join(' ')
  display_price = Spree::Money.new(price.to_i, currency: currency).to_s
  "#{display_sku} - #{display_price}"
end

#in_stock?(options = {}) ⇒ Boolean

override

Returns:

  • (Boolean)


86
87
88
# File 'app/models/spree_cm_commissioner/variant_decorator.rb', line 86

def in_stock?(options = {})
  SpreeCmCommissioner::Stock::AvailabilityChecker.new(self, options).can_supply?
end

#non_digital_ecommerce?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/models/spree_cm_commissioner/variant_decorator.rb', line 45

def non_digital_ecommerce?
  !digital? && ecommerce?
end

#options_textObject

override



71
72
73
# File 'app/models/spree_cm_commissioner/variant_decorator.rb', line 71

def options_text
  @options_text ||= Spree::Variants::VisableOptionsPresenter.new(self).to_sentence
end

#price_for_date(currency:, date:) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'app/models/spree_cm_commissioner/variant_decorator.rb', line 90

def price_for_date(currency:, date:)
  currency = currency.upcase

  prices.joins(:inventory_item)
        .find_by(
          currency: currency,
          inventory_item: { inventory_date: date }
        )
end

#selected_option_value_idsObject



75
76
77
# File 'app/models/spree_cm_commissioner/variant_decorator.rb', line 75

def selected_option_value_ids
  option_value_variants.pluck(:option_value_id)
end