katalyst-tables 3.4.6 → 3.5.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 (39) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/builds/katalyst/tables.esm.js +16 -4
  3. data/app/assets/builds/katalyst/tables.js +16 -4
  4. data/app/assets/builds/katalyst/tables.min.js +1 -1
  5. data/app/assets/builds/katalyst/tables.min.js.map +1 -1
  6. data/app/assets/stylesheets/katalyst/tables/_ordinal.scss +1 -1
  7. data/app/assets/stylesheets/katalyst/tables/_query.scss +20 -20
  8. data/app/components/katalyst/tables/query/input_component.html.erb +7 -6
  9. data/app/components/katalyst/tables/query/input_component.rb +23 -7
  10. data/app/components/katalyst/tables/query/modal_component.html.erb +7 -17
  11. data/app/components/katalyst/tables/query/modal_component.rb +7 -62
  12. data/app/components/katalyst/tables/query/suggestion_component.html.erb +3 -0
  13. data/app/components/katalyst/tables/query/suggestion_component.rb +35 -0
  14. data/app/components/katalyst/tables/query_component.rb +0 -1
  15. data/app/components/katalyst/tables/selectable/form_component.html.erb +1 -7
  16. data/app/components/katalyst/tables/selectable/form_component.rb +14 -1
  17. data/app/javascript/tables/query_controller.js +11 -4
  18. data/app/javascript/tables/query_input_controller.js +5 -0
  19. data/app/models/concerns/katalyst/tables/collection/query/array_value_parser.rb +19 -1
  20. data/app/models/concerns/katalyst/tables/collection/query/parser.rb +12 -11
  21. data/app/models/concerns/katalyst/tables/collection/query/single_value_parser.rb +10 -0
  22. data/app/models/concerns/katalyst/tables/collection/query/untagged_literal.rb +36 -0
  23. data/app/models/concerns/katalyst/tables/collection/query/value_parser.rb +11 -2
  24. data/app/models/concerns/katalyst/tables/collection/query.rb +11 -26
  25. data/app/models/concerns/katalyst/tables/collection/suggestions.rb +120 -0
  26. data/app/models/katalyst/tables/suggestions/attribute.rb +13 -0
  27. data/app/models/katalyst/tables/suggestions/base.rb +31 -0
  28. data/app/models/katalyst/tables/suggestions/constant_value.rb +28 -0
  29. data/app/models/katalyst/tables/suggestions/custom_value.rb +26 -0
  30. data/app/models/katalyst/tables/suggestions/database_value.rb +36 -0
  31. data/app/models/katalyst/tables/suggestions/search_value.rb +13 -0
  32. data/config/locales/tables.en.yml +9 -1
  33. data/lib/katalyst/tables/collection/type/boolean.rb +10 -2
  34. data/lib/katalyst/tables/collection/type/date.rb +7 -5
  35. data/lib/katalyst/tables/collection/type/enum.rb +4 -21
  36. data/lib/katalyst/tables/collection/type/helpers/extensions.rb +1 -11
  37. data/lib/katalyst/tables/collection/type/value.rb +14 -12
  38. metadata +12 -3
  39. data/lib/katalyst/tables/collection/type/example.rb +0 -30
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Katalyst
4
+ module Tables
5
+ module Suggestions
6
+ class CustomValue < Base
7
+ delegate :to_param, to: :@attribute_type
8
+
9
+ def initialize(value, name:, type:)
10
+ super(value)
11
+
12
+ @name = name
13
+ @attribute_type = type
14
+ end
15
+
16
+ def type
17
+ :custom_value
18
+ end
19
+
20
+ def value
21
+ to_param(@value)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Katalyst
4
+ module Tables
5
+ module Suggestions
6
+ class DatabaseValue < Base
7
+ attr_reader :model, :column
8
+
9
+ delegate :to_param, to: :@attribute_type
10
+
11
+ def initialize(name:, type:, model:, column:, value:)
12
+ super(value)
13
+
14
+ @attribute_type = type
15
+ @model = model
16
+ @column = column
17
+ @name = name
18
+ end
19
+
20
+ def type
21
+ :database_value
22
+ end
23
+
24
+ using Tables::Collection::Type::Helpers::Extensions
25
+
26
+ def value
27
+ if @attribute_type.multiple? && @value.is_a?(Array) && @value.length == 1
28
+ to_param(@value.first)
29
+ else
30
+ to_param(@value)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Katalyst
4
+ module Tables
5
+ module Suggestions
6
+ class SearchValue < Base
7
+ def type
8
+ :search_value
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -2,7 +2,15 @@ en:
2
2
  katalyst:
3
3
  tables:
4
4
  query:
5
- placeholder: "Filter %{name}"
5
+ input_component:
6
+ placeholder: "Filter %{name}"
7
+ modal_component:
8
+ suggestions_title: "Search options"
6
9
  orderable:
7
10
  value:
8
11
  "⠿"
12
+ errors:
13
+ attributes:
14
+ query:
15
+ unknown_key: "The field '%{input}' isn’t searchable here. Please check your input."
16
+ no_untagged_search: "'%{input}' isn't searchable here. Please choose a field to search."
@@ -22,8 +22,16 @@ module Katalyst
22
22
  end
23
23
  end
24
24
 
25
- def examples_for(...)
26
- [example(true), example(false)]
25
+ def suggestions(scope, attribute)
26
+ _, model, column = model_and_column_for(scope, attribute)
27
+
28
+ values = %w[true false]
29
+
30
+ if attribute.value_before_type_cast.present?
31
+ values = values.select { |value| value.include?(attribute.value_before_type_cast) }
32
+ end
33
+
34
+ values.map { |v| constant_suggestion(attribute:, model:, column:, value: deserialize(v)) }
27
35
  end
28
36
  end
29
37
  end
@@ -22,13 +22,15 @@ module Katalyst
22
22
  end
23
23
  end
24
24
 
25
- def examples_for(scope, attribute)
25
+ def suggestions(scope, attribute)
26
+ _, model, column = model_and_column_for(scope, attribute)
27
+
26
28
  [
27
29
  *super(scope, attribute, limit: 6, order: :desc),
28
- example(::Date.current.beginning_of_week.., "this week"),
29
- example(::Date.current.beginning_of_month.., "this month"),
30
- example(1.month.ago.all_month, "last month"),
31
- example(1.year.ago.all_year, "last year"),
30
+ database_suggestion(attribute:, model:, column:, value: ::Date.current.beginning_of_week..),
31
+ database_suggestion(attribute:, model:, column:, value: ::Date.current.beginning_of_month..),
32
+ database_suggestion(attribute:, model:, column:, value: 1.month.ago.all_month),
33
+ database_suggestion(attribute:, model:, column:, value: 1.year.ago.all_year),
32
34
  ]
33
35
  end
34
36
 
@@ -15,18 +15,18 @@ module Katalyst
15
15
  :enum
16
16
  end
17
17
 
18
- def examples_for(scope, attribute)
18
+ def suggestions(scope, attribute)
19
19
  _, model, column = model_and_column_for(scope, attribute)
20
20
 
21
21
  raise ArgumentError, "Unknown enum #{column} for #{model}" unless model.defined_enums.has_key?(column)
22
22
 
23
- keys = model.defined_enums[column].keys
23
+ values = model.defined_enums[column].keys
24
24
 
25
25
  if attribute.value_before_type_cast.present?
26
- keys = keys.select { |key| key.include?(attribute.value_before_type_cast.last) }
26
+ values = values.select { |key| key.include?(attribute.value_before_type_cast) }
27
27
  end
28
28
 
29
- keys.map { |key| example(key, describe_key(model, attribute, key)) }
29
+ values.map { |value| constant_suggestion(attribute:, model:, column:, value:) }
30
30
  end
31
31
 
32
32
  private
@@ -34,23 +34,6 @@ module Katalyst
34
34
  def cast_value(value)
35
35
  value.to_s
36
36
  end
37
-
38
- def describe_key(model, attribute, key)
39
- label = model.human_attribute_name(attribute.name).downcase
40
- value = model.human_attribute_name("#{attribute.name}.#{key}").downcase
41
-
42
- description = "#{model.model_name.human} #{label} is #{value}"
43
- description += " (default)" if default?(attribute, key)
44
- description
45
- end
46
-
47
- def default?(attribute, value)
48
- if multiple?
49
- attribute.original_value&.intersection(cast(value))&.any?
50
- else
51
- attribute.default_value.eql?(cast(value))
52
- end
53
- end
54
37
  end
55
38
  end
56
39
  end
@@ -20,20 +20,10 @@ module Katalyst
20
20
  false
21
21
  end
22
22
 
23
- def examples_for(...)
23
+ def suggestions(...)
24
24
  []
25
25
  end
26
26
  end
27
-
28
- refine(::ActiveModel::Attribute) do
29
- def query_range=(range)
30
- @query_range = range
31
- end
32
-
33
- def query_range
34
- @query_range
35
- end
36
- end
37
27
  end
38
28
  end
39
29
  end
@@ -30,9 +30,7 @@ module Katalyst
30
30
  end
31
31
  end
32
32
 
33
- def filter(scope, attribute)
34
- value = filter_value(attribute)
35
-
33
+ def filter(scope, attribute, value: filter_value(attribute))
36
34
  return scope unless filter?(attribute, value)
37
35
 
38
36
  scope, model, column = model_and_column_for(scope, attribute)
@@ -45,29 +43,33 @@ module Katalyst
45
43
  serialize(value)
46
44
  end
47
45
 
48
- def examples_for(scope, attribute, limit: 10, order: :asc)
46
+ def suggestions(scope, attribute, limit: 10, order: :asc)
49
47
  scope, model, column = model_and_column_for(scope, attribute)
50
48
 
51
49
  unless model.attribute_types.has_key?(column)
52
50
  raise(ArgumentError, "Unknown column '#{column}' for #{model}. " \
53
- "Consider defining '#{attribute.name.parameterize.underscore}_examples'")
51
+ "Consider defining '#{attribute.name.parameterize.underscore}_suggestions'")
54
52
  end
55
53
 
56
- column = model.arel_table[column]
54
+ arel_column = model.arel_table[column]
57
55
 
58
56
  filter(scope, attribute)
59
- .group(column)
57
+ .group(arel_column)
60
58
  .distinct
61
59
  .limit(limit)
62
- .reorder(column => order)
63
- .pluck(column)
64
- .map { |v| example(deserialize(v)) }
60
+ .reorder(arel_column => order)
61
+ .pluck(arel_column)
62
+ .map { |v| database_suggestion(attribute:, model:, column:, value: deserialize(v)) }
65
63
  end
66
64
 
67
65
  private
68
66
 
69
- def example(value, description = "")
70
- Example.new(to_param(value), description)
67
+ def constant_suggestion(attribute:, model:, column:, value:)
68
+ Tables::Suggestions::ConstantValue.new(name: attribute.name, type: attribute.type, model:, column:, value:)
69
+ end
70
+
71
+ def database_suggestion(attribute:, model:, column:, value:)
72
+ Tables::Suggestions::DatabaseValue.new(name: attribute.name, type: attribute.type, model:, column:, value:)
71
73
  end
72
74
 
73
75
  def filter_value(attribute)
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.4.6
4
+ version: 3.5.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: 2024-07-15 00:00:00.000000000 Z
11
+ date: 2024-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: katalyst-html-attributes
@@ -101,6 +101,8 @@ files:
101
101
  - app/components/katalyst/tables/query/input_component.rb
102
102
  - app/components/katalyst/tables/query/modal_component.html.erb
103
103
  - app/components/katalyst/tables/query/modal_component.rb
104
+ - app/components/katalyst/tables/query/suggestion_component.html.erb
105
+ - app/components/katalyst/tables/query/suggestion_component.rb
104
106
  - app/components/katalyst/tables/query_component.html.erb
105
107
  - app/components/katalyst/tables/query_component.rb
106
108
  - app/components/katalyst/tables/selectable/form_component.html.erb
@@ -130,12 +132,20 @@ files:
130
132
  - app/models/concerns/katalyst/tables/collection/query/array_value_parser.rb
131
133
  - app/models/concerns/katalyst/tables/collection/query/parser.rb
132
134
  - app/models/concerns/katalyst/tables/collection/query/single_value_parser.rb
135
+ - app/models/concerns/katalyst/tables/collection/query/untagged_literal.rb
133
136
  - app/models/concerns/katalyst/tables/collection/query/value_parser.rb
134
137
  - app/models/concerns/katalyst/tables/collection/reducers.rb
135
138
  - app/models/concerns/katalyst/tables/collection/sorting.rb
139
+ - app/models/concerns/katalyst/tables/collection/suggestions.rb
136
140
  - app/models/katalyst/tables/collection/array.rb
137
141
  - app/models/katalyst/tables/collection/base.rb
138
142
  - app/models/katalyst/tables/collection/filter.rb
143
+ - app/models/katalyst/tables/suggestions/attribute.rb
144
+ - app/models/katalyst/tables/suggestions/base.rb
145
+ - app/models/katalyst/tables/suggestions/constant_value.rb
146
+ - app/models/katalyst/tables/suggestions/custom_value.rb
147
+ - app/models/katalyst/tables/suggestions/database_value.rb
148
+ - app/models/katalyst/tables/suggestions/search_value.rb
139
149
  - config/importmap.rb
140
150
  - config/locales/tables.en.yml
141
151
  - lib/katalyst/tables.rb
@@ -144,7 +154,6 @@ files:
144
154
  - lib/katalyst/tables/collection/type/boolean.rb
145
155
  - lib/katalyst/tables/collection/type/date.rb
146
156
  - lib/katalyst/tables/collection/type/enum.rb
147
- - lib/katalyst/tables/collection/type/example.rb
148
157
  - lib/katalyst/tables/collection/type/float.rb
149
158
  - lib/katalyst/tables/collection/type/helpers/delegate.rb
150
159
  - lib/katalyst/tables/collection/type/helpers/extensions.rb
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Katalyst
4
- module Tables
5
- module Collection
6
- module Type
7
- class Example
8
- attr_reader :value, :description
9
-
10
- def initialize(value, description = "")
11
- @value = value
12
- @description = description
13
- end
14
-
15
- def hash
16
- value.hash
17
- end
18
-
19
- def eql?(other)
20
- value.eql?(other.value)
21
- end
22
-
23
- def to_s
24
- value.to_s
25
- end
26
- end
27
- end
28
- end
29
- end
30
- end