formatic 0.6.1 → 0.7.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: c770f8b2518ead02b5d2a7797068c3192ed9d264e22d59af830569eb07a6c0ef
4
- data.tar.gz: ffc6912dea00ef5faf99fa815d7d3173d446fb07f312c160a80e6d29f9ec175c
3
+ metadata.gz: 0dfc8f4ebc2732072e4bce05a6bb1e8ca892fa98c3cc7ad51871e3f16468fbbb
4
+ data.tar.gz: 7511ea6cfd3d519a2f03438162b25efc369422e981a4edf51193361928a32f0a
5
5
  SHA512:
6
- metadata.gz: eaf5adeb08637ff84743f4a0c26c694533af908ee64d716340f96478fed4cc2a93004bf73a5bb3c50d8be4b647e703ab7f5d9bafb93cad4e483473c60501760c
7
- data.tar.gz: af465ca562212a64012756431fd96340d2e44c5d68a11ee4ede6a912f2e8d5627b853140b0f759d57d84f90f1d6722aab752496e47a386bd5c56132f7d5d9e0f
6
+ metadata.gz: 50383fb5853a0b588c723776b019e58acd4f7f557316df8c0be2bcf426251bec32d5049f57290e3b125be2ca05b9ad80664e0ab0be437b25f34d6498eeddddc7
7
+ data.tar.gz: cbb323dbde2a7bbf9abfb6d694f133f93d6f81fb7366877a05a98d847a21f7779bcdc9e0f38df1d1d91b74c04335b0107ed2709f0c60530c7baa26e429de7f56
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## [0.7.0] - 2026-10-27
2
+
3
+ - Add an instance-based `Formatic::Search::Frontend` with `as_json` and `to_json`.
4
+ - Pass the frontend object to `Formatic::Search` to render selected records.
5
+ - Remove the class-level `.json` helper and the `with_selected_record` slot.
6
+
1
7
  ## [0.6.1] 2026-10-26
2
8
 
3
9
  - Proper i18n for Formatic::Search placeholders
data/README.md CHANGED
@@ -38,6 +38,20 @@ For Rack tests, call `Formatic::Testing::Search.call(page:, query: 'Bob', from:
38
38
  It reads the field's URL and parameter names, requests matching JSON directly, and
39
39
  fills the hidden input with a result returned by that endpoint.
40
40
 
41
+ Use `Formatic::Search::Frontend` to represent a result for both the JSON response
42
+ and a selected search field. Its `as_json` and `to_json` methods return the wrapped
43
+ `id`, `choice`, and `selected` record. Pass the object to
44
+ `Formatic::Search` to render its selected markup, including the usual clear control:
45
+ the app supplies the rendered record fragments, and Formatic adds the search wrappers.
46
+
47
+ ```ruby
48
+ frontend = Formatic::Search::Frontend.new(id: commander.id,
49
+ choice_html:,
50
+ selected_html:)
51
+ render Formatic::Search.new(f:, attribute_name: :commander_id,
52
+ url: admin_commanders_path(format: :json), frontend:)
53
+ ```
54
+
41
55
  Capybara RackTest test classes can include the `search` helper instead:
42
56
 
43
57
  ```ruby
@@ -2,20 +2,37 @@
2
2
 
3
3
  module Formatic
4
4
  class Search
5
- # Builds one search JSON record from the app partials.
5
+ # Represents one search result for JSON responses and selected search fields.
6
6
  class Frontend
7
- def self.json(id:, choice:, selected:)
7
+ attr_reader :id, :choice_html, :selected_html
8
+
9
+ def initialize(id:, choice_html:, selected_html:)
10
+ @id = id
11
+ @choice_html = choice_html
12
+ @selected_html = selected_html
13
+ end
14
+
15
+ def as_json(_options = nil)
8
16
  {
9
- id: id,
10
- choice: render_html(::Formatic::Search::Choice.new(record_id: id).with_content(choice)),
11
- selected: render_html(::Formatic::Search::Selected.new.with_content(selected))
17
+ id:,
18
+ choice: render_html(choice_component),
19
+ selected: render_html(::Formatic::Search::Selected.new.with_content(selected_html))
12
20
  }
13
21
  end
14
22
 
15
- def self.render_html(component)
23
+ def to_json(options = nil)
24
+ as_json.to_json(options)
25
+ end
26
+
27
+ private
28
+
29
+ def choice_component
30
+ ::Formatic::Search::Choice.new(record_id: id).with_content(choice_html)
31
+ end
32
+
33
+ def render_html(component)
16
34
  ApplicationController.render(component)
17
35
  end
18
- private_class_method :render_html
19
36
  end
20
37
  end
21
38
  end
@@ -4,12 +4,12 @@ module Formatic
4
4
  class Search
5
5
  # The record that is currently chosen.
6
6
  class Selected < ::Formatic::ApplicationComponent
7
- option :readonly, default: -> { false }
7
+ option :clearable, default: -> { true }
8
8
 
9
9
  erb_template <<~ERB
10
10
  <div class="c-formatic-search__selection">
11
11
  <div class="c-formatic-search__selected-record"><%= content %></div>
12
- <% unless readonly %>
12
+ <% if clearable %>
13
13
  <div class="c-formatic-search__actions"><%= render ::Formatic::Search::Clear.new %></div>
14
14
  <% end %>
15
15
  </div>
@@ -8,10 +8,10 @@ module Formatic
8
8
  option :url
9
9
  option :record_name, as: :manual_record_name, optional: true
10
10
  option :exclude_ids, default: -> { [] }
11
+ option :frontend, optional: true
11
12
  option :placeholder, as: :manual_placeholder, optional: true
12
13
  option :prevent_submit_on_enter, default: -> { true }
13
14
 
14
- renders_one :selected_record
15
15
  renders_one :add_record
16
16
 
17
17
  erb_template(::Formatic::Templates::Search.call)
@@ -19,8 +19,9 @@ module Formatic
19
19
  class: 'js-formatic-search__value' %>
20
20
 
21
21
  <div class="c-formatic-search__selected js-formatic-search__selected">
22
- <% if selected_record? %>
23
- <%= render ::Formatic::Search::Selected.new(readonly: readonly).with_content(selected_record) %>
22
+ <% if frontend %>
23
+ <%= render ::Formatic::Search::Selected.new(clearable: !readonly)
24
+ .with_content(frontend.selected_html) %>
24
25
  <% end %>
25
26
  </div>
26
27
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Formatic
4
- VERSION = '0.6.1'
4
+ VERSION = '0.7.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.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - halo