Class: SolidusAdmin::UI::Thumbnail::Component

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

Constant Summary collapse

SIZES =
{
  s: 'h-6 w-6',
  m: 'h-10 w-10',
  l: 'h-20 w-20',
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(icon: nil, size: :m, **attributes) ⇒ Component

Returns a new instance of Component.



10
11
12
13
14
# File 'app/components/solidus_admin/ui/thumbnail/component.rb', line 10

def initialize(icon: nil, size: :m, **attributes)
  @icon = icon
  @size = size
  @attributes = attributes
end

Class Method Details

.for(record, **attrs) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/components/solidus_admin/ui/thumbnail/component.rb', line 32

def self.for(record, **attrs)
  case record
  when *Spree::Config.adjustment_promotion_source_types then new(icon: "megaphone-line", **attrs)
  when Spree::UnitCancel then new(icon: "close-circle-line", **attrs)
  when Spree::TaxRate then new(icon: "percent-line", **attrs)
  when Spree::LineItem then self.for(record.variant, **attrs)
  when Spree::Product then self.for((record.images.first || record.master.images.first), **attrs)
  when Spree::Variant then self.for((record.images.first || record.product), **attrs)
  when Spree::Image then new(src: record.attachment&.url(:small), alt: record.alt, **attrs)
  when Spree::Order then new(icon: "shopping-bag-line", **attrs)
  when Spree::Shipment then new(icon: "truck-line", **attrs)
  else new(icon: "question-line", **attrs)
  end
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/components/solidus_admin/ui/thumbnail/component.rb', line 16

def call
  icon = if @icon
    icon_tag(@icon, class: "bg-gray-25 fill-gray-700 #{SIZES[@size]} p-2")
  else
    tag.img(**@attributes, class: "object-contain #{SIZES[@size]}")
  end

  tag.div(icon, class: "
    #{SIZES[@size]}
    rounded border border-gray-100
    bg-white overflow-hidden
    content-box
    #{@attributes[:class]}
  ")
end