katalyst-tables 3.14.0 → 3.15.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 544768f007ab3f12b3cbbba816479f12d3592eb31bf36789097edf3208c88517
4
- data.tar.gz: 47d084766c55f247991c78aa86d83348e5f28db9f189510aba144fd7c7e5158d
3
+ metadata.gz: 6d3667ae86fae4850ddda5e73db980cf7ae5250514ea43820f9c45f7fdfe54d0
4
+ data.tar.gz: 8223000b7268a6f87ad74b82225eab728c3d7cf1e52871cd28f062fc6976288b
5
5
  SHA512:
6
- metadata.gz: 749e2a3c9e39822f2349e16a15e58aa71b248ebd72fcd7f9b5f996acba68cad767d0b92432a43f5c942576a617a0c506798a289d2ff5a57898f204cc0685280d
7
- data.tar.gz: 7c607a4c47c772835fde2ded77c383d9cbdb3a3245d401dfc406ab94b52963cb1976af773449a4db24acf55848dbe73b81d9836bd694ddb617f083a28bb80514
6
+ metadata.gz: 97b5d21f99d9178f64c9918467d423a64134bc23e195ce515ddf6346f670565b95c8540edde79e75fbd05946e6cb75646291a553f28806abee4b66eed65fdec5
7
+ data.tar.gz: 2d09f88cfbb4e10b28dab7b8a16123c2a6304030ff32d55165c5c0f41a1b683b6a019fe3ff97e92e74a7936422dbb5119a1620a2ccbe5d184749b67824e5aa26
data/CHANGELOG.md CHANGED
@@ -1,3 +1,21 @@
1
+ ## [3.15.0]
2
+
3
+ * Date filters on datetime columns now match whole days in `Time.zone`, including the last day of a range (#169)
4
+ * Collection inputs containing NUL bytes are treated as invalid and match nothing, instead of raising database errors (#171)
5
+ * Applies to all collection types; values in `multiple` attributes are ignored individually
6
+ * Invalid input now matches nothing for attributes with a custom `scope:`, instead of calling the scope with `nil`
7
+ * Integer filters accept bigint values and leave range checks to the database column, instead of raising `ActiveModel::RangeError` for values beyond 4 bytes
8
+ * Out of range values match nothing, out of range upper bounds are unbounded, and out of range `multiple` values are ignored
9
+ * Values longer than 16 digits are no longer truncated when cast
10
+ * Float filters treat `NaN` as invalid input and match nothing
11
+ * Blank and invalid values in `multiple` integer, float, boolean and enum filters are ignored, instead of also matching rows where the column is `NULL`
12
+ * e.g. the hidden blank value submitted by Rails multiple selects
13
+ * Only blank values (e.g. `[""]`, or `category:[]` in a query) do not filter, and are not reported as filters
14
+ * Enum filters match nothing for unknown values, instead of matching rows where the column is `NULL`
15
+ * Labels and database values are both accepted, as in Active Record
16
+ * Unknown values alongside known values are ignored
17
+ * Attributes with a custom `scope:`, or on columns without a Rails enum, are unchanged
18
+
1
19
  ## [3.14.0]
2
20
 
3
21
  * Support for ordering collections sorted `ordinal desc`
@@ -16,7 +16,7 @@ module Katalyst
16
16
  return false unless filterable?
17
17
 
18
18
  if attribute.came_from_user?
19
- attribute.value_before_type_cast.present? || value === false
19
+ !blank_input?(attribute.value_before_type_cast)
20
20
  else
21
21
  !value.nil? && !value.eql?([])
22
22
  end
@@ -36,6 +36,26 @@ module Katalyst
36
36
 
37
37
  private
38
38
 
39
+ # Dates filtering a datetime column are interpreted as whole days in Time.zone.
40
+ def apply_filter(scope, model, attribute, value)
41
+ return super unless value && datetime_column?(model, attribute.name.split(".").last)
42
+
43
+ scope.where(attribute.name => to_time_range(value))
44
+ end
45
+
46
+ def datetime_column?(model, column)
47
+ %i[datetime timestamptz].include?(model.type_for_attribute(column).type)
48
+ end
49
+
50
+ # Converts a date or range of dates to an exclusive range of times that
51
+ # covers each day in full, from the start of the first day to the start
52
+ # of the day after the last.
53
+ def to_time_range(value)
54
+ from, to = value.is_a?(::Range) ? [value.begin, value.end] : [value, value]
55
+
56
+ from&.in_time_zone...to&.next_day&.in_time_zone
57
+ end
58
+
39
59
  def cast_value(value)
40
60
  return value unless value.is_a?(::String)
41
61
 
@@ -39,8 +39,29 @@ module Katalyst
39
39
 
40
40
  private
41
41
 
42
+ # Blank values are removed, as for other multiple value types.
42
43
  def cast_value(value)
43
- value.to_s
44
+ value.to_s.presence
45
+ end
46
+
47
+ # Active Record filters unknown enum values on IS NULL. Unknown values
48
+ # can't match any row, so remove them, and match nothing if none remain.
49
+ def apply_filter(scope, model, attribute, value)
50
+ mapping = model.defined_enums[attribute.name.split(".").last]
51
+
52
+ # not a Rails enum, leave it to the database
53
+ return super if mapping.nil?
54
+
55
+ known = known_values(mapping, Array(value))
56
+
57
+ known.any? ? super(scope, model, attribute, known) : scope.none
58
+ end
59
+
60
+ # Accepts labels and database values, matching Active Record.
61
+ def known_values(mapping, values)
62
+ database_values = mapping.values.map(&:to_s)
63
+
64
+ values.select { |v| mapping.key?(v) || database_values.include?(v) }
44
65
  end
45
66
  end
46
67
  end
@@ -14,6 +14,15 @@ module Katalyst
14
14
  def initialize(**)
15
15
  super(**, delegate: ActiveModel::Type::Float)
16
16
  end
17
+
18
+ private
19
+
20
+ # ActiveModel accepts "NaN", but NaN is not equal to anything, so
21
+ # treat it as invalid input.
22
+ def cast_value(value)
23
+ result = super
24
+ result unless result.is_a?(::Float) && result.nan?
25
+ end
17
26
  end
18
27
  end
19
28
  end
@@ -67,6 +67,8 @@ module Katalyst
67
67
  value.grep_v(::Range)
68
68
  when ::Range
69
69
  value
70
+ when nil
71
+ []
70
72
  else
71
73
  [value]
72
74
  end
@@ -12,7 +12,17 @@ module Katalyst
12
12
  define_range_patterns(/-?\d+/)
13
13
 
14
14
  def initialize(**)
15
- super(**, delegate: ActiveModel::Type::Integer)
15
+ super(**, delegate: Unbounded)
16
+ end
17
+
18
+ # Integer values are checked against the column's range by Active
19
+ # Record when filtering, which matches nothing for out of range values.
20
+ # BigInteger skips ActiveModel's own 4 byte range check, and limit: 8
21
+ # avoids truncating bigint values when casting from strings.
22
+ class Unbounded < ActiveModel::Type::BigInteger
23
+ def initialize(**)
24
+ super(limit: 8, **)
25
+ end
16
26
  end
17
27
  end
18
28
  end
@@ -24,7 +24,7 @@ module Katalyst
24
24
  return false unless filterable?
25
25
 
26
26
  if attribute.came_from_user?
27
- attribute.value_before_type_cast.present?
27
+ !blank_input?(attribute.value_before_type_cast)
28
28
  else
29
29
  value.present?
30
30
  end
@@ -33,6 +33,11 @@ module Katalyst
33
33
  def filter(scope, attribute, value: filter_value(attribute))
34
34
  return scope unless filter?(attribute, value)
35
35
 
36
+ value = without_null_bytes(value)
37
+
38
+ # user input was supplied but it can't match anything
39
+ return scope.none if value.nil?
40
+
36
41
  if self.scope.present?
37
42
  scope.public_send(self.scope, value)
38
43
  elsif attribute.name.include?(".")
@@ -88,6 +93,34 @@ module Katalyst
88
93
  Tables::Suggestions::DatabaseValue.new(name: attribute.name, type: attribute.type, value:)
89
94
  end
90
95
 
96
+ # Strings containing NUL bytes can't be sent to the database: Postgres
97
+ # raises ArgumentError when quoting and SQLite truncates the literal.
98
+ # No row can match them, so they are removed before filtering.
99
+ def without_null_bytes(value)
100
+ case value
101
+ when ::String
102
+ value unless value.include?("\0")
103
+ when ::Array
104
+ kept = value.reject { |v| v.is_a?(::String) && v.include?("\0") }
105
+ kept unless kept.empty? && value.any?
106
+ else
107
+ value
108
+ end
109
+ end
110
+
111
+ # Blank input means nothing was chosen, e.g. the hidden blank value that
112
+ # Rails multiple selects submit. False is input for boolean filters.
113
+ def blank_input?(value)
114
+ case value
115
+ when ::Array
116
+ value.all? { |v| blank_input?(v) }
117
+ when ::String
118
+ value.blank?
119
+ else
120
+ value.nil?
121
+ end
122
+ end
123
+
91
124
  def filter_value(attribute)
92
125
  attribute.value
93
126
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katalyst-tables
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.14.0
4
+ version: 3.15.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive