Class: SolidusAdmin::Products::Stock::Component

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/solidus_admin/products/stock/component.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(on_hand:, variants_count:) ⇒ Component

Returns a new instance of Component.



18
19
20
21
# File 'app/components/solidus_admin/products/stock/component.rb', line 18

def initialize(on_hand:, variants_count:)
  @on_hand = on_hand
  @variants_count = variants_count
end

Class Method Details

.from_product(product) ⇒ Object



4
5
6
7
8
9
# File 'app/components/solidus_admin/products/stock/component.rb', line 4

def self.from_product(product)
  new(
    on_hand: product.total_on_hand,
    variants_count: product.variants.count,
  )
end

.from_variant(variant) ⇒ Object



11
12
13
14
15
16
# File 'app/components/solidus_admin/products/stock/component.rb', line 11

def self.from_variant(variant)
  new(
    on_hand: variant.total_on_hand,
    variants_count: nil,
  )
end

Instance Method Details

#callObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/components/solidus_admin/products/stock/component.rb', line 23

def call
  stock_info =
    case @on_hand
    when Float::INFINITY
      tag.span t('.stock.in_stock', on_hand: t('.stock.infinity')), class: 'text-forest'
    when 1..Float::INFINITY
      tag.span t('.stock.in_stock', on_hand: @on_hand), class: 'text-forest'
    else
      tag.span t('.stock.in_stock', on_hand: @on_hand), class: 'text-red-500'
    end

  variant_info = t('.for_variants', count: @variants_count) if @variants_count

  tag.span safe_join([stock_info, variant_info], ' ')
end