katalyst-tables 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fff2924a5584c83799ccce74292d2597083e81fd771d24022a517f38e89147ef
4
- data.tar.gz: d0230ed5a686ef5f5cedeb54f96ff8a206955d718a66420940087550958f7997
3
+ metadata.gz: 4c4a55f7a1b45c9f70f357eaa670debf6457aca92a8ac7ea8b10df20140d1c23
4
+ data.tar.gz: 620cff7771f9de2d74c613d6b107136ea34c494862d2da4e612ccb55d8dee51d
5
5
  SHA512:
6
- metadata.gz: 1655096befcbe0fcd8e11e3bf7240b6975ca3d6d1c3eef6e09d22b04aadd5697b37ebc124b9ebb0ddc7d1742e947a5d42c47f70f2e803bb9e2f04b54517cc99a
7
- data.tar.gz: eca6c92288908ef026165bef43b8450187a3e101acfa94e67f8cf635125ade4567eab914bc1f44cae0afef80561efbd226302e9787a007e1bc5fd82e0fe6a5cf
6
+ metadata.gz: 15cc508fdbb3ad40baec51314fde01c3889b670d4ccd6366ea49171a45b979d441a4fb3fafe65e5bd07093375615b6d298ebe091f240122edf9d68b10471f251
7
+ data.tar.gz: e5f4186c070c81c4645bc3cf8a378927f502df6084d235c38416d98eb7e4ab45c775da2d22338d32bf5063774a575b4c7a92a556468f1b35d18c809db421922c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [2.0.0]
4
+
5
+ - Replaces builders with view_components
6
+ We want view components to be the way to build custom tables, and add more
7
+ complete features to the gem. This will be a breaking change, but we have
8
+ tried hard to retain compatibility with existing code. Unless you are using
9
+ a custom builder then it's unlikely that you will see any changes.
10
+ - If you are using custom builders, then you will need to update them to use
11
+ view components. See [[README.md]] for examples.
12
+
13
+ ## [1.1.0] - 2023-07-18
14
+
15
+ - Replaces `param_key` with `i18n_key` for attribute lookup in locale file
16
+ - Remove controller and url helpers from backend
17
+ - No changes required to existing code unless you were using the internal
18
+ classes directly
19
+
3
20
  ## [1.0.0] - 2022-03-23
4
21
 
5
22
  - Initial release
data/README.md CHANGED
@@ -7,18 +7,16 @@ Tools for building HTML tables from ActiveRecord collections.
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
- gem "katalyst-tables", git: "https://github.com/katalyst/katalyst-tables", branch: "main"
10
+ gem "katalyst-tables"
11
11
  ```
12
12
 
13
13
  And then execute:
14
14
 
15
15
  $ bundle install
16
16
 
17
- **Reminder:** If you have a rails server running, remember to restart the server to prevent the `uninitialized constant` error.
18
-
19
17
  ## Usage
20
18
 
21
- This gem provides two entry points: Frontend for use in your views, and Backend for use in your controllers. The backend
19
+ This gem provides two entry points: `Frontend` for use in your views, and `Backend` for use in your controllers. The backend
22
20
  entry point is optional, as it's only required if you want to support sorting by column headers.
23
21
 
24
22
  ### Frontend
@@ -35,7 +33,7 @@ Add `include Katalyst::Tables::Frontend` to your `ApplicationHelper` or similar.
35
33
  <% end %>
36
34
  ```
37
35
 
38
- `table_builder` will call your block once per row and accumulate the cells you generate into rows:
36
+ The table builder will call your block once per row and accumulate the cells you generate into rows:
39
37
 
40
38
  ```html
41
39
 
@@ -102,14 +100,14 @@ All cells generated in the table header iteration will automatically be header c
102
100
  in your body rows by passing `heading: true` when you generate the cell.
103
101
 
104
102
  ```erb
105
- <%= row.cell :id, heading: true %>
103
+ <% row.cell :id, heading: true %>
106
104
  ```
107
105
 
108
- The table header cells default to showing the titleized column name, but you can customize this in one of two ways:
106
+ The table header cells default to showing the capitalized column name, but you can customize this in one of two ways:
109
107
 
110
108
  * Set the value inline
111
109
  ```erb
112
- <%= row.cell :id, label: "ID" %>
110
+ <% row.cell :id, label: "ID" %>
113
111
  ```
114
112
  * Define a translation for the attribute
115
113
  ```yml
@@ -261,56 +259,42 @@ You can write a custom builder that helps generate this type of table by adding
261
259
  for generating the actions. This allows for a declarative table syntax, something like this:
262
260
 
263
261
  ```erb
264
- <%= table_with(collection: collection, builder: Test::ActionTable) do |row| %>
265
- <%= row.cell :name %>
266
- <%= row.actions do |cell| %>
262
+ <%= table_with(collection: collection, component: ActionTableComponent) do |row| %>
263
+ <% row.cell :name %>
264
+ <% row.actions do |cell| %>
267
265
  <%= cell.action "Edit", :edit %>
268
266
  <%= cell.action "Delete", :delete, method: :delete %>
269
267
  <% end %>
270
268
  <% end %>
271
269
  ```
272
270
 
273
- And the custom builder:
271
+ And the customized component:
274
272
 
275
273
  ```ruby
276
- class ActionTable < Katalyst::Tables::Frontend::TableBuilder
277
- def build(&block)
278
- (@html_options[:class] ||= []) << "action-table"
279
- super
280
- end
281
-
282
- def table_header_row(builder = ActionHeaderRow, &block)
283
- super
284
- end
274
+ class ActionTableComponent < Katalyst::TableComponent
285
275
 
286
- def table_header_cell(method, builder = ActionHeaderCell, **options)
287
- super
288
- end
276
+ config.header_row = "ActionHeaderRow"
277
+ config.body_row = "ActionBodyRow"
278
+ config.body_cell = "ActionBodyCell"
289
279
 
290
- def table_body_row(object, builder = ActionBodyRow, &block)
280
+ def call
281
+ options(class: "action-table")
291
282
  super
292
283
  end
293
284
 
294
- def table_body_cell(object, method, builder = ActionBodyCell, **options, &block)
295
- super
296
- end
297
-
298
- class ActionHeaderRow < Katalyst::Tables::Frontend::Builder::HeaderRow
285
+ class ActionHeaderRow < Katalyst::Tables::HeaderRowComponent
299
286
  def actions(&block)
300
- cell(:actions, class: "actions", label: "")
287
+ cell(:actions, class: "actions", label: "", &block)
301
288
  end
302
289
  end
303
290
 
304
- class ActionHeaderCell < Katalyst::Tables::Frontend::Builder::HeaderCell
305
- end
306
-
307
- class ActionBodyRow < Katalyst::Tables::Frontend::Builder::BodyRow
291
+ class ActionBodyRow < Katalyst::Tables::BodyRowComponent
308
292
  def actions(&block)
309
293
  cell(:actions, class: "actions", &block)
310
294
  end
311
295
  end
312
296
 
313
- class ActionBodyCell < Katalyst::Tables::Frontend::Builder::BodyCell
297
+ class ActionBodyCell < Katalyst::Tables::BodyCellComponent
314
298
  def action(label, href, **opts)
315
299
  content_tag :a, label, { href: href }.merge(opts)
316
300
  end
@@ -318,11 +302,11 @@ class ActionTable < Katalyst::Tables::Frontend::TableBuilder
318
302
  end
319
303
  ```
320
304
 
321
- If you have a table builder you want to reuse, you can set it as a default for some or all of your controllers:
305
+ If you have a table component you want to reuse, you can set it as a default for some or all of your controllers:
322
306
 
323
307
  ```html
324
308
  class ApplicationController < ActiveController::Base
325
- default_table_builder ActionTableBuilder
309
+ default_table_component ActionTableComponent
326
310
  end
327
311
  ```
328
312
 
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Katalyst
4
+ # A component for rendering a table from a collection, with a header row.
5
+ # ```erb
6
+ # <%= Katalyst::TableComponent.new(collection: @people) do |row, person| %>
7
+ # <%= row.cell :name do |cell| %>
8
+ # <%= link_to cell.value, person %>
9
+ # <% end %>
10
+ # <%= row.cell :email %>
11
+ # <% end %>
12
+ # ```
13
+ class TableComponent < ViewComponent::Base
14
+ include ActiveSupport::Configurable
15
+ include Tables::Frontend::Helper
16
+
17
+ attr_reader :collection, :sort, :object_name
18
+
19
+ # Workaround: ViewComponent::Base.config is incompatible with ActiveSupport::Configurable
20
+ @_config = Class.new(Configuration).new
21
+
22
+ config_accessor :header_row
23
+ config_accessor :header_cell
24
+ config_accessor :body_row
25
+ config_accessor :body_cell
26
+
27
+ def initialize(collection:,
28
+ sort: nil,
29
+ header: true,
30
+ object_name: collection.try(:model_name)&.i18n_key,
31
+ **html_options)
32
+ super
33
+
34
+ @collection = collection
35
+ @sort = sort
36
+ @header = header
37
+ @object_name = object_name
38
+ end
39
+
40
+ def call
41
+ tag.table(**@html_options) do
42
+ thead + tbody
43
+ end
44
+ end
45
+
46
+ def thead
47
+ return "".html_safe unless @header
48
+
49
+ tag.thead do
50
+ concat(render_header)
51
+ end
52
+ end
53
+
54
+ def tbody
55
+ tag.tbody do
56
+ collection.each do |record|
57
+ concat(render_row(record))
58
+ end
59
+ end
60
+ end
61
+
62
+ def render_header
63
+ # extract the column's block from the slot and pass it to the cell for rendering
64
+ self.class.header_row_component.new(self).render_in(view_context, &@__vc_render_in_block)
65
+ end
66
+
67
+ def render_row(record)
68
+ # extract the column's block from the slot and pass it to the cell for rendering
69
+ block = @__vc_render_in_block
70
+ self.class.body_row_component.new(self, record).render_in(view_context) do |row|
71
+ block.call(row, record)
72
+ end
73
+ end
74
+
75
+ def self.header_row_component
76
+ @header_row_component ||= const_get(config.header_row || "Katalyst::Tables::HeaderRowComponent")
77
+ end
78
+
79
+ def self.header_cell_component
80
+ @header_cell_component ||= const_get(config.header_cell || "Katalyst::Tables::HeaderCellComponent")
81
+ end
82
+
83
+ def self.body_row_component
84
+ @body_row_component ||= const_get(config.body_row || "Katalyst::Tables::BodyRowComponent")
85
+ end
86
+
87
+ def self.body_cell_component
88
+ @body_cell_component ||= const_get(config.body_cell || "Katalyst::Tables::BodyCellComponent")
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Katalyst
4
+ module Tables
5
+ class BodyCellComponent < ViewComponent::Base # :nodoc:
6
+ include Frontend::Helper
7
+
8
+ attr_reader :record
9
+
10
+ def initialize(table, record, attribute, heading: false, **html_options)
11
+ super(**html_options)
12
+
13
+ @table = table
14
+ @record = record
15
+ @attribute = attribute
16
+ @type = heading ? :th : :td
17
+ end
18
+
19
+ def before_render
20
+ # fallback if no content block is given
21
+ with_content(value.to_s) unless content?
22
+ end
23
+
24
+ def call
25
+ content # ensure content is set before rendering options
26
+
27
+ content_tag(@type, content, **@html_options)
28
+ end
29
+
30
+ # @return the object for this row.
31
+ def object
32
+ @record
33
+ end
34
+
35
+ def value
36
+ @record.public_send(@attribute)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Katalyst
4
+ module Tables
5
+ class BodyRowComponent < ViewComponent::Base # :nodoc:
6
+ include Frontend::Helper
7
+
8
+ renders_many :columns, ->(component) { component }
9
+
10
+ def initialize(table, record)
11
+ super()
12
+
13
+ @table = table
14
+ @record = record
15
+ end
16
+
17
+ def call
18
+ content # generate content before rendering
19
+
20
+ tag.tr(**@html_options) do
21
+ columns.each do |column|
22
+ concat(column.to_s)
23
+ end
24
+ end
25
+ end
26
+
27
+ def cell(attribute, **options, &block)
28
+ with_column(@table.class.body_cell_component.new(@table, @record, attribute, **options), &block)
29
+ end
30
+
31
+ def header?
32
+ false
33
+ end
34
+
35
+ def body?
36
+ true
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Katalyst
4
+ module Tables
5
+ class HeaderCellComponent < ViewComponent::Base # :nodoc:
6
+ include Frontend::Helper
7
+
8
+ delegate :object_name, :sort, to: :@table
9
+
10
+ def initialize(table, attribute, label: nil, **html_options)
11
+ super(**html_options)
12
+
13
+ @table = table
14
+ @attribute = attribute
15
+ @value = label
16
+ end
17
+
18
+ def call
19
+ content = if @table.sort&.supports?(@table.collection, @attribute)
20
+ sort_link(value) # writes to html_options
21
+ else
22
+ value
23
+ end
24
+
25
+ tag.th(content, **@html_options)
26
+ end
27
+
28
+ def value
29
+ if !@value.nil?
30
+ @value
31
+ elsif object_name.present?
32
+ translation
33
+ else
34
+ default_value
35
+ end
36
+ end
37
+
38
+ def translation(key = "activerecord.attributes.#{object_name}.#{@attribute}")
39
+ translate(key, default: default_value)
40
+ end
41
+
42
+ def default_value
43
+ @attribute.to_s.humanize.capitalize
44
+ end
45
+
46
+ private
47
+
48
+ def sort_link(content)
49
+ (@html_options["data"] ||= {})["sort"] = sort.status(@attribute)
50
+ link_to(content, sort_url_for(sort: sort.toggle(@attribute)))
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Katalyst
4
+ module Tables
5
+ class HeaderRowComponent < ViewComponent::Base # :nodoc:
6
+ include Frontend::Helper
7
+
8
+ renders_many :columns, ->(component) { component }
9
+
10
+ def initialize(table)
11
+ super()
12
+
13
+ @table = table
14
+ end
15
+
16
+ def call
17
+ content # generate content before rendering
18
+
19
+ tag.tr(**@html_options) do
20
+ columns.each do |column|
21
+ concat(column.to_s)
22
+ end
23
+ end
24
+ end
25
+
26
+ def cell(attribute, **options, &block)
27
+ with_column(@table.class.header_cell_component.new(@table, attribute, **options), &block)
28
+ end
29
+
30
+ def header?
31
+ true
32
+ end
33
+
34
+ def body?
35
+ false
36
+ end
37
+ end
38
+ end
39
+ end
@@ -10,11 +10,9 @@ module Katalyst
10
10
 
11
11
  attr_accessor :column, :direction
12
12
 
13
- def initialize(controller, column: nil, direction: nil)
13
+ def initialize(column: nil, direction: nil)
14
14
  self.column = column
15
15
  self.direction = direction
16
-
17
- @controller = controller
18
16
  end
19
17
 
20
18
  # Returns true if the given collection supports sorting on the given
@@ -38,35 +36,19 @@ module Katalyst
38
36
  direction if column.to_s == self.column
39
37
  end
40
38
 
41
- # Generates a url for applying/toggling sort for the given column.
39
+ # Calculates the sort parameter to apply when the given column is toggled.
42
40
  #
43
- # @param column [String, Symbol] the table column as defined in table_with
44
- # @return [String] URL for use as a link in a column header
45
- def url_for(column)
46
- # Implementation inspired by pagy's `pagy_url_for` helper.
47
-
48
- request = @controller.request
49
-
50
- # Preserve any existing GET parameters
51
- # CAUTION: these parameters are not sanitised
52
- params = request.GET.merge("sort" => "#{column} #{toggle_direction(column)}").except("page")
53
- query_string = params.empty? ? "" : "?#{Rack::Utils.build_nested_query(params)}"
54
-
55
- "#{request.path}#{query_string}"
56
- end
57
-
58
- # Generates a url for the current page without any sorting parameters.
59
- #
60
- # @return [String] URL for use as a link in a column header
61
- def unsorted_url
62
- request = @controller.request
63
-
64
- # Preserve any existing GET parameters but remove sort.
65
- # CAUTION: these parameters are not sanitised
66
- params = request.GET.except("sort", "page")
67
- query_string = params.empty? ? "" : "?#{Rack::Utils.build_nested_query(params)}"
41
+ # @param column [String, Symbol]
42
+ # @return [String]
43
+ def toggle(column)
44
+ return "#{column} asc" unless column.to_s == self.column
68
45
 
69
- "#{request.path}#{query_string}"
46
+ case direction
47
+ when "asc"
48
+ "#{column} desc"
49
+ when "desc"
50
+ "#{column} asc"
51
+ end
70
52
  end
71
53
 
72
54
  # Apply the constructed sort ordering to the collection.
@@ -92,17 +74,6 @@ module Katalyst
92
74
  def clear!
93
75
  self.column = self.direction = nil
94
76
  end
95
-
96
- def toggle_direction(column)
97
- return "asc" unless column.to_s == self.column
98
-
99
- case direction
100
- when "asc"
101
- "desc"
102
- when "desc"
103
- "asc"
104
- end
105
- end
106
77
  end
107
78
  end
108
79
  end
@@ -22,30 +22,29 @@ module Katalyst
22
22
  column, direction = params[:sort]&.split(" ")
23
23
  direction = "asc" unless SortForm::DIRECTIONS.include?(direction)
24
24
 
25
- SortForm.new(self,
26
- column: column,
25
+ SortForm.new(column: column,
27
26
  direction: direction)
28
27
  .apply(collection)
29
28
  end
30
29
 
31
30
  included do
32
- class_attribute :_default_table_builder, instance_accessor: false
31
+ class_attribute :_default_table_component, instance_accessor: false
33
32
  end
34
33
 
35
34
  class_methods do
36
- # Set the table builder to be used as the default for all tables
35
+ # Set the table component to be used as the default for all tables
37
36
  # in the views rendered by this controller and its subclasses.
38
37
  #
39
38
  # ==== Parameters
40
- # * <tt>builder</tt> - Default table builder, an instance of +Katalyst::Tables::Frontend::TableBuilder+
41
- def default_table_builder(builder)
42
- self._default_table_builder = builder
39
+ # * <tt>component</tt> - Default table component, an instance of +Katalyst::TableComponent+
40
+ def default_table_component(component)
41
+ self._default_table_component = component
43
42
  end
44
43
  end
45
44
 
46
- # Default table builder for the controller
47
- def default_table_builder
48
- self.class._default_table_builder
45
+ # Default table component for this controller
46
+ def default_table_component
47
+ self.class._default_table_component
49
48
  end
50
49
  end
51
50
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails"
4
+
5
+ module Katalyst
6
+ module Tables
7
+ class Engine < ::Rails::Engine # :nodoc:
8
+ isolate_namespace Katalyst::Tables
9
+ end
10
+ end
11
+ end
@@ -4,11 +4,36 @@ module Katalyst
4
4
  module Tables
5
5
  module Frontend
6
6
  module Helper # :nodoc:
7
- private
7
+ extend ActiveSupport::Concern
8
8
 
9
- def html_options_for_table_with(html: {}, **options)
10
- html_options = options.slice(:id, :class, :data).merge(html)
11
- html_options.stringify_keys!
9
+ def initialize(**options)
10
+ super()
11
+
12
+ options(**options)
13
+ end
14
+
15
+ # Add HTML options to the current component.
16
+ def options(html: {}, **options)
17
+ @html_options = options.slice(:id, :aria, :class, :data).merge(html)
18
+ @html_options.stringify_keys!
19
+ end
20
+
21
+ # Generates a url for applying/toggling sort for the given column.
22
+ #
23
+ # @param sort [String, nil] sort parameter to apply, or nil to remove sorting
24
+ # @return [String] URL for toggling column sorting
25
+ def sort_url_for(sort: nil)
26
+ # Implementation inspired by pagy's `pagy_url_for` helper.
27
+ # Preserve any existing GET parameters
28
+ # CAUTION: these parameters are not sanitised
29
+ params = if sort
30
+ request.GET.merge("sort" => sort).except("page")
31
+ else
32
+ request.GET.except("page", "sort")
33
+ end
34
+ query_string = params.empty? ? "" : "?#{Rack::Utils.build_nested_query(params)}"
35
+
36
+ "#{request.path}#{query_string}"
12
37
  end
13
38
  end
14
39
  end
@@ -1,51 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "frontend/builder/base"
4
- require_relative "frontend/builder/body_cell"
5
- require_relative "frontend/builder/body_row"
6
- require_relative "frontend/builder/header_cell"
7
- require_relative "frontend/builder/header_row"
8
3
  require_relative "frontend/helper"
9
- require_relative "frontend/table_builder"
10
4
 
11
5
  module Katalyst
12
6
  module Tables
13
7
  # View Helper for generating HTML tables. Include in your ApplicationHelper, or similar.
14
8
  module Frontend
15
- include Helper
16
-
17
- def table_with(collection:, **options, &block)
18
- table_options = options.slice(:header, :object_name, :sort)
19
-
20
- table_options[:object_name] ||= collection.try(:model_name)&.param_key
21
-
22
- html_options = html_options_for_table_with(**options)
23
-
24
- builder = options.fetch(:builder) { default_table_builder_class }
25
- builder.new(self, collection, table_options, html_options).build(&block)
26
- end
27
-
28
- def table_header_row(table, builder, &block)
29
- builder.new(table).build(&block)
30
- end
31
-
32
- def table_header_cell(table, method, builder, **options, &block)
33
- builder.new(table, method, **options).build(&block)
34
- end
35
-
36
- def table_body_row(table, object, builder, &block)
37
- builder.new(table, object).build(&block)
38
- end
39
-
40
- def table_body_cell(table, object, method, builder, **options, &block)
41
- builder.new(table, object, method, **options).build(&block)
9
+ def table_with(collection:, component: nil, **options, &block)
10
+ component ||= default_table_component_class
11
+ render(component.new(collection: collection, **options), &block)
42
12
  end
43
13
 
44
14
  private
45
15
 
46
- def default_table_builder_class
47
- builder = controller.try(:default_table_builder) || TableBuilder
48
- builder.respond_to?(:constantize) ? builder.constantize : builder
16
+ def default_table_component_class
17
+ component = controller.try(:default_table_component) || TableComponent
18
+ component.respond_to?(:constantize) ? component.constantize : component
49
19
  end
50
20
  end
51
21
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Katalyst
4
4
  module Tables
5
- VERSION = "1.0.0"
5
+ VERSION = "2.0.0"
6
6
  end
7
7
  end
@@ -1,9 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "view_component"
4
+
3
5
  require_relative "tables/backend"
6
+ require_relative "tables/engine"
4
7
  require_relative "tables/frontend"
5
8
  require_relative "tables/version"
6
9
 
10
+ require_relative "tables/engine" if Object.const_defined?("Rails")
11
+
7
12
  module Katalyst
8
13
  module Tables
9
14
  class Error < StandardError; end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katalyst-tables
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-23 00:00:00.000000000 Z
11
+ date: 2023-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rspec
14
+ name: view_component
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '3.2'
20
- type: :development
19
+ version: '0'
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '3.2'
26
+ version: '0'
27
27
  description: Builder-style HTML table generator for building tabular index views.
28
28
  Supports sorting by columns.
29
29
  email:
@@ -35,17 +35,17 @@ files:
35
35
  - CHANGELOG.md
36
36
  - LICENSE.txt
37
37
  - README.md
38
+ - app/components/katalyst/table_component.rb
39
+ - app/components/katalyst/tables/body_cell_component.rb
40
+ - app/components/katalyst/tables/body_row_component.rb
41
+ - app/components/katalyst/tables/header_cell_component.rb
42
+ - app/components/katalyst/tables/header_row_component.rb
38
43
  - lib/katalyst/tables.rb
39
44
  - lib/katalyst/tables/backend.rb
40
45
  - lib/katalyst/tables/backend/sort_form.rb
46
+ - lib/katalyst/tables/engine.rb
41
47
  - lib/katalyst/tables/frontend.rb
42
- - lib/katalyst/tables/frontend/builder/base.rb
43
- - lib/katalyst/tables/frontend/builder/body_cell.rb
44
- - lib/katalyst/tables/frontend/builder/body_row.rb
45
- - lib/katalyst/tables/frontend/builder/header_cell.rb
46
- - lib/katalyst/tables/frontend/builder/header_row.rb
47
48
  - lib/katalyst/tables/frontend/helper.rb
48
- - lib/katalyst/tables/frontend/table_builder.rb
49
49
  - lib/katalyst/tables/version.rb
50
50
  homepage: https://github.com/katalyst/katalyst-tables
51
51
  licenses:
@@ -56,7 +56,7 @@ metadata:
56
56
  homepage_uri: https://github.com/katalyst/katalyst-tables
57
57
  source_code_uri: https://github.com/katalyst/katalyst-tables
58
58
  changelog_uri: https://github.com/katalyst/katalyst-tables/blobs/main/CHANGELOG.md
59
- post_install_message:
59
+ post_install_message:
60
60
  rdoc_options: []
61
61
  require_paths:
62
62
  - lib
@@ -71,8 +71,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
71
  - !ruby/object:Gem::Version
72
72
  version: '0'
73
73
  requirements: []
74
- rubygems_version: 3.1.6
75
- signing_key:
74
+ rubygems_version: 3.4.12
75
+ signing_key:
76
76
  specification_version: 4
77
77
  summary: HTML table generator for Rails views
78
78
  test_files: []
@@ -1,62 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "active_support"
4
- require "active_support/core_ext/module/delegation"
5
-
6
- require_relative "../helper"
7
-
8
- module Katalyst
9
- module Tables
10
- module Frontend
11
- module Builder
12
- class Base # :nodoc:
13
- include Helper
14
-
15
- attr_reader :table
16
-
17
- delegate :sort,
18
- :table_header_cell,
19
- :table_header_row,
20
- :table_body_cell,
21
- :table_body_row,
22
- :template,
23
- to: :table
24
-
25
- delegate :content_tag,
26
- :link_to,
27
- :render,
28
- :translate,
29
- :with_output_buffer,
30
- to: :template
31
-
32
- def initialize(table, **options)
33
- @table = table
34
- @header = false
35
- self.options(**options)
36
- end
37
-
38
- def header?
39
- @header
40
- end
41
-
42
- def body?
43
- !@header
44
- end
45
-
46
- def options(**options)
47
- @html_options = html_options_for_table_with(**options)
48
- end
49
-
50
- private
51
-
52
- def table_tag(type, value = nil, &block)
53
- # capture output before calling tag, to allow users to modify `options` during body execution
54
- value = with_output_buffer(&block) if block_given?
55
-
56
- content_tag(type, value, @html_options, &block)
57
- end
58
- end
59
- end
60
- end
61
- end
62
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
-
5
- module Katalyst
6
- module Tables
7
- module Frontend
8
- module Builder
9
- class BodyCell < Base # :nodoc:
10
- attr_reader :object, :method
11
-
12
- def initialize(table, object, method, **options)
13
- super table, **options
14
-
15
- @type = options.fetch(:heading, false) ? :th : :td
16
- @object = object
17
- @method = method
18
- end
19
-
20
- def build
21
- table_tag(@type) { block_given? ? yield(self).to_s : value.to_s }
22
- end
23
-
24
- def value
25
- object.public_send(method)
26
- end
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "base"
4
-
5
- module Katalyst
6
- module Tables
7
- module Frontend
8
- module Builder
9
- class BodyRow < Base # :nodoc:
10
- attr_reader :object
11
-
12
- def initialize(table, object)
13
- super table
14
-
15
- @object = object
16
- end
17
-
18
- def build
19
- table_tag(:tr) { yield self, object }
20
- end
21
-
22
- def cell(method, **options, &block)
23
- table_body_cell(object, method, **options, &block)
24
- end
25
- end
26
- end
27
- end
28
- end
29
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "body_cell"
4
-
5
- module Katalyst
6
- module Tables
7
- module Frontend
8
- module Builder
9
- class HeaderCell < BodyCell # :nodoc:
10
- def initialize(table, method, **options)
11
- super(table, nil, method, **options)
12
-
13
- @value = options[:label]
14
- @header = true
15
- end
16
-
17
- def build(&_block)
18
- # NOTE: block ignored intentionally but subclasses may consume it
19
- if @table.sort&.supports?(@table.collection, method)
20
- content = sort_link(value) # writes to html_options
21
- table_tag :th, content # consumes options
22
- else
23
- table_tag :th, value
24
- end
25
- end
26
-
27
- def value
28
- if !@value.nil?
29
- @value
30
- elsif @table.object_name.present?
31
- translation
32
- else
33
- default_value
34
- end
35
- end
36
-
37
- def translation(key = "activerecord.attributes.#{@table.object_name}.#{method}")
38
- translate(key, default: default_value)
39
- end
40
-
41
- def default_value
42
- method.to_s.humanize.titleize
43
- end
44
-
45
- private
46
-
47
- def sort_link(content)
48
- (@html_options["data"] ||= {})["sort"] = sort.status(method)
49
- link_to(content, @table.sort.url_for(method))
50
- end
51
- end
52
- end
53
- end
54
- end
55
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "body_row"
4
-
5
- module Katalyst
6
- module Tables
7
- module Frontend
8
- module Builder
9
- class HeaderRow < BodyRow # :nodoc:
10
- def initialize(table)
11
- super table, nil
12
-
13
- @header = true
14
- end
15
-
16
- def cell(method, **options, &block)
17
- table_header_cell(method, **options, &block)
18
- end
19
- end
20
- end
21
- end
22
- end
23
- end
@@ -1,71 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "builder/body_cell"
4
- require_relative "builder/body_row"
5
- require_relative "builder/header_cell"
6
- require_relative "builder/header_row"
7
-
8
- module Katalyst
9
- module Tables
10
- module Frontend
11
- # Builder API for generating HTML tables from ActiveRecord.
12
- # @see Frontend#table_with
13
- class TableBuilder
14
- attr_reader :template, :collection, :object_name, :sort
15
-
16
- def initialize(template, collection, options, html_options)
17
- @template = template
18
- @collection = collection
19
- @header = options.fetch(:header, true)
20
- @object_name = options.fetch(:object_name, nil)
21
- @sort = options[:sort]
22
- @html_options = html_options
23
- end
24
-
25
- def table_header_row(builder = nil, &block)
26
- @template.table_header_row(self, builder || Builder::HeaderRow, &block)
27
- end
28
-
29
- def table_header_cell(method, builder = nil, **options, &block)
30
- @template.table_header_cell(self, method, builder || Builder::HeaderCell, **options, &block)
31
- end
32
-
33
- def table_body_row(object, builder = nil, &block)
34
- @template.table_body_row(self, object, builder || Builder::BodyRow, &block)
35
- end
36
-
37
- def table_body_cell(object, method, builder = nil, **options, &block)
38
- @template.table_body_cell(self, object, method, builder || Builder::BodyCell, **options, &block)
39
- end
40
-
41
- def build(&block)
42
- template.content_tag("table", @html_options) do
43
- thead(&block) + tbody(&block)
44
- end
45
- end
46
-
47
- private
48
-
49
- def thead(&block)
50
- return "".html_safe unless @header
51
-
52
- template.content_tag("thead") do
53
- table_header_row(&block)
54
- end
55
- end
56
-
57
- def tbody(&block)
58
- template.content_tag("tbody") do
59
- buffer = ActiveSupport::SafeBuffer.new
60
-
61
- collection.each do |object|
62
- buffer << table_body_row(object, &block)
63
- end
64
-
65
- buffer
66
- end
67
- end
68
- end
69
- end
70
- end
71
- end