katalyst-tables 2.0.0 → 2.1.1

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 (31) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/README.md +151 -103
  4. data/app/assets/config/katalyst-tables.js +1 -0
  5. data/app/assets/javascripts/controllers/tables/turbo_collection_controller.js +22 -0
  6. data/app/components/concerns/katalyst/tables/configurable_component.rb +31 -0
  7. data/app/components/concerns/katalyst/tables/has_html_attributes.rb +45 -0
  8. data/app/components/concerns/katalyst/tables/has_table_content.rb +33 -0
  9. data/app/components/concerns/katalyst/tables/sortable.rb +32 -0
  10. data/app/components/concerns/katalyst/tables/turbo_replaceable.rb +62 -0
  11. data/app/components/katalyst/table_component.rb +51 -40
  12. data/app/components/katalyst/tables/body_cell_component.rb +4 -4
  13. data/app/components/katalyst/tables/body_row_component.rb +3 -3
  14. data/app/components/katalyst/tables/empty_caption_component.html.erb +6 -0
  15. data/app/components/katalyst/tables/empty_caption_component.rb +38 -0
  16. data/app/components/katalyst/tables/header_cell_component.rb +20 -16
  17. data/app/components/katalyst/tables/header_row_component.rb +6 -5
  18. data/app/components/katalyst/tables/pagy_nav_component.rb +26 -0
  19. data/app/components/katalyst/turbo/pagy_nav_component.rb +23 -0
  20. data/app/components/katalyst/turbo/table_component.rb +48 -0
  21. data/app/models/concerns/katalyst/tables/collection/core.rb +67 -0
  22. data/app/models/concerns/katalyst/tables/collection/pagination.rb +66 -0
  23. data/app/models/concerns/katalyst/tables/collection/reducers.rb +67 -0
  24. data/app/models/concerns/katalyst/tables/collection/sorting.rb +63 -0
  25. data/app/models/katalyst/tables/collection.rb +32 -0
  26. data/config/importmap.rb +7 -0
  27. data/lib/katalyst/tables/backend/sort_form.rb +16 -2
  28. data/lib/katalyst/tables/engine.rb +13 -0
  29. data/lib/katalyst/tables/frontend/helper.rb +4 -14
  30. data/lib/katalyst/tables/version.rb +1 -1
  31. metadata +34 -2
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Katalyst
4
+ module Tables
5
+ module Collection
6
+ # Entry point for creating a collection for use with table components.
7
+ # This class is intended to be subclassed, i.e.:
8
+ #
9
+ # class ApplicationController < ActionController::Base
10
+ # class Collection < Katalyst::Tables::Collection::Base
11
+ # ...
12
+ # end
13
+ # end
14
+ #
15
+ # In the context of a controller action, construct a collection, apply it
16
+ # to a model, then pass the result to the view component:
17
+ # ```
18
+ # collection = Collection.new.with_params(params).apply(People.all)
19
+ # table = Katalyst::TableComponent.new(collection: collection)
20
+ # render table
21
+ # ````
22
+ class Base
23
+ include Core
24
+ include Pagination
25
+ include Sorting
26
+
27
+ use(Sorting::Sort)
28
+ use(Pagination::Paginate)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
4
+
5
+ pin_all_from Katalyst::Tables::Engine.root.join("app/assets/javascripts"),
6
+ # preload in tests so that we don't start clicking before controllers load
7
+ preload: Rails.env.test?
@@ -8,11 +8,25 @@ module Katalyst
8
8
  class SortForm
9
9
  DIRECTIONS = %w[asc desc].freeze
10
10
 
11
- attr_accessor :column, :direction
11
+ attr_accessor :column, :direction, :default
12
12
 
13
- def initialize(column: nil, direction: nil)
13
+ def self.parse(param, default: nil)
14
+ column, direction = param.to_s.split
15
+ direction = "asc" unless DIRECTIONS.include?(direction)
16
+
17
+ default = SortForm.parse(default).to_param if default.present?
18
+
19
+ SortForm.new(column: column, direction: direction, default: default)
20
+ end
21
+
22
+ def initialize(column: nil, direction: nil, default: nil)
14
23
  self.column = column
15
24
  self.direction = direction
25
+ self.default = default
26
+ end
27
+
28
+ def to_param
29
+ "#{column} #{direction}"
16
30
  end
17
31
 
18
32
  # Returns true if the given collection supports sorting on the given
@@ -6,6 +6,19 @@ module Katalyst
6
6
  module Tables
7
7
  class Engine < ::Rails::Engine # :nodoc:
8
8
  isolate_namespace Katalyst::Tables
9
+
10
+ initializer "katalyst-tables.asset" do
11
+ config.after_initialize do |app|
12
+ app.config.assets.precompile += %w[katalyst-tables.js] if app.config.respond_to?(:assets)
13
+ end
14
+ end
15
+
16
+ initializer "katalyst-tables.importmap", before: "importmap" do |app|
17
+ if app.config.respond_to?(:importmap)
18
+ app.config.importmap.paths << root.join("config/importmap.rb")
19
+ app.config.importmap.cache_sweepers << root.join("app/assets/javascripts")
20
+ end
21
+ end
9
22
  end
10
23
  end
11
24
  end
@@ -3,30 +3,20 @@
3
3
  module Katalyst
4
4
  module Tables
5
5
  module Frontend
6
+ # @deprecated Use {Katalyst::TableComponent} instead.
6
7
  module Helper # :nodoc:
7
8
  extend ActiveSupport::Concern
8
9
 
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
10
  # Generates a url for applying/toggling sort for the given column.
22
11
  #
23
12
  # @param sort [String, nil] sort parameter to apply, or nil to remove sorting
24
13
  # @return [String] URL for toggling column sorting
25
- def sort_url_for(sort: nil)
14
+ # @deprecated Use {Katalyst::TablesComponent} instead.
15
+ def sort_url_for(sort: nil, default: nil)
26
16
  # Implementation inspired by pagy's `pagy_url_for` helper.
27
17
  # Preserve any existing GET parameters
28
18
  # CAUTION: these parameters are not sanitised
29
- params = if sort
19
+ params = if sort && !sort.eql?(default)
30
20
  request.GET.merge("sort" => sort).except("page")
31
21
  else
32
22
  request.GET.except("page", "sort")
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Katalyst
4
4
  module Tables
5
- VERSION = "2.0.0"
5
+ VERSION = "2.1.1"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katalyst-tables
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-20 00:00:00.000000000 Z
11
+ date: 2023-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: html-attributes-utils
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: view_component
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -35,11 +49,29 @@ files:
35
49
  - CHANGELOG.md
36
50
  - LICENSE.txt
37
51
  - README.md
52
+ - app/assets/config/katalyst-tables.js
53
+ - app/assets/javascripts/controllers/tables/turbo_collection_controller.js
54
+ - app/components/concerns/katalyst/tables/configurable_component.rb
55
+ - app/components/concerns/katalyst/tables/has_html_attributes.rb
56
+ - app/components/concerns/katalyst/tables/has_table_content.rb
57
+ - app/components/concerns/katalyst/tables/sortable.rb
58
+ - app/components/concerns/katalyst/tables/turbo_replaceable.rb
38
59
  - app/components/katalyst/table_component.rb
39
60
  - app/components/katalyst/tables/body_cell_component.rb
40
61
  - app/components/katalyst/tables/body_row_component.rb
62
+ - app/components/katalyst/tables/empty_caption_component.html.erb
63
+ - app/components/katalyst/tables/empty_caption_component.rb
41
64
  - app/components/katalyst/tables/header_cell_component.rb
42
65
  - app/components/katalyst/tables/header_row_component.rb
66
+ - app/components/katalyst/tables/pagy_nav_component.rb
67
+ - app/components/katalyst/turbo/pagy_nav_component.rb
68
+ - app/components/katalyst/turbo/table_component.rb
69
+ - app/models/concerns/katalyst/tables/collection/core.rb
70
+ - app/models/concerns/katalyst/tables/collection/pagination.rb
71
+ - app/models/concerns/katalyst/tables/collection/reducers.rb
72
+ - app/models/concerns/katalyst/tables/collection/sorting.rb
73
+ - app/models/katalyst/tables/collection.rb
74
+ - config/importmap.rb
43
75
  - lib/katalyst/tables.rb
44
76
  - lib/katalyst/tables/backend.rb
45
77
  - lib/katalyst/tables/backend/sort_form.rb