katalyst-tables 3.4.1 → 3.4.3

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: f5923255962191599c9ddf2eef5a5fa1dee03b8b1513336d0f2e849ebe5748ea
4
- data.tar.gz: d169ccb2f68d196c1c0472070e6ef4f57aabb1d93189acadbc253ec860d05b76
3
+ metadata.gz: 3759aa46548e98b88f6528c449ae5daff86785c9511a4637bbb17c692b3c7c04
4
+ data.tar.gz: 3982b2749ec6c19b4a7deb279ebc78ef8415c04be64d5949d9730d55b0999474
5
5
  SHA512:
6
- metadata.gz: 3437fad480724057194bb31a26508d19e139ee1b118a3896c4abc2181da3a2d6d45e62d492c84304b30abb2c4917a0e805c4be04d1a0feac753546733962d84c
7
- data.tar.gz: 542774d195ca0b1a1818b96c162f57dda6a0ac6feb3b8620ee76cfa9aee3f018c20b1120ce4e671c1069783ff5a72827b119a53af0feda6d361ba2fd787f072d
6
+ metadata.gz: 550c46bcbfec02a42c019a6f52c9339f1fb0e0046ad1368c1c2651201fc7179fb92b9f40bbb2176be27e23fff49cd7910276ea44ce19338cc8500470cb97a3cd
7
+ data.tar.gz: 7d56c07e0755a26d05a9b8dbe38b33f649a185ac4491961dc0130e29e5a2478860e9fb4625ddbf2844106297f13d2989e23f1d7f51a4a49e5e6d4bd9ddf8f52e
@@ -72,8 +72,9 @@
72
72
  dl {
73
73
  display: grid;
74
74
  grid-auto-flow: row;
75
- grid-auto-rows: 1fr;
76
- grid-template-columns: 1fr 4fr;
75
+ grid-auto-rows: auto;
76
+ grid-template-columns: auto 1fr;
77
+ gap: 0 1rem;
77
78
  }
78
79
 
79
80
  dt {
@@ -85,11 +86,6 @@
85
86
  color: #666;
86
87
  }
87
88
 
88
- ul {
89
- list-style: none;
90
- padding-left: 0;
91
- }
92
-
93
89
  .error {
94
90
  background: #fff2f2;
95
91
  border-bottom: 1px solid #fdd3d3;
@@ -9,6 +9,7 @@ $row-border-color: $grey !default;
9
9
  $row-height: 48px !default;
10
10
  $cell-spacing: 0.5rem !default;
11
11
  $tag-color: $grey !default;
12
+ $on-tag-color: black !default;
12
13
 
13
14
  $width-small: 6rem !default;
14
15
  $width-medium: 12rem !default;
@@ -20,6 +21,7 @@ table {
20
21
  --table-header-color: #{$table-header-color};
21
22
  --row-border-color: #{$row-border-color};
22
23
  --tag-color: #{$tag-color};
24
+ --on-tag-color: #{$on-tag-color};
23
25
 
24
26
  --width-small: #{$width-small};
25
27
  --width-medium: #{$width-medium};
@@ -2,8 +2,14 @@
2
2
  width: var(--width-small);
3
3
  }
4
4
 
5
- :where(td.type-enum small) {
6
- background: var(--tag-color);
7
- border-radius: 0.25rem;
8
- padding: 0.25rem 0.5rem;
5
+ :where(td.type-enum) {
6
+ // ensure ellipsis is the correct color
7
+ color: var(--on-tag-color);
8
+
9
+ small {
10
+ background: var(--tag-color);
11
+ border-radius: 0.25rem;
12
+ padding: 0.25rem 0.5rem;
13
+ font-size: small;
14
+ }
9
15
  }
@@ -8,13 +8,14 @@
8
8
  </header>
9
9
  <% end %>
10
10
  <div class="content">
11
- <% if show_values? %>
11
+ <% if show_examples? %>
12
12
  <h4>Possible values for <code><%= current_key %>:</code></h4>
13
- <ul>
14
- <% values_for(current_key).each do |value| %>
15
- <li><code><%= format_value(value) %></code></li>
13
+ <dl>
14
+ <% examples_for(current_key).each do |example| %>
15
+ <dt><code><%= format_value(example.value) %></code></dt>
16
+ <% if example.description.present? %><dd><%= example.description %></dd><% end %>
16
17
  <% end %>
17
- </ul>
18
+ </dl>
18
19
  <% else %>
19
20
  <h4>Available filters:</h4>
20
21
  <dl>
@@ -31,7 +31,7 @@ module Katalyst
31
31
 
32
32
  using Collection::Type::Helpers::Extensions
33
33
 
34
- def show_values?
34
+ def show_examples?
35
35
  current_key && attributes[current_key]
36
36
  end
37
37
 
@@ -63,13 +63,13 @@ module Katalyst
63
63
  end
64
64
  end
65
65
 
66
- def values_for(key)
67
- collection.examples_for(key).map(&:to_s).compact_blank
66
+ def examples_for(key)
67
+ collection.examples_for(key)&.reject { |e| e.value.to_s.blank? } || []
68
68
  end
69
69
 
70
70
  def format_value(value)
71
- if /\A[\w.-]*\z/.match?(value)
72
- value
71
+ if /\A[\w.-]*\z/.match?(value.to_s)
72
+ value.to_s
73
73
  else
74
74
  %("#{value}")
75
75
  end
@@ -32,7 +32,7 @@ module Katalyst
32
32
  key = key.to_s
33
33
  examples_method = "#{key.parameterize.underscore}_examples"
34
34
  if respond_to?(examples_method)
35
- public_send(examples_method)
35
+ public_send(examples_method)&.map { |e| e.is_a?(Example) ? e : Example.new(example) }
36
36
  elsif @attributes.key?(key)
37
37
  @attributes[key].type.examples_for(unscoped_items, @attributes[key])
38
38
  end
@@ -23,7 +23,7 @@ module Katalyst
23
23
  end
24
24
 
25
25
  def examples_for(...)
26
- [true, false]
26
+ [example(true), example(false)]
27
27
  end
28
28
  end
29
29
  end
@@ -13,22 +13,23 @@ module Katalyst
13
13
  :date
14
14
  end
15
15
 
16
- def serialize(value)
17
- if value.is_a?(::Date)
18
- value.to_fs(:db)
16
+ def to_param(value)
17
+ case value
18
+ when ::Date, ::DateTime, ::Time, ActiveSupport::TimeWithZone
19
+ value.to_date.to_fs(:db)
19
20
  else
20
21
  super
21
22
  end
22
23
  end
23
24
 
24
- def examples_for(...)
25
+ def examples_for(scope, attribute)
25
26
  [
26
- ::Date.current,
27
- ::Date.yesterday,
28
- ::Date.current.beginning_of_week..,
29
- ::Date.current.beginning_of_month..,
30
- ::Date.current.beginning_of_year..,
31
- ].map { |d| serialize(d) }
27
+ *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"),
32
+ ]
32
33
  end
33
34
 
34
35
  private
@@ -20,10 +20,17 @@ module Katalyst
20
20
  keys = model.defined_enums[column]&.keys
21
21
 
22
22
  if attribute.value_before_type_cast.present?
23
- keys.select { |key| key.include?(attribute.value_before_type_cast.last) }
24
- else
25
- keys
23
+ keys = keys.select { |key| key.include?(attribute.value_before_type_cast.last) }
26
24
  end
25
+
26
+ keys.map { |key| example(key, describe_key(model, attribute.name, key)) }
27
+ end
28
+
29
+ private
30
+
31
+ def describe_key(model, attribute, key)
32
+ key = I18n.t("active_record.attributes.#{model.model_name.i18n_key}/#{key}", default: key.to_s.titleize)
33
+ "#{model.model_name.human} #{model.human_attribute_name(attribute).downcase} is #{key}"
27
34
  end
28
35
  end
29
36
  end
@@ -0,0 +1,30 @@
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
@@ -21,9 +21,9 @@ module Katalyst
21
21
  return (multiple? ? [] : nil) if value.nil?
22
22
 
23
23
  if multiple? && value.is_a?(::Array)
24
- value.map { |v| super(v) }
24
+ value_for_multiple(value.flat_map { |v| cast(v) })
25
25
  elsif multiple?
26
- [super]
26
+ value_for_multiple(super)
27
27
  else
28
28
  super
29
29
  end
@@ -31,7 +31,7 @@ module Katalyst
31
31
 
32
32
  def deserialize(value)
33
33
  if multiple? && value.is_a?(::Array)
34
- value.map { |v| super(v) }.flatten
34
+ value.map { |v| deserialize(v) }.flatten
35
35
  elsif multiple?
36
36
  [super].flatten.compact
37
37
  else
@@ -41,7 +41,15 @@ module Katalyst
41
41
 
42
42
  def serialize(value)
43
43
  if multiple? && value.is_a?(::Array)
44
- value.map { |v| super(v) }.flatten
44
+ value.map { |v| serialize(v) }.flatten
45
+ else
46
+ super
47
+ end
48
+ end
49
+
50
+ def to_param(value)
51
+ if multiple? && value.is_a?(::Array)
52
+ "[#{value.map { |v| to_param(v) }.flatten.join(', ')}]"
45
53
  else
46
54
  super
47
55
  end
@@ -52,6 +60,17 @@ module Katalyst
52
60
  def default_value
53
61
  multiple? ? [] : super
54
62
  end
63
+
64
+ def value_for_multiple(value)
65
+ case value
66
+ when ::Array
67
+ value.reject { |v| v.is_a?(::Range) }
68
+ when ::Range
69
+ value
70
+ else
71
+ [value]
72
+ end
73
+ end
55
74
  end
56
75
  end
57
76
  end
@@ -19,14 +19,42 @@ module Katalyst
19
19
  end
20
20
  end
21
21
 
22
+ def deserialize(value)
23
+ if value.is_a?(::Range)
24
+ if value.begin.nil?
25
+ make_range(nil, deserialize(value.end))
26
+ elsif value.end.nil?
27
+ make_range(deserialize(value.begin), nil)
28
+ else
29
+ make_range(deserialize(value.begin), deserialize(value.end))
30
+ end
31
+ else
32
+ super
33
+ end
34
+ end
35
+
22
36
  def serialize(value)
23
37
  if value.is_a?(::Range)
24
38
  if value.begin.nil?
25
- "..#{serialize(value.end)}"
39
+ make_range(nil, serialize(value.end))
26
40
  elsif value.end.nil?
27
- "#{serialize(value.begin)}.."
41
+ make_range(serialize(value.begin), nil)
28
42
  else
29
- "#{serialize(value.begin)}..#{serialize(value.end)}"
43
+ make_range(serialize(value.begin), serialize(value.end))
44
+ end
45
+ else
46
+ super
47
+ end
48
+ end
49
+
50
+ def to_param(value)
51
+ if value.is_a?(::Range)
52
+ if value.begin.nil?
53
+ "..#{to_param(value.end)}"
54
+ elsif value.end.nil?
55
+ "#{to_param(value.begin)}.."
56
+ else
57
+ "#{to_param(value.begin)}..#{to_param(value.end)}"
30
58
  end
31
59
  else
32
60
  super
@@ -42,15 +70,25 @@ module Katalyst
42
70
  when self.class.const_get(:SINGLE_VALUE)
43
71
  super($~[:value])
44
72
  when self.class.const_get(:LOWER_BOUND)
45
- ((super($~[:lower]))..)
73
+ make_range(super($~[:lower]), nil)
46
74
  when self.class.const_get(:UPPER_BOUND)
47
- (..(super($~[:upper])))
75
+ make_range(nil, super($~[:upper]))
48
76
  when self.class.const_get(:BOUNDED)
49
- ((super($~[:lower]))..(super($~[:upper])))
77
+ make_range(super($~[:lower]), super($~[:upper]))
50
78
  else
51
79
  super
52
80
  end
53
81
  end
82
+
83
+ private
84
+
85
+ def make_range(from, to)
86
+ # when a value that accepts multiple is given a range, it double-packs the value so we get ..[0]
87
+ # unpack the array value
88
+ from = from.first if from.is_a?(::Array) && from.length == 1
89
+ to = to.first if to.is_a?(::Array) && to.length == 1
90
+ (from..to)
91
+ end
54
92
  end
55
93
  end
56
94
  end
@@ -41,7 +41,11 @@ module Katalyst
41
41
  scope.merge(condition)
42
42
  end
43
43
 
44
- def examples_for(scope, attribute)
44
+ def to_param(value)
45
+ serialize(value)
46
+ end
47
+
48
+ def examples_for(scope, attribute, limit: 10, order: :asc)
45
49
  scope, model, column = model_and_column_for(scope, attribute)
46
50
 
47
51
  return unless model.attribute_types.has_key?(column)
@@ -51,14 +55,18 @@ module Katalyst
51
55
  filter(scope, attribute)
52
56
  .group(column)
53
57
  .distinct
54
- .limit(10)
55
- .reorder(column => :asc)
58
+ .limit(limit)
59
+ .reorder(column => order)
56
60
  .pluck(column)
57
- .map { |v| serialize(v) }
61
+ .map { |v| example(deserialize(v)) }
58
62
  end
59
63
 
60
64
  private
61
65
 
66
+ def example(value, description = "")
67
+ Example.new(to_param(value), description)
68
+ end
69
+
62
70
  def filter_value(attribute)
63
71
  attribute.value
64
72
  end
@@ -69,7 +77,7 @@ module Katalyst
69
77
  elsif scope
70
78
  model.public_send(scope, value)
71
79
  else
72
- model.where(column => value)
80
+ model.where(column => serialize(value))
73
81
  end
74
82
  end
75
83
 
@@ -7,6 +7,7 @@ require "katalyst/tables/collection/type/helpers/extensions"
7
7
  require "katalyst/tables/collection/type/helpers/multiple"
8
8
  require "katalyst/tables/collection/type/helpers/range"
9
9
 
10
+ require "katalyst/tables/collection/type/example"
10
11
  require "katalyst/tables/collection/type/value"
11
12
 
12
13
  require "katalyst/tables/collection/type/boolean"
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.1
4
+ version: 3.4.3
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-02 00:00:00.000000000 Z
11
+ date: 2024-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: katalyst-html-attributes
@@ -144,6 +144,7 @@ files:
144
144
  - lib/katalyst/tables/collection/type/boolean.rb
145
145
  - lib/katalyst/tables/collection/type/date.rb
146
146
  - lib/katalyst/tables/collection/type/enum.rb
147
+ - lib/katalyst/tables/collection/type/example.rb
147
148
  - lib/katalyst/tables/collection/type/float.rb
148
149
  - lib/katalyst/tables/collection/type/helpers/delegate.rb
149
150
  - lib/katalyst/tables/collection/type/helpers/extensions.rb