Class: IronAdmin::Resources::DataTableComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- IronAdmin::Resources::DataTableComponent
- Defined in:
- app/components/iron_admin/resources/data_table_component.rb
Overview
Renders the main data table for resource index pages.
Instance Attribute Summary collapse
-
#base_url ⇒ String
readonly
Base URL for sorting links.
-
#current_direction ⇒ Symbol?
readonly
Current sort direction.
-
#current_sort ⇒ Symbol?
readonly
Current sort column.
-
#current_user ⇒ Object?
readonly
Current user.
-
#fields ⇒ Array<IronAdmin::Field>
readonly
Fields to show as columns.
-
#records ⇒ ActiveRecord::Relation
readonly
Records to display.
-
#resource_class ⇒ Class
readonly
The resource class.
Instance Method Summary collapse
-
#header_classes ⇒ String
private
CSS classes for table header.
-
#initialize(records:, fields:, resource_class:, base_url:, current_sort: nil, current_direction: nil, current_user: nil) ⇒ DataTableComponent
constructor
A new instance of DataTableComponent.
-
#row_dom_id(record) ⇒ String
private
Stable Turbo Stream target id for a record row.
-
#sort_direction ⇒ String?
private
Current sort direction.
-
#sort_url(field_name) ⇒ String
private
URL with sort parameters.
-
#sorted?(field_name) ⇒ Boolean
private
Whether this field is currently sorted.
-
#table_classes ⇒ String
private
CSS classes for table element.
-
#theme ⇒ IronAdmin::Configuration::Theme
private
Theme configuration.
-
#visible_fields ⇒ Array<IronAdmin::Field>
private
Fields visible to current user.
Constructor Details
#initialize(records:, fields:, resource_class:, base_url:, current_sort: nil, current_direction: nil, current_user: nil) ⇒ DataTableComponent
Returns a new instance of DataTableComponent.
35 36 37 38 39 40 41 42 43 44 |
# File 'app/components/iron_admin/resources/data_table_component.rb', line 35 def initialize(records:, fields:, resource_class:, base_url:, current_sort: nil, current_direction: nil, current_user: nil) @records = records @fields = fields @resource_class = resource_class @current_sort = current_sort @current_direction = current_direction @base_url = base_url @current_user = current_user end |
Instance Attribute Details
#base_url ⇒ String (readonly)
Returns Base URL for sorting links.
25 26 27 |
# File 'app/components/iron_admin/resources/data_table_component.rb', line 25 def base_url @base_url end |
#current_direction ⇒ Symbol? (readonly)
Returns Current sort direction.
22 23 24 |
# File 'app/components/iron_admin/resources/data_table_component.rb', line 22 def current_direction @current_direction end |
#current_sort ⇒ Symbol? (readonly)
Returns Current sort column.
19 20 21 |
# File 'app/components/iron_admin/resources/data_table_component.rb', line 19 def current_sort @current_sort end |
#current_user ⇒ Object? (readonly)
Returns Current user.
28 29 30 |
# File 'app/components/iron_admin/resources/data_table_component.rb', line 28 def current_user @current_user end |
#fields ⇒ Array<IronAdmin::Field> (readonly)
Returns Fields to show as columns.
13 14 15 |
# File 'app/components/iron_admin/resources/data_table_component.rb', line 13 def fields @fields end |
#records ⇒ ActiveRecord::Relation (readonly)
Returns Records to display.
10 11 12 |
# File 'app/components/iron_admin/resources/data_table_component.rb', line 10 def records @records end |
#resource_class ⇒ Class (readonly)
Returns The resource class.
16 17 18 |
# File 'app/components/iron_admin/resources/data_table_component.rb', line 16 def resource_class @resource_class end |
Instance Method Details
#header_classes ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns CSS classes for table header.
66 67 68 |
# File 'app/components/iron_admin/resources/data_table_component.rb', line 66 def header_classes theme.table_header_bg end |
#row_dom_id(record) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Stable Turbo Stream target id for a record row.
95 96 97 |
# File 'app/components/iron_admin/resources/data_table_component.rb', line 95 def row_dom_id(record) "iron_admin_#{resource_class.resource_name}_row_#{record.id}" end |
#sort_direction ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Current sort direction.
89 90 91 |
# File 'app/components/iron_admin/resources/data_table_component.rb', line 89 def sort_direction current_direction end |
#sort_url(field_name) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns URL with sort parameters.
75 76 77 78 |
# File 'app/components/iron_admin/resources/data_table_component.rb', line 75 def sort_url(field_name) direction = current_sort == field_name.to_s && current_direction == "asc" ? "desc" : "asc" "#{base_url}&sort=#{field_name}&direction=#{direction}" end |
#sorted?(field_name) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Whether this field is currently sorted.
83 84 85 |
# File 'app/components/iron_admin/resources/data_table_component.rb', line 83 def sorted?(field_name) current_sort == field_name.to_s end |
#table_classes ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns CSS classes for table element.
60 61 62 |
# File 'app/components/iron_admin/resources/data_table_component.rb', line 60 def table_classes "min-w-full divide-y #{theme.card_bg} #{theme.border_radius} #{theme.card_shadow}" end |
#theme ⇒ IronAdmin::Configuration::Theme
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Theme configuration.
54 55 56 |
# File 'app/components/iron_admin/resources/data_table_component.rb', line 54 def theme IronAdmin.configuration.theme end |
#visible_fields ⇒ Array<IronAdmin::Field>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Fields visible to current user.
48 49 50 |
# File 'app/components/iron_admin/resources/data_table_component.rb', line 48 def visible_fields @visible_fields ||= @fields.select { |f| f.visible?(@current_user) } end |