Class: SolidusAdmin::UI::Table::Component

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

Instance Method Summary collapse

Constructor Details

#initialize(id:, model_class:, rows:, search_key:, search_url:, fade_row_proc: nil, columns: [], batch_actions: [], filters: [], prev_page_link: nil, next_page_link: nil) ⇒ Component

Returns a new instance of Component.

Parameters:

  • id (String)

    A unique identifier for the table component.

  • model_class (ActiveModel::Translation)

    The model class used for translations.

  • rows (Array)

    The collection of objects that will be passed to columns for display.

  • fade_row_proc (Proc, nil) (defaults to: nil)

    A proc determining if a row should have a faded appearance.

  • search_key (Symbol)

    The key for searching.

  • search_url (String)

    The base URL for searching.

  • columns (Array<Hash>) (defaults to: [])

    The array of column definitions.

  • batch_actions (Array<Hash>) (defaults to: [])

    The array of batch action definitions.

  • filters (Array<Hash>) (defaults to: [])

    The array of filter definitions.

  • prev_page_link (String, nil) (defaults to: nil)

    The link to the previous page.

  • next_page_link (String, nil) (defaults to: nil)

    The link to the next page.

Options Hash (columns:):

  • :header (Symbol|Proc|#to_s)

    The column header.

  • :data (Symbol|Proc|#to_s)

    The data accessor for the column.

  • :class_name (String) — default: optional

    The class name for the column.

Options Hash (batch_actions:):

  • :display_name (String)

    The batch action display name.

  • :icon (String)

    The batch action icon.

  • :action (String)

    The batch action path.

  • :method (String)

    The batch action HTTP method for the provided path.

Options Hash (filters:):

  • :name (String)

    The filter’s name.

  • :value (Any)

    The filter’s value.

  • :label (String)

    The filter’s label.



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
# File 'app/components/solidus_admin/ui/table/component.rb', line 30

def initialize(
  id:,
  model_class:,
  rows:,
  search_key:,
  search_url:,
  fade_row_proc: nil,
  columns: [],
  batch_actions: [],
  filters: [],
  prev_page_link: nil,
  next_page_link: nil
)
  @columns = columns.map { Column.new(**_1) }
  @batch_actions = batch_actions.map { BatchAction.new(**_1) }
  @filters = filters.map { Filter.new(**_1) }
  @id = id
  @model_class = model_class
  @rows = rows
  @fade_row_proc = fade_row_proc
  @search_key = search_key
  @search_url = search_url
  @prev_page_link = prev_page_link
  @next_page_link = next_page_link

  @columns.unshift selectable_column if batch_actions.present?
end

Instance Method Details

#batch_actions_form_idObject



86
87
88
# File 'app/components/solidus_admin/ui/table/component.rb', line 86

def batch_actions_form_id
  @batch_actions_form_id ||= "#{stimulus_id}--batch-actions-#{@id}"
end

#render_batch_action_button(batch_action) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/components/solidus_admin/ui/table/component.rb', line 98

def render_batch_action_button(batch_action)
  render component("ui/button").new(
    name: request_forgery_protection_token,
    value: form_authenticity_token(form_options: {
      action: batch_action.action,
      method: batch_action.method,
    }),
    formaction: batch_action.action,
    formmethod: batch_action.method,
    form: batch_actions_form_id,
    type: :submit,
    icon: batch_action.icon,
    text: batch_action.display_name,
    scheme: :secondary,
  )
end

#render_data_cell(cell, data) ⇒ Object



131
132
133
134
135
136
137
# File 'app/components/solidus_admin/ui/table/component.rb', line 131

def render_data_cell(cell, data)
  cell = cell.call(data) if cell.respond_to?(:call)
  cell = data.public_send(cell) if cell.is_a?(Symbol)
  cell = cell.render_in(self) if cell.respond_to?(:render_in)

  (:td, (:div, cell, class: "flex items-center gap-1.5"), class: "py-2 px-4 h-10 vertical-align-middle leading-none")
end

#render_header_cell(cell, **attrs) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/components/solidus_admin/ui/table/component.rb', line 115

def render_header_cell(cell, **attrs)
  cell = cell.call if cell.respond_to?(:call)
  cell = @model_class.human_attribute_name(cell) if cell.is_a?(Symbol)
  cell = cell.render_in(self) if cell.respond_to?(:render_in)

  (:th, cell, class: %{
    border-b
    border-gray-100
    px-4
    h-9
    font-semibold
    vertical-align-middle
    leading-none
  }, **attrs)
end

#resource_plural_nameObject



58
59
60
# File 'app/components/solidus_admin/ui/table/component.rb', line 58

def resource_plural_name
  @model_class.model_name.human.pluralize
end

#row_class_for(row) ⇒ Object



139
140
141
142
143
144
# File 'app/components/solidus_admin/ui/table/component.rb', line 139

def row_class_for(row)
  classes = ['border-b', 'border-gray-100']
  classes << ['bg-gray-15', 'text-gray-700'] if @fade_row_proc&.call(row)

  classes.join(' ')
end

#search_form_idObject



94
95
96
# File 'app/components/solidus_admin/ui/table/component.rb', line 94

def search_form_id
  @search_form_id ||= "#{stimulus_id}--search-form-#{@id}"
end

#selectable_columnObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/components/solidus_admin/ui/table/component.rb', line 62

def selectable_column
  @selectable_column ||= Column.new(
    header: -> {
      component("ui/forms/checkbox").new(
        form: batch_actions_form_id,
        "data-action": "#{stimulus_id}#selectAllRows",
        "data-#{stimulus_id}-target": "headerCheckbox",
        "aria-label": t('.select_all'),
      )
    },
    data: ->(data) {
      component("ui/forms/checkbox").new(
        name: "id[]",
        form: batch_actions_form_id,
        value: data.id,
        "data-action": "#{stimulus_id}#selectRow",
        "data-#{stimulus_id}-target": "checkbox",
        "aria-label": t('.select_row'),
      )
    },
    class_name: 'w-[52px]',
  )
end

#table_frame_idObject



90
91
92
# File 'app/components/solidus_admin/ui/table/component.rb', line 90

def table_frame_id
  @table_frame_id ||= "#{stimulus_id}--table-frame-#{@id}"
end