formatic 0.6.0 → 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: d41f61aacdbc9679d1e63ff82441ac6008ae35f53a8b5425986e3bac53baeb0d
4
- data.tar.gz: 2974cac3f7d742dc001b45a2c5cdfe81f71e4e2cfd24d6052b4d563a790423c7
3
+ metadata.gz: 0dfc8f4ebc2732072e4bce05a6bb1e8ca892fa98c3cc7ad51871e3f16468fbbb
4
+ data.tar.gz: 7511ea6cfd3d519a2f03438162b25efc369422e981a4edf51193361928a32f0a
5
5
  SHA512:
6
- metadata.gz: 4da00730403d13520acf91dd762fe73e9a007ba6ba4146aa96cfb40d9e18ee71e4d45eb54569fab08a87df55838ee6c6302b1881c6b4a8eaa3376b10cf91462f
7
- data.tar.gz: 7d37f9de2bb33f902fb85390aa8bcdc0f1500131196cf55107eeaaa5df94cd78e22bb34bc88cadaae41ac20457eb3d74d34053989da08d457d7e2a04f99cfd51
6
+ metadata.gz: 50383fb5853a0b588c723776b019e58acd4f7f557316df8c0be2bcf426251bec32d5049f57290e3b125be2ca05b9ad80664e0ab0be437b25f34d6498eeddddc7
7
+ data.tar.gz: cbb323dbde2a7bbf9abfb6d694f133f93d6f81fb7366877a05a98d847a21f7779bcdc9e0f38df1d1d91b74c04335b0107ed2709f0c60530c7baa26e429de7f56
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
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
+
7
+ ## [0.6.1] 2026-10-26
8
+
9
+ - Proper i18n for Formatic::Search placeholders
10
+
1
11
  ## [0.6.0] 2026-10-26
2
12
 
3
13
  - Include `Formatic::Testing::Capybara::Search` in a Capybara RackTest class to call `search 'Acme Corp', from: 'Customer'`
data/README.md CHANGED
@@ -22,6 +22,11 @@ when its query still matches the input. An optional `with_clear` slot clears
22
22
  the current selection.
23
23
  An optional `with_add_record` slot stays hidden while searching and appears
24
24
  after a successful response, even when there are no matches.
25
+ The query input uses a field-specific `placeholder:` or `helpers.placeholder`
26
+ translation when present. Otherwise it combines the translated label with the
27
+ localized search prompt. English and German prompts are included; other locales
28
+ fall back to the label. Search fields prevent implicit form submission on Enter
29
+ by default; pass `prevent_submit_on_enter: false` to disable that behavior.
25
30
 
26
31
  ```ruby
27
32
  = render Formatic::Search.new(f:, attribute_name: :commander_id,
@@ -33,6 +38,20 @@ For Rack tests, call `Formatic::Testing::Search.call(page:, query: 'Bob', from:
33
38
  It reads the field's URL and parameter names, requests matching JSON directly, and
34
39
  fills the hidden input with a result returned by that endpoint.
35
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
+
36
55
  Capybara RackTest test classes can include the `search` helper instead:
37
56
 
38
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,9 +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
13
+ option :prevent_submit_on_enter, default: -> { true }
12
14
 
13
- renders_one :selected_record
14
15
  renders_one :add_record
15
16
 
16
17
  erb_template(::Formatic::Templates::Search.call)
@@ -24,7 +25,11 @@ module Formatic
24
25
  end
25
26
 
26
27
  def search_placeholder
27
- manual_placeholder.presence || wrapper.placeholder
28
+ return manual_placeholder if manual_placeholder.present?
29
+ return wrapper.placeholder if wrapper.placeholder.present?
30
+
31
+ translated_label = helpers.strip_tags(f.label(attribute_name))
32
+ ::I18n.t('formatic.search.placeholder', label: translated_label, default: translated_label)
28
33
  end
29
34
  end
30
35
  end
@@ -3,3 +3,6 @@ de:
3
3
 
4
4
  date:
5
5
  blank: Leer
6
+
7
+ search:
8
+ placeholder: '%{label} suchen...'
@@ -3,3 +3,6 @@ en:
3
3
 
4
4
  date:
5
5
  blank: Blank
6
+
7
+ search:
8
+ placeholder: Search %{label}...
@@ -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.0'
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.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - halo
@@ -249,7 +249,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
249
249
  - !ruby/object:Gem::Version
250
250
  version: '0'
251
251
  requirements: []
252
- rubygems_version: 4.0.6
252
+ rubygems_version: 4.0.20
253
253
  specification_version: 4
254
254
  summary: A set of ViewComponents for form elements
255
255
  test_files: []