formatic 0.9.1 → 1.0.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: dc6207b98bbdcf93dcae103ca03eebc2993c68503b4eca6911e93d57749e26e0
4
- data.tar.gz: dbc8e45a7d99b106a56dd3f40a024da2baf3df46fdb11c9cffed0dd797a1efe5
3
+ metadata.gz: 4a1a431fa0b0ef1361c7365edbe44cb75f66493be99b7622b3ca342285d2ffe5
4
+ data.tar.gz: c2996fe6668ee146751ec2b609c38883d3252297e7b4fe17638ae4e61dec7c0a
5
5
  SHA512:
6
- metadata.gz: cc7e35488bd7a377d21b2482cb8460bcee495390ba89bd96c6b60d56a13f739c3a550263151d572b7c765bab5e9b980a399e17b479cdb3946d1ac6f638ad1976
7
- data.tar.gz: 963a4fb447e3b5c1c3a7b7385f825de975ae5d8cb13f83c5cbf291609313bc6a2f9bc2076860897a617edb71b9304a08b52e8275b914e9e010e69b5213780dc0
6
+ metadata.gz: '09a6c6c0d8d0719c3707d804de4b1c1d429ebe585cf39b3dd6193747e04a07b7f6b7ef4732708561aba039e858825bcebb808def4fa5f2399ff5ba6f17272f24'
7
+ data.tar.gz: f5c08ea3809ad0c4cd22ffac331f572dbbeb099f1d26c04ee7ec6d20e5900540abd3e51280311dcbcee367f173d8803085f21cc2be77625f735aeb94af67d7df
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [1.0.0] - 2026-11-11
2
+
3
+ - `presenters.for_search` returns a `Formatic::Search::Frontend` carrying id and HTML; picks, hits, and selection all render from that one object
4
+
5
+ - Search picks build entries directly from records (`id` plus mandatory `presenters.for_search`); the `Formatic::Choices`/`for_select` detour is gone from the search path
6
+
7
+ - Rename presenters contract `for_pick` to `for_search`
8
+ - Rename `Formatic::Search::Frontend` option `choice_html:` to `html:`
9
+ - Remove `Formatic::Search::Frontend` option `selected_html:` (selected card always renders `html`)
10
+
11
+ ## [0.9.2] - 2026-11-11
12
+
13
+ - Move the `formatic-search-hook__quickpicks` hook onto the list grid element, so layout overrides take effect
14
+
1
15
  ## [0.9.1] - 2026-11-11
2
16
 
3
17
  - Search list hooks carry an automatic `--route_key` modifier per model
data/README.md CHANGED
@@ -12,16 +12,16 @@ Opinionated ViewComponents for building forms.
12
12
  ### Search fields
13
13
 
14
14
  `Formatic::Search` renders a hidden value input and a query input for an endpoint
15
- that returns JSON in the form `{ records: [...] }`. Each record has an `id`, a
16
- `label`, `html_for_autocomplete`, and `html_for_input` properties. The
17
- `html_for_autocomplete` markup must include one `.js-formatic-search__choice`
18
- element per record, in the same order as `records`. `html_for_input` provides
19
- the selected-value markup. Search waits 400 ms after the last input. A newer
15
+ that returns JSON in the form `{ records: [...] }`. Each record carries an `id`
16
+ plus `choice` and `selected` markup, usually built with
17
+ `Formatic::Search::Frontend`. Records can also live on the page itself as
18
+ quick-pick lists: pass `favorites:`, `recents:`, or `recommended:` with model
19
+ records, and each record renders through its `presenters.for_search`, which
20
+ returns a `Frontend`. Search waits 400 ms after the last input. A newer
20
21
  query aborts the previous request for that field, and a response is shown only
21
- when its query still matches the input. An optional `with_clear` slot clears
22
- the current selection.
23
- An optional `with_add_record` slot stays hidden while searching and appears
24
- after a successful response, even when there are no matches.
22
+ when its query still matches the input. An optional `with_add_record` slot stays
23
+ hidden while searching and appears after a successful response, even when there
24
+ are no matches.
25
25
  The query input uses a field-specific `placeholder:` or `helpers.placeholder`
26
26
  translation when present. Otherwise it combines the translated label with the
27
27
  localized search prompt. English and German prompts are included; other locales
@@ -44,10 +44,11 @@ and a selected search field. Its `as_json` and `to_json` methods return the wrap
44
44
  `Formatic::Search` to render its selected markup, including the usual clear control:
45
45
  the app supplies the rendered record fragments, and Formatic adds the search wrappers.
46
46
 
47
- Styling hooks: the quick-pick lists carry `formatic-search-hook__quickpicks`,
48
- the async hits `formatic-search-hook__results`. Both are stable across
49
- refactors. Each also carries a modifier with the records route key, so one
50
- model can differ from the rest:
47
+ Styling hooks: the quick-pick list carries `formatic-search-hook__quickpicks`,
48
+ the async hits `formatic-search-hook__results`. Both sit on the grid element
49
+ itself, so layout overrides like `display` land where they take effect.
50
+ Both are stable across refactors. Each also carries a modifier with the
51
+ records route key, so one model can differ from the rest:
51
52
 
52
53
  ```css
53
54
  .formatic-search-hook__quickpicks--animals {
@@ -58,9 +59,7 @@ model can differ from the rest:
58
59
  ```
59
60
 
60
61
  ```ruby
61
- frontend = Formatic::Search::Frontend.new(id: commander.id,
62
- choice_html:,
63
- selected_html:)
62
+ frontend = Formatic::Search::Frontend.new(id: commander.id, html:)
64
63
  render Formatic::Search.new(f:, attribute_name: :commander_id,
65
64
  url: admin_commanders_path(format: :json), frontend:)
66
65
  ```
@@ -3,21 +3,18 @@
3
3
  module Formatic
4
4
  class Search
5
5
  # Represents one search result for JSON responses and selected search fields.
6
- # The selected card falls back to the result row when both look the same.
6
+ # The result row and the selected card render the same HTML.
7
7
  class Frontend
8
- attr_reader :id, :choice_html, :selected_html
8
+ extend Dry::Initializer
9
9
 
10
- def initialize(id:, choice_html:, selected_html: nil)
11
- @id = id
12
- @choice_html = choice_html
13
- @selected_html = selected_html || choice_html
14
- end
10
+ option :id
11
+ option :html
15
12
 
16
13
  def as_json(_options = nil)
17
14
  {
18
15
  id:,
19
16
  choice: render_html(choice_component),
20
- selected: render_html(::Formatic::Search::Selected.new.with_content(selected_html))
17
+ selected: render_html(::Formatic::Search::Selected.new.with_content(html))
21
18
  }
22
19
  end
23
20
 
@@ -28,7 +25,7 @@ module Formatic
28
25
  private
29
26
 
30
27
  def choice_component
31
- ::Formatic::Search::Choice.new(record_id: id).with_content(choice_html)
28
+ ::Formatic::Search::Choice.new(record_id: id).with_content(html)
32
29
  end
33
30
 
34
31
  def render_html(component)
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'calls'
4
+
5
+ module Formatic
6
+ class Search
7
+ class Picks
8
+ # Turns records into pick hashes with id, HTML, and checked state.
9
+ class Entries
10
+ include ::Calls
11
+
12
+ param :search
13
+
14
+ def call
15
+ self.records = all_records.uniq { it.id.to_s }
16
+ prepend_current_record
17
+
18
+ records.map { entry_for(it) }
19
+ end
20
+
21
+ private
22
+
23
+ attr_accessor :records
24
+
25
+ # A pick hash carries the Frontend id and HTML plus the checked state.
26
+ def entry_for(record)
27
+ frontend = frontend_for(record)
28
+ { id: frontend.id.to_s, html: frontend.html, checked: frontend.id.to_s == @search.value.to_s }
29
+ end
30
+
31
+ # The Frontend supplies the pick id and the display HTML.
32
+ # Records without `presenters.for_search` cannot appear in picks.
33
+ def frontend_for(record)
34
+ presenters = record.presenters if record.respond_to?(:presenters)
35
+ return presenters.for_search if presenters.respond_to?(:for_search)
36
+
37
+ raise ArgumentError, "#{record.class} must implement `presenters.for_search` for search picks"
38
+ end
39
+
40
+ def prepend_current_record
41
+ current = current_record
42
+ return if current.nil?
43
+ return if records.any? { same_record?(it, current) }
44
+
45
+ self.records = [current] + records
46
+ end
47
+
48
+ def current_record
49
+ @current_record ||= find_current_record
50
+ end
51
+
52
+ def find_current_record
53
+ belongs_to = association
54
+ return unless include_current? && belongs_to && @search.f.object
55
+
56
+ @search.f.object.public_send(belongs_to.name)
57
+ end
58
+
59
+ def association
60
+ model_klass = @search.f&.object&.class
61
+ return false unless model_klass.respond_to?(:reflect_on_all_associations)
62
+
63
+ model_klass.reflect_on_all_associations(:belongs_to)
64
+ .detect { it.foreign_key == @search.attribute_name.to_s }
65
+ end
66
+
67
+ def same_record?(left, right)
68
+ return true if left.equal?(right) || left == right
69
+
70
+ left.respond_to?(:id) && right.respond_to?(:id) && left.id == right.id
71
+ end
72
+
73
+ def include_current?
74
+ @search.include_current != false
75
+ end
76
+
77
+ def all_records
78
+ Array(@search.recents) + Array(@search.favorites) + Array(@search.recommended)
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -11,10 +11,7 @@ module Formatic
11
11
  end
12
12
 
13
13
  def groups
14
- keyed = choice_entries.map { it.merge(group: group_key(it)) }
15
- built = GROUPS.filter_map { |key| build_group(keyed, key) }
16
- assign_dom_ids(built)
17
- built
14
+ @groups ||= build_groups
18
15
  end
19
16
 
20
17
  def flat
@@ -22,7 +19,7 @@ module Formatic
22
19
  end
23
20
 
24
21
  def active_index
25
- groups.index { it[:picks].any? { |pick| pick[:checked] } } || 0
22
+ groups.index { it[:picks].any? { it[:checked] } } || 0
26
23
  end
27
24
 
28
25
  def value_disabled?
@@ -33,84 +30,43 @@ module Formatic
33
30
  flat.detect { it[:checked] }&.fetch(:html, nil)
34
31
  end
35
32
 
36
- def favorites_legend
37
- ::I18n.t('formatic.search.favorites', default: 'Favorites')
38
- end
33
+ private
39
34
 
40
- def recommended_legend
41
- ::I18n.t('formatic.search.recommended', default: 'Recommended')
35
+ def build_groups
36
+ GROUPS.filter_map { build_group(it) }
37
+ .tap { assign_dom_ids(it) }
42
38
  end
43
39
 
44
- def recents_legend
45
- ::I18n.t('formatic.search.recents', default: 'Recents')
40
+ def keyed_entries
41
+ @keyed_entries ||= entries.map { it.merge(group: group_key(it)) }
46
42
  end
47
43
 
48
- private
49
-
50
- def build_group(keyed, key)
51
- picks = keyed.select { it[:group] == key }.map { it.except(:group) }
44
+ def build_group(key)
45
+ picks = keyed_entries.select { it[:group] == key }.map { it.except(:group) }
52
46
  return if picks.empty?
53
47
 
54
48
  { legend: legend_for(key), picks: }
55
49
  end
56
50
 
57
51
  def legend_for(key)
58
- case key
59
- when :recents then recents_legend
60
- when :recommended then recommended_legend
61
- else favorites_legend
62
- end
52
+ ::I18n.t("formatic.search.#{key}", default: key.to_s.capitalize)
63
53
  end
64
54
 
65
55
  def group_key(entry)
66
56
  id = entry[:id].to_s
67
- return :recents if recent_ids.include?(id)
68
- return :favorites if favorite_ids.include?(id)
69
- return :recommended if recommended_ids.include?(id)
57
+ return :recents if ids_for(:recents).include?(id)
58
+ return :favorites if ids_for(:favorites).include?(id)
59
+ return :recommended if ids_for(:recommended).include?(id)
70
60
 
71
61
  GROUPS.detect { @search.public_send(it).present? } || :recents
72
62
  end
73
63
 
74
- def choices
75
- ::Formatic::Choices.call(
76
- f: @search.f,
77
- attribute_name: @search.attribute_name,
78
- options: nil,
79
- records: all_records,
80
- keys: nil,
81
- include_current: @search.include_current,
82
- include_blank: false
83
- )
84
- end
85
-
86
- def recent_ids
87
- Array(@search.recents).map { it.id.to_s }
88
- end
89
-
90
- def favorite_ids
91
- Array(@search.favorites).map { it.id.to_s }
92
- end
93
-
94
- def recommended_ids
95
- Array(@search.recommended).map { it.id.to_s }
96
- end
97
-
98
- def choice_entries
99
- by_id = combined_by_id
100
- choices.uniq { it.last.to_s }.map { |name, id| entry_for(name, id, by_id) }
101
- end
102
-
103
- def entry_for(name, id, by_id)
104
- record = by_id[id.to_s]
105
- { name:, id:, html: pick_html(record, name), checked: id.to_s == @search.value.to_s }
64
+ def ids_for(list)
65
+ Array(@search.public_send(list)).map { it.id.to_s }
106
66
  end
107
67
 
108
- def combined_by_id
109
- all_records.to_h { [it.id.to_s, it] }
110
- end
111
-
112
- def all_records
113
- Array(@search.recents) + Array(@search.favorites) + Array(@search.recommended)
68
+ def entries
69
+ ::Formatic::Search::Picks::Entries.call(@search)
114
70
  end
115
71
 
116
72
  def assign_dom_ids(built)
@@ -122,17 +78,6 @@ module Formatic
122
78
  end
123
79
  end
124
80
  end
125
-
126
- # Rich display comes from `presenters.for_pick` and is trusted HTML.
127
- # The search endpoint reuses the same presenter for `choice_html`.
128
- # Picks and hits look identical as a result.
129
- # Plain records fall back to text.
130
- def pick_html(record, fallback_name)
131
- presenters = record.presenters if record.respond_to?(:presenters)
132
- return presenters.for_pick if presenters.respond_to?(:for_pick)
133
-
134
- ERB::Util.html_escape(fallback_name)
135
- end
136
81
  end
137
82
  end
138
83
  end
@@ -55,6 +55,7 @@ module Formatic
55
55
  require_relative '../../app/components/formatic/select'
56
56
  require_relative '../../app/components/formatic/search'
57
57
  require_relative '../../app/components/formatic/search/picks'
58
+ require_relative '../../app/components/formatic/search/picks/entries'
58
59
  require_relative '../../app/components/formatic/search/clear'
59
60
  require_relative '../../app/components/formatic/search/choice'
60
61
  require_relative '../../app/components/formatic/search/selected'
@@ -23,7 +23,7 @@ module Formatic
23
23
  <div class="c-formatic-search__selected js-formatic-search__selected">
24
24
  <% if frontend %>
25
25
  <%= render ::Formatic::Search::Selected.new(clearable: !readonly)
26
- .with_content(frontend.selected_html) %>
26
+ .with_content(frontend.html) %>
27
27
  <% elsif checked_pick_html %>
28
28
  <%= render ::Formatic::Search::Selected.new(clearable: true)
29
29
  .with_content(checked_pick_html.html_safe) %>
@@ -38,7 +38,7 @@ module Formatic
38
38
  class: 'js-formatic-search__query-input' %>
39
39
  </div>
40
40
 
41
- <div class="c-formatic-search__quickpicks js-formatic-search__quickpicks formatic-search-hook__quickpicks<%= ' formatic-search-hook__quickpicks--' + hook_modifier if hook_modifier %><%= ' is-hidden' if picks_initially_hidden? %>">
41
+ <div class="c-formatic-search__quickpicks js-formatic-search__quickpicks<%= ' is-hidden' if picks_initially_hidden? %>">
42
42
  <% if pick_groups.size > 1 %>
43
43
  <div class="c-formatic-search__quickpicks-tabs js-formatic-search__quickpicks-tablist" role="tablist">
44
44
  <% pick_groups.each_with_index do |group, group_index| %>
@@ -51,7 +51,7 @@ module Formatic
51
51
  </div>
52
52
  <% end %>
53
53
  <% pick_groups.each_with_index do |group, group_index| %>
54
- <fieldset class="c-formatic-search__quickpicks-group js-formatic-search__quickpicks-panel<%= ' is-hidden' if pick_groups.size > 1 && group_index != active_group_index %>"
54
+ <fieldset class="c-formatic-search__quickpicks-group js-formatic-search__quickpicks-panel formatic-search-hook__quickpicks<%= ' formatic-search-hook__quickpicks--' + hook_modifier if hook_modifier %><%= ' is-hidden' if pick_groups.size > 1 && group_index != active_group_index %>"
55
55
  <% if pick_groups.size > 1 %>role="tabpanel" aria-labelledby="<%= query_input_id %>_tab_<%= group_index %>"<% else %>aria-label="<%= group[:legend] %>"<% end %>>
56
56
  <% group[:picks].each do |pick| %>
57
57
  <label class="c-formatic-search__pick js-formatic-search__pick-label" for="<%= pick[:dom_id] %>">
@@ -84,7 +84,7 @@ module Formatic
84
84
  <div class="c-formatic-search__selected js-formatic-search__selected">
85
85
  <% if frontend %>
86
86
  <%= render ::Formatic::Search::Selected.new(clearable: !readonly)
87
- .with_content(frontend.selected_html) %>
87
+ .with_content(frontend.html) %>
88
88
  <% end %>
89
89
  </div>
90
90
 
@@ -62,7 +62,7 @@ module Formatic
62
62
 
63
63
  def search_params(component)
64
64
  {
65
- query: query,
65
+ query:,
66
66
  record_name: configured_value(component, 'record-name'),
67
67
  attribute_name: configured_value(component, 'attribute-name'),
68
68
  exclude_ids: configured_value(component, 'exclude-ids')
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Formatic
4
- VERSION = '0.9.1'
4
+ VERSION = '1.0.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formatic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - halo
@@ -194,6 +194,7 @@ files:
194
194
  - app/components/formatic/search/clear.rb
195
195
  - app/components/formatic/search/frontend.rb
196
196
  - app/components/formatic/search/picks.rb
197
+ - app/components/formatic/search/picks/entries.rb
197
198
  - app/components/formatic/search/selected.rb
198
199
  - app/components/formatic/select.rb
199
200
  - app/components/formatic/stepper.rb
@@ -243,7 +244,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
243
244
  requirements:
244
245
  - - ">="
245
246
  - !ruby/object:Gem::Version
246
- version: 3.4.0
247
+ version: 4.0.0
247
248
  required_rubygems_version: !ruby/object:Gem::Requirement
248
249
  requirements:
249
250
  - - ">="