katalyst-tables 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/README.md +15 -3
  4. data/app/assets/stylesheets/katalyst/tables/_index.scss +1 -0
  5. data/app/assets/stylesheets/katalyst/tables/_summary.scss +14 -0
  6. data/app/assets/stylesheets/katalyst/tables/typed-columns/_boolean.scss +1 -1
  7. data/app/assets/stylesheets/katalyst/tables/typed-columns/_currency.scss +3 -0
  8. data/app/assets/stylesheets/katalyst/tables/typed-columns/_date.scss +1 -1
  9. data/app/assets/stylesheets/katalyst/tables/typed-columns/_datetime.scss +1 -1
  10. data/app/assets/stylesheets/katalyst/tables/typed-columns/_number.scss +3 -0
  11. data/app/components/concerns/katalyst/tables/has_table_content.rb +2 -2
  12. data/app/components/concerns/katalyst/tables/row_renderer.rb +1 -1
  13. data/app/components/concerns/katalyst/tables/sortable.rb +1 -1
  14. data/app/components/katalyst/summary_table_component.html.erb +15 -0
  15. data/app/components/katalyst/summary_table_component.rb +44 -0
  16. data/app/components/katalyst/tables/summary/body_component.html.erb +3 -0
  17. data/app/components/katalyst/tables/summary/body_component.rb +10 -0
  18. data/app/components/katalyst/tables/summary/header_component.html.erb +3 -0
  19. data/app/components/katalyst/tables/summary/header_component.rb +10 -0
  20. data/app/components/katalyst/tables/summary/row_component.html.erb +4 -0
  21. data/app/components/katalyst/tables/summary/row_component.rb +12 -0
  22. data/app/controllers/concerns/katalyst/tables/backend.rb +15 -0
  23. data/app/helpers/katalyst/tables/frontend.rb +19 -0
  24. data/app/models/concerns/katalyst/tables/collection/pagination.rb +2 -2
  25. data/app/models/concerns/katalyst/tables/collection/sorting.rb +2 -2
  26. data/app/models/katalyst/tables/collection/filter.rb +2 -2
  27. data/config/importmap.rb +1 -0
  28. metadata +12 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cda2701479ecd72f9964ab539c5203bd0c300aeb962cf926552904575474ddea
4
- data.tar.gz: 6f43fc5e31c8536811e28cba0db4be3c0967db24d06ad22f2a72c89512030d87
3
+ metadata.gz: 196a8b7ff382215cbb156ff83099071aa983176353f819f5031aac774a88a572
4
+ data.tar.gz: 3d7463ccb20935e52500f186fb7c4024f3c8c236bfb9338a331a0830bf58edb5
5
5
  SHA512:
6
- metadata.gz: 14acdb5b09216c84c06b2082ed2a11888f0930b1807f7ade14cbc7fa02751f3c888a69b5397df7eb028395b7902eaad47abaa710e33fa24ecc6d6ed98757616f
7
- data.tar.gz: f3721287c65a5aa0d4e81c36d5f704b90e2995cf372376ba19c9b03cb5b0129a5f4c8a56b369a09b2c62e72eb585163d656abb303a8dd62aa8b7912193f710b5
6
+ metadata.gz: 325bfa1b161fa322acd419279a57ddb644f5a3e0289ca42294c9936fe7ea6fcded6f1d077f8866b5b70408d7d495c8dabe4733ba83a1f7652c4eb02115bc5438
7
+ data.tar.gz: 1382995a15fdc57829232223d67d20a691b9f5b908f4059c7b41dcfdb9396ce1d90f3431417ceb2b88a10eca880939608b20ec19dc421288f25b8c6bfad0c58c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## [3.1.0]
2
+ - Introduce summary tables
3
+ - Update ruby requirement >= 3.3
4
+
1
5
  ## [3.0.0]
2
6
 
3
7
  - Breaking change: remove Turbo Streams from table and pagination components,
data/README.md CHANGED
@@ -32,10 +32,11 @@ application.load(tables);
32
32
  ## Usage
33
33
 
34
34
  This gem provides entry points for backend and frontend concerns:
35
- * `Katalyst::TableComponent` can be used render encapsulated tables.
36
- * `Katalyst::Tables::Frontend` provides `table_with` for inline table generation
35
+ * `Katalyst::TableComponent` can be used render encapsulated tables,
36
+ * `Katalyst::SummaryTableComponent` can be used render a record using the table syntax,
37
+ * `Katalyst::Tables::Frontend` provides `table_with` for inline table generation,
37
38
  * `Katalyst::Tables::Collection::Base` provides a default entry point for
38
- building collections in your controller actions.
39
+ building collections in your controller actions
39
40
 
40
41
  ### Frontend
41
42
 
@@ -205,6 +206,17 @@ detect features such as sorting and generate the appropriate table header links.
205
206
  <%= table_with(collection:) %>
206
207
  ```
207
208
 
209
+ ## Summary tables
210
+ You can use the `Katalyst::SummaryTableComponent` to render a single record utilizing all the functionality from the
211
+ `Katalyst::TableComponent`.
212
+
213
+ ```erb
214
+ <%= summary_table_with model: @person do |row| %>
215
+ <% row.text :name %>
216
+ <% row.text :email %>
217
+ <% end %>
218
+ ```
219
+
208
220
  ## Extensions
209
221
 
210
222
  The following extensions are available and activated by default:
@@ -1 +1,2 @@
1
1
  @use "table";
2
+ @use "summary";
@@ -0,0 +1,14 @@
1
+ table.summary-table {
2
+ width: unset;
3
+ border-top: 1px solid var(--row-border-color);
4
+
5
+ th {
6
+ min-width: 12rem;
7
+ }
8
+
9
+ th,
10
+ td {
11
+ vertical-align: baseline;
12
+ text-align: left;
13
+ }
14
+ }
@@ -1,4 +1,4 @@
1
1
  // where psuedo selector has a specificity of 0 so it can be easily overwritten with simple selectors
2
- :where(th.type-boolean) {
2
+ :where(th.type-boolean, td.type-boolean) {
3
3
  width: var(--width-small);
4
4
  }
@@ -1,5 +1,8 @@
1
1
  // where psuedo selector has a specificity of 0 so it can be easily overwritten with simple selectors
2
2
  :where(th.type-currency, td.type-currency) {
3
3
  width: var(--width-small);
4
+ }
5
+
6
+ :where(thead th.type-currency, td.type-currency) {
4
7
  text-align: right;
5
8
  }
@@ -1,4 +1,4 @@
1
1
  // where psuedo selector has a specificity of 0 so it can be easily overwritten with simple selectors
2
- :where(th.type-date) {
2
+ :where(th.type-date, td.type-date) {
3
3
  width: var(--width-medium);
4
4
  }
@@ -1,4 +1,4 @@
1
1
  // where psuedo selector has a specificity of 0 so it can be easily overwritten with simple selectors
2
- :where(th.type-datetime) {
2
+ :where(th.type-datetime, td.type-date) {
3
3
  width: var(--width-medium);
4
4
  }
@@ -1,5 +1,8 @@
1
1
  // where psuedo selector has a specificity of 0 so it can be easily overwritten with simple selectors
2
2
  :where(th.type-number, td.type-number) {
3
3
  width: var(--width-small);
4
+ }
5
+
6
+ :where(thead th.type-number, td.type-number) {
4
7
  text-align: right;
5
8
  }
@@ -5,8 +5,8 @@ module Katalyst
5
5
  module HasTableContent # :nodoc:
6
6
  extend ActiveSupport::Concern
7
7
 
8
- def initialize(object_name: nil, partial: nil, as: nil, **options)
9
- super(**options)
8
+ def initialize(object_name: nil, partial: nil, as: nil, **)
9
+ super(**)
10
10
 
11
11
  @object_name = object_name || model_name&.i18n_key
12
12
  @partial = partial
@@ -50,7 +50,7 @@ module Katalyst
50
50
  def render_partial_template(view, locals, template, layout, block)
51
51
  locals[@local_name || template.variable] = @object
52
52
  locals[:row] = @row
53
- super(view, locals, template, layout, block)
53
+ super
54
54
  end
55
55
  end
56
56
  end
@@ -8,7 +8,7 @@ module Katalyst
8
8
  extend ActiveSupport::Concern
9
9
 
10
10
  def initialize(**)
11
- super(**)
11
+ super
12
12
 
13
13
  @header_row_cell_callbacks << method(:add_sorting_to_cell) if collection.sortable?
14
14
  end
@@ -0,0 +1,15 @@
1
+ <%# Simulating a normal table render for the side effects %>
2
+ <% capture do %>
3
+ <%= header_row %>
4
+ <% body_rows.each do |body| %>
5
+ <%= body %>
6
+ <% end %>
7
+ <% end %>
8
+
9
+ <%= tag.table(**html_attributes) do %>
10
+ <%= tag.tbody(**tbody_attributes) do %>
11
+ <% summary_rows.each do |summary_row| %>
12
+ <%= summary_row %>
13
+ <% end %>
14
+ <% end %>
15
+ <% end %>
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Katalyst
4
+ # A component for rendering a summary table for a model.
5
+ # @example
6
+ # <%= Katalyst::SummaryTableComponent.new(model: @person) do |row, person| %>
7
+ # <%= row.text :name do |cell| %>
8
+ # <%= link_to cell.value, person %>
9
+ # <% end %>
10
+ # <%= row.text :email %>
11
+ # <% end %>
12
+ # Generates:
13
+ # <table>
14
+ # <tr><th>Name</th><td><a href="/people/1">Aaron</a></td></tr>
15
+ # <tr><th>Email</th><td>aaron@example.com</td></tr>
16
+ # </table>
17
+ class SummaryTableComponent < TableComponent
18
+ renders_many :summary_rows, Tables::Summary::RowComponent
19
+
20
+ def initialize(model:, **)
21
+ super(collection: [model], **)
22
+
23
+ @summary_rows = []
24
+
25
+ update_html_attributes(class: "summary-table")
26
+ end
27
+
28
+ def with_cell(cell, &)
29
+ if row.header?
30
+ @summary_rows << with_summary_row do |row|
31
+ row.with_header do |header|
32
+ header.with_cell(cell)
33
+ end
34
+ end
35
+ @index = 0
36
+ else
37
+ @summary_rows[@index].with_body do |body|
38
+ body.with_cell(cell, &)
39
+ end
40
+ @index += 1
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ <% cells.each do |cell| %>
2
+ <%= cell %>
3
+ <% end %>
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Katalyst
4
+ module Tables
5
+ module Summary
6
+ class BodyComponent < Katalyst::Tables::BodyRowComponent
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ <% cells.each do |cell| %>
2
+ <%= cell %>
3
+ <% end %>
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Katalyst
4
+ module Tables
5
+ module Summary
6
+ class HeaderComponent < Katalyst::Tables::HeaderRowComponent
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ <tr>
2
+ <%= header %>
3
+ <%= body %>
4
+ </tr>
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Katalyst
4
+ module Tables
5
+ module Summary
6
+ class RowComponent < ViewComponent::Base
7
+ renders_one :header, Summary::HeaderComponent
8
+ renders_one :body, Summary::BodyComponent
9
+ end
10
+ end
11
+ end
12
+ end
@@ -8,6 +8,7 @@ module Katalyst
8
8
 
9
9
  included do
10
10
  class_attribute :_default_table_component, instance_accessor: false
11
+ class_attribute :_default_summary_table_component, instance_accessor: false
11
12
  end
12
13
 
13
14
  class_methods do
@@ -18,12 +19,26 @@ module Katalyst
18
19
  def default_table_component(component)
19
20
  self._default_table_component = component
20
21
  end
22
+
23
+ # Set the summary table component to be used as the default for all
24
+ # summary tables in the views rendered by this controller and its
25
+ # subclasses.
26
+ #
27
+ # @param component [Class] the summary table component class to use
28
+ def default_summary_table_component(component)
29
+ self._default_summary_table_component = component
30
+ end
21
31
  end
22
32
 
23
33
  # Default table component for this controller
24
34
  def default_table_component
25
35
  self.class._default_table_component
26
36
  end
37
+
38
+ # Default summary table component for this controller
39
+ def default_summary_table_component
40
+ self.class._default_summary_table_component
41
+ end
27
42
  end
28
43
  end
29
44
  end
@@ -55,12 +55,31 @@ module Katalyst
55
55
  render(Selectable::FormComponent.new(collection:, id:, primary_key:), &)
56
56
  end
57
57
 
58
+ # Construct a new summary table component.
59
+ #
60
+ # @param model [ActiveRecord::Base] subject for the table
61
+ #
62
+ # Blocks will receive the table in row-rendering mode (with row and record defined):
63
+ # @yieldparam [Katalyst::TableComponent] the table component to render rows
64
+ # @yieldparam [nil, Object] nil for the header column, or the given model for the value column
65
+ def summary_table_with(model:, **, &)
66
+ component ||= default_summary_table_component_class
67
+ component = component.new(model:, **)
68
+
69
+ render(component, &)
70
+ end
71
+
58
72
  private
59
73
 
60
74
  def default_table_component_class
61
75
  component = controller.try(:default_table_component) || TableComponent
62
76
  component.respond_to?(:constantize) ? component.constantize : component
63
77
  end
78
+
79
+ def default_summary_table_component_class
80
+ component = controller.try(:default_summary_table_component) || SummaryTableComponent
81
+ component.respond_to?(:constantize) ? component.constantize : component
82
+ end
64
83
  end
65
84
  end
66
85
  end
@@ -24,8 +24,8 @@ module Katalyst
24
24
  config_accessor :paginate
25
25
  end
26
26
 
27
- def initialize(paginate: config.paginate, **options)
28
- super(**options)
27
+ def initialize(paginate: config.paginate, **)
28
+ super(**)
29
29
 
30
30
  @paginate = paginate.freeze
31
31
  end
@@ -45,10 +45,10 @@ module Katalyst
45
45
  attr_reader :default_sort
46
46
  end
47
47
 
48
- def initialize(sorting: config.sorting, **options)
48
+ def initialize(sorting: config.sorting, **)
49
49
  @default_sort = sorting.to_param if sorting.present?
50
50
 
51
- super(sort: @default_sort, **options) # set default sort based on config
51
+ super(sort: @default_sort, **) # set default sort based on config
52
52
  end
53
53
 
54
54
  def default_sort?
@@ -40,8 +40,8 @@ module Katalyst
40
40
 
41
41
  attr_reader :param_key
42
42
 
43
- def initialize(param_key: :filters, **options)
44
- super(**options)
43
+ def initialize(param_key: :filters, **)
44
+ super(**)
45
45
 
46
46
  @param_key = param_key.to_sym
47
47
  end
data/config/importmap.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  pin "@katalyst/tables", to: "katalyst/tables.js"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katalyst-tables
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-14 00:00:00.000000000 Z
11
+ date: 2024-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: katalyst-html-attributes
@@ -56,6 +56,7 @@ files:
56
56
  - app/assets/config/katalyst-tables.js
57
57
  - app/assets/stylesheets/katalyst/tables/_index.scss
58
58
  - app/assets/stylesheets/katalyst/tables/_ordinal.scss
59
+ - app/assets/stylesheets/katalyst/tables/_summary.scss
59
60
  - app/assets/stylesheets/katalyst/tables/_table.scss
60
61
  - app/assets/stylesheets/katalyst/tables/typed-columns/_boolean.scss
61
62
  - app/assets/stylesheets/katalyst/tables/typed-columns/_currency.scss
@@ -69,6 +70,8 @@ files:
69
70
  - app/components/concerns/katalyst/tables/row_renderer.rb
70
71
  - app/components/concerns/katalyst/tables/selectable.rb
71
72
  - app/components/concerns/katalyst/tables/sortable.rb
73
+ - app/components/katalyst/summary_table_component.html.erb
74
+ - app/components/katalyst/summary_table_component.rb
72
75
  - app/components/katalyst/table_component.html.erb
73
76
  - app/components/katalyst/table_component.rb
74
77
  - app/components/katalyst/tables/body_row_component.html.erb
@@ -92,6 +95,12 @@ files:
92
95
  - app/components/katalyst/tables/pagy_nav_component.rb
93
96
  - app/components/katalyst/tables/selectable/form_component.html.erb
94
97
  - app/components/katalyst/tables/selectable/form_component.rb
98
+ - app/components/katalyst/tables/summary/body_component.html.erb
99
+ - app/components/katalyst/tables/summary/body_component.rb
100
+ - app/components/katalyst/tables/summary/header_component.html.erb
101
+ - app/components/katalyst/tables/summary/header_component.rb
102
+ - app/components/katalyst/tables/summary/row_component.html.erb
103
+ - app/components/katalyst/tables/summary/row_component.rb
95
104
  - app/controllers/concerns/katalyst/tables/backend.rb
96
105
  - app/helpers/katalyst/tables/frontend.rb
97
106
  - app/javascript/tables/application.js
@@ -131,7 +140,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
131
140
  requirements:
132
141
  - - ">="
133
142
  - !ruby/object:Gem::Version
134
- version: 3.2.0
143
+ version: 3.3.0
135
144
  required_rubygems_version: !ruby/object:Gem::Requirement
136
145
  requirements:
137
146
  - - ">="