Class: Alchemy::Admin::Resource::Table

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/alchemy/admin/resource/table.rb

Overview

Renders a resource table with columns and buttons

Example

<%= render Alchemy::Admin::Resource::Table.new(@languages, query: @query) do |table| %>
  <% table.icon_column "translate-2", style: false %>
  <% table.column :name, sortable: true %>
  <% table.column :language_code, sortable: true %>
  <% table.column :page_layout do |language| %>
    <%= Alchemy::Page.human_layout_name(language.page_layout) %>
  <% end %>
  <% table.delete_button %>
  <% table.edit_button %>
<% end %>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, query: nil, nothing_found_label: Alchemy.t("Nothing found"), search_filter_params: {}, icon: nil) ⇒ Table

Returns a new instance of Table.



92
93
94
95
96
97
98
# File 'app/components/alchemy/admin/resource/table.rb', line 92

def initialize(collection, query: nil, nothing_found_label: Alchemy.t("Nothing found"), search_filter_params: {}, icon: nil)
  @collection = collection
  @query = query
  @nothing_found_label = nothing_found_label
  @search_filter_params = search_filter_params
  @icon = icon
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



41
42
43
# File 'app/components/alchemy/admin/resource/table.rb', line 41

def collection
  @collection
end

#nothing_found_labelObject (readonly)

Returns the value of attribute nothing_found_label.



41
42
43
# File 'app/components/alchemy/admin/resource/table.rb', line 41

def nothing_found_label
  @nothing_found_label
end

#search_filter_paramsObject (readonly)

Returns the value of attribute search_filter_params.



41
42
43
# File 'app/components/alchemy/admin/resource/table.rb', line 41

def search_filter_params
  @search_filter_params
end

Instance Method Details

#column(name, header: nil, sortable: false, type: nil, class_name: nil, &block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'app/components/alchemy/admin/resource/table.rb', line 100

def column(name, header: nil, sortable: false, type: nil, class_name: nil, &block)
  header ||= resource_handler.model.human_attribute_name(name)
  type ||= resource_handler.model.columns_hash[name.to_s]&.type
  attribute = resource_handler.attributes.find { |item| item[:name] == name.to_s } || {name: name, type: type}
  block ||= lambda { |item| render_attribute(item, attribute) }

  css_classes = [name, type, class_name].compact.join(" ")
  with_header(name, @query, css_classes: css_classes, text: header, type: type, sortable: sortable)
  with_cell(css_classes, &block)
end

#delete_button(tooltip: Alchemy.t("Delete"), confirm_message: Alchemy.t("Are you sure?")) ⇒ Object



117
118
119
120
121
# File 'app/components/alchemy/admin/resource/table.rb', line 117

def delete_button(tooltip: Alchemy.t("Delete"), confirm_message: Alchemy.t("Are you sure?"))
  with_action(:destroy, tooltip) do |row|
    helpers.delete_button(resource_path(row, search_filter_params), {message: confirm_message})
  end
end

#edit_button(tooltip: Alchemy.t("Edit"), dialog_title: tooltip, dialog_size: resource_window_size) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'app/components/alchemy/admin/resource/table.rb', line 123

def edit_button(tooltip: Alchemy.t("Edit"), dialog_title: tooltip, dialog_size: resource_window_size)
  with_action(:edit, tooltip) do |row|
    helpers.link_to_dialog render_icon(:edit),
      edit_resource_path(row, search_filter_params),
      {
        size: dialog_size,
        title: dialog_title
      },
      class: "icon_button"
  end
end

#icon_column(icon = nil, style: nil) ⇒ Object



111
112
113
114
115
# File 'app/components/alchemy/admin/resource/table.rb', line 111

def icon_column(icon = nil, style: nil)
  column(:icon, header: "") do |resource|
    render_icon(icon || yield(resource), size: "xl", style: style)
  end
end