Class: SolidusAdmin::StockItems::Index::Component

Inherits:
UI::Pages::Index::Component
  • Object
show all
Defined in:
app/components/solidus_admin/stock_items/index/component.rb

Instance Method Summary collapse

Instance Method Details

#back_orderable_columnObject



152
153
154
155
156
157
158
159
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 152

def back_orderable_column
  {
    header: :back_orderable,
    data: ->(stock_item) do
      stock_item.backorderable? ? component('ui/badge').yes : component('ui/badge').no
    end
  }
end

#columnsObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 59

def columns
  [
    image_column,
    name_column,
    sku_column,
    variant_column,
    stock_location_column,
    back_orderable_column,
    count_on_hand_column,
    stock_movements_column,
  ]
end

#count_on_hand_columnObject



161
162
163
164
165
166
167
168
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 161

def count_on_hand_column
  {
    header: :count_on_hand,
    data: ->(stock_item) do
       :div, stock_item.count_on_hand
    end
  }
end

#filtersObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 30

def filters
  [
    {
      label: t('.filters.stock_locations'),
      combinator: 'or',
      attribute: "stock_location_id",
      predicate: "eq",
      options: Spree::StockLocation.all.map do |stock_location|
        [
          stock_location.name.titleize,
          stock_location.id
        ]
      end
    },
    {
      label: t('.filters.variants'),
      combinator: 'or',
      attribute: "variant_id",
      predicate: "eq",
      options: Spree::Variant.all.map do |variant|
        [
          variant.descriptive_name,
          variant.id
        ]
      end
    },
  ]
end

#image_columnObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 72

def image_column
  {
    col: { class: "w-[72px]" },
    header: tag.span('aria-label': Spree::Image.model_name.human, role: 'text'),
    data: ->(stock_item) do
      image = stock_item.variant.gallery.images.first or return

      render(
        component('ui/thumbnail').new(
          src: image.url(:small),
          alt: stock_item.variant.name
        )
      )
    end
  }
end

#model_classObject



4
5
6
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 4

def model_class
  Spree::StockItem
end

#name_columnObject



89
90
91
92
93
94
95
96
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 89

def name_column
  {
    header: :name,
    data: ->(stock_item) do
       :div, stock_item.variant.name
    end
  }
end

#row_url(stock_item) ⇒ Object



16
17
18
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 16

def row_url(stock_item)
  solidus_admin.edit_stock_item_path(stock_item, _turbo_frame: :edit_stock_item_modal)
end

#scopesObject



20
21
22
23
24
25
26
27
28
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 20

def scopes
  [
    { label: t('.scopes.all_stock_items'), name: 'all', default: true },
    { label: t('.scopes.back_orderable'), name: 'back_orderable' },
    { label: t('.scopes.out_of_stock'), name: 'out_of_stock' },
    { label: t('.scopes.low_stock'), name: 'low_stock' },
    { label: t('.scopes.in_stock'), name: 'in_stock' },
  ]
end

#search_keyObject



8
9
10
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 8

def search_key
  :variant_product_name_or_variant_sku_or_variant_option_values_name_or_variant_option_values_presentation_cont
end

#search_urlObject



12
13
14
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 12

def search_url
  solidus_admin.stock_items_path
end

#sku_columnObject



98
99
100
101
102
103
104
105
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 98

def sku_column
  {
    header: :sku,
    data: ->(stock_item) do
       :div, stock_item.variant.sku
    end
  }
end

#stock_location_columnObject



122
123
124
125
126
127
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 122

def stock_location_column
  {
    header: :stock_location,
    data: ->(stock_item) { stock_item.stock_location.name },
  }
end

#stock_movement_countsObject

Cache the stock movement counts to avoid N+1 queries



130
131
132
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 130

def stock_movement_counts
  @stock_movement_counts ||= Spree::StockMovement.where(stock_item_id: @page.records.ids).group(:stock_item_id).count
end

#stock_movements_columnObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 134

def stock_movements_column
  {
    header: :stock_movements,
    data: -> do
      count = stock_movement_counts[_1.id] || 0

      link_to(
        "#{count} #{Spree::StockMovement.model_name.human(count:).downcase}",
        spree.admin_stock_location_stock_movements_path(
          _1.stock_location.id,
          q: { variant_sku_eq: _1.variant.sku },
        ),
        class: 'body-link'
      )
    end
  }
end

#turbo_framesObject



170
171
172
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 170

def turbo_frames
  %w[edit_stock_item_modal]
end

#variant_columnObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/components/solidus_admin/stock_items/index/component.rb', line 107

def variant_column
  {
    header: :variant,
    data: ->(stock_item) do
      (:div, class: "space-y-0.5") do
        safe_join(
          stock_item.variant.option_values.sort_by(&:option_type_name).map do |option_value|
            render(component('ui/badge').new(name: "#{option_value.option_type_presentation}: #{option_value.presentation}"))
          end
        )
      end
    end
  }
end