katalyst-tables 1.0.0 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fff2924a5584c83799ccce74292d2597083e81fd771d24022a517f38e89147ef
4
- data.tar.gz: d0230ed5a686ef5f5cedeb54f96ff8a206955d718a66420940087550958f7997
3
+ metadata.gz: 5482118f21175e8c1dbff1a397fa7b5ff68480783a2c47da2a92c0436c66e739
4
+ data.tar.gz: 99e70f74ef353cce895305e5ad39a98dc690efd7f0c68d4305867fbcdc23c941
5
5
  SHA512:
6
- metadata.gz: 1655096befcbe0fcd8e11e3bf7240b6975ca3d6d1c3eef6e09d22b04aadd5697b37ebc124b9ebb0ddc7d1742e947a5d42c47f70f2e803bb9e2f04b54517cc99a
7
- data.tar.gz: eca6c92288908ef026165bef43b8450187a3e101acfa94e67f8cf635125ade4567eab914bc1f44cae0afef80561efbd226302e9787a007e1bc5fd82e0fe6a5cf
6
+ metadata.gz: 219380d0ccb99e001c9604594d0aa68a58925bbd745d87898af359316192e61727d3549c4ab64198c31656c663ec00809d93ddc8034921143f8169fcb49f7769
7
+ data.tar.gz: b9d8a9e03604f9b39389d218bfceb4d354343bd0d1da4ff3bd7ca55f66446ba8f5fbc8430b9d5b22974f1210eed344ce0d579bccb209e29a0d35ca59982d4c63
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.1.0] - 2023-07-18
4
+
5
+ - Replaces `param_key` with `i18n_key` for attribute lookup in locale file
6
+ - Remove controller and url helpers from backend
7
+ - No changes required to existing code unless you were using the internal
8
+ classes directly
9
+
3
10
  ## [1.0.0] - 2022-03-23
4
11
 
5
12
  - Initial release
@@ -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,8 +22,7 @@ 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
@@ -25,6 +25,7 @@ module Katalyst
25
25
  delegate :content_tag,
26
26
  :link_to,
27
27
  :render,
28
+ :request,
28
29
  :translate,
29
30
  :with_output_buffer,
30
31
  to: :template
@@ -46,7 +46,7 @@ module Katalyst
46
46
 
47
47
  def sort_link(content)
48
48
  (@html_options["data"] ||= {})["sort"] = sort.status(method)
49
- link_to(content, @table.sort.url_for(method))
49
+ link_to(content, sort_url_for(sort: sort.toggle(method)))
50
50
  end
51
51
  end
52
52
  end
@@ -4,6 +4,22 @@ module Katalyst
4
4
  module Tables
5
5
  module Frontend
6
6
  module Helper # :nodoc:
7
+ extend ActiveSupport::Concern
8
+
9
+ # Generates a url for applying/toggling sort for the given column.
10
+ #
11
+ # @param sort [String, nil] sort parameter to apply, or nil to remove sorting
12
+ # @return [String] URL for toggling column sorting
13
+ def sort_url_for(sort: nil)
14
+ # Implementation inspired by pagy's `pagy_url_for` helper.
15
+ # Preserve any existing GET parameters
16
+ # CAUTION: these parameters are not sanitised
17
+ params = request.GET.merge("sort" => sort).except("page")
18
+ query_string = params.empty? ? "" : "?#{Rack::Utils.build_nested_query(params)}"
19
+
20
+ "#{request.path}#{query_string}"
21
+ end
22
+
7
23
  private
8
24
 
9
25
  def html_options_for_table_with(html: {}, **options)
@@ -17,7 +17,7 @@ module Katalyst
17
17
  def table_with(collection:, **options, &block)
18
18
  table_options = options.slice(:header, :object_name, :sort)
19
19
 
20
- table_options[:object_name] ||= collection.try(:model_name)&.param_key
20
+ table_options[:object_name] ||= collection.try(:model_name)&.i18n_key
21
21
 
22
22
  html_options = html_options_for_table_with(**options)
23
23
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Katalyst
4
4
  module Tables
5
- VERSION = "1.0.0"
5
+ VERSION = "1.1.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,29 +1,15 @@
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: 1.1.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
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: rspec
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '3.2'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '3.2'
11
+ date: 2023-07-18 00:00:00.000000000 Z
12
+ dependencies: []
27
13
  description: Builder-style HTML table generator for building tabular index views.
28
14
  Supports sorting by columns.
29
15
  email:
@@ -56,7 +42,7 @@ metadata:
56
42
  homepage_uri: https://github.com/katalyst/katalyst-tables
57
43
  source_code_uri: https://github.com/katalyst/katalyst-tables
58
44
  changelog_uri: https://github.com/katalyst/katalyst-tables/blobs/main/CHANGELOG.md
59
- post_install_message:
45
+ post_install_message:
60
46
  rdoc_options: []
61
47
  require_paths:
62
48
  - lib
@@ -71,8 +57,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
57
  - !ruby/object:Gem::Version
72
58
  version: '0'
73
59
  requirements: []
74
- rubygems_version: 3.1.6
75
- signing_key:
60
+ rubygems_version: 3.4.12
61
+ signing_key:
76
62
  specification_version: 4
77
63
  summary: HTML table generator for Rails views
78
64
  test_files: []