formatic 0.4.4 → 0.6.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.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +14 -0
  3. data/README.md +38 -0
  4. data/app/assets/javascript/declarations/search.d.ts +39 -0
  5. data/app/assets/javascript/declarations/search_record.d.ts +9 -0
  6. data/app/assets/javascript/declarations/search_requests.d.ts +13 -0
  7. data/app/assets/javascript/formatic/search.js +188 -0
  8. data/app/assets/javascript/formatic/search.js.map +1 -0
  9. data/app/assets/javascript/formatic/search_record.js +21 -0
  10. data/app/assets/javascript/formatic/search_record.js.map +1 -0
  11. data/app/assets/javascript/formatic/search_requests.js +72 -0
  12. data/app/assets/javascript/formatic/search_requests.js.map +1 -0
  13. data/app/assets/javascript/formatic/setup.js +6 -0
  14. data/app/assets/javascript/src/search.ts +242 -0
  15. data/app/assets/javascript/src/search_record.ts +24 -0
  16. data/app/assets/javascript/src/search_requests.ts +94 -0
  17. data/app/assets/stylesheets/formatic/components/search.sass +51 -0
  18. data/app/assets/stylesheets/formatic/formatic.css +25 -0
  19. data/app/assets/stylesheets/formatic/formatic.css.map +1 -1
  20. data/app/assets/stylesheets/formatic.sass +1 -0
  21. data/app/components/formatic/file.rb +27 -20
  22. data/app/components/formatic/search/choice.rb +14 -0
  23. data/app/components/formatic/search/clear.rb +12 -0
  24. data/app/components/formatic/search/frontend.rb +21 -0
  25. data/app/components/formatic/search/selected.rb +19 -0
  26. data/app/components/formatic/search.rb +30 -0
  27. data/config/importmap.rb +3 -0
  28. data/lib/formatic/engine.rb +5 -0
  29. data/lib/formatic/templates/search.rb +51 -0
  30. data/lib/formatic/testing/capybara.rb +20 -0
  31. data/lib/formatic/testing/search.rb +87 -0
  32. data/lib/formatic/version.rb +1 -1
  33. metadata +22 -1
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Formatic
4
+ module Templates
5
+ # Holds the ERB template for this component.
6
+ module Search
7
+ TEMPLATE = <<~ERB
8
+ <%= render wrapper do |formatic_wrapper| %>
9
+ <% formatic_wrapper.with_input do %>
10
+ <div class="c-formatic-search s-formatic js-formatic-search">
11
+ <input type="hidden" class="js-formatic-search__url" value="<%= url %>">
12
+ <input type="hidden" class="js-formatic-search__record-name" value="<%= record_name %>">
13
+ <input type="hidden" class="js-formatic-search__attribute-name" value="<%= attribute_name %>">
14
+ <input type="hidden" class="js-formatic-search__exclude-ids" value="<%= exclude_ids.join(',') %>">
15
+
16
+ <%= f.hidden_field attribute_name,
17
+ id: nil,
18
+ value:,
19
+ class: 'js-formatic-search__value' %>
20
+
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) %>
24
+ <% end %>
25
+ </div>
26
+
27
+ <% unless readonly %>
28
+ <div class="c-formatic-search__query">
29
+ <%= helpers.tag.input type: 'search',
30
+ id: query_input_id,
31
+ placeholder: search_placeholder,
32
+ autocomplete: 'off',
33
+ class: 'js-formatic-search__query-input' %>
34
+ </div>
35
+
36
+ <div class="c-formatic-search__results js-formatic-search__results"></div>
37
+ <div class="c-formatic-search__add-record js-formatic-search__add-record" hidden><%= add_record %></div>
38
+ <% end %>
39
+ </div>
40
+ <% end %>
41
+ <% end %>
42
+ ERB
43
+
44
+ private_constant :TEMPLATE
45
+
46
+ def self.call
47
+ TEMPLATE
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'search'
4
+
5
+ module Formatic
6
+ module Testing
7
+ module Capybara
8
+ # Include in a Capybara RackTest test class to select Formatic search results.
9
+ #
10
+ # include Formatic::Testing::Capybara::Search
11
+ #
12
+ # search 'Acme Corp', from: 'Customer'
13
+ module Search
14
+ def search(query, from:)
15
+ ::Formatic::Testing::Search.call(page:, query:, from:)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'calls'
5
+
6
+ module Formatic
7
+ module Testing
8
+ # Selects a Formatic search result in a Rack test.
9
+ class Search
10
+ include Calls
11
+
12
+ class SelectionError < ::StandardError; end
13
+
14
+ option :page
15
+ option :query
16
+ option :from
17
+
18
+ def call
19
+ ensure_rack_test_driver!
20
+ query_field = page.find_field(from)
21
+ component = search_component(query_field)
22
+ record = matching_record(search_records(component), query)
23
+
24
+ raise SelectionError, "No result matching #{query.inspect} from #{from.inspect}" unless record
25
+
26
+ apply_selection(component, query_field, record)
27
+ record
28
+ end
29
+
30
+ private
31
+
32
+ def ensure_rack_test_driver!
33
+ require 'capybara/rack_test/driver'
34
+ return if page.driver.is_a?(::Capybara::RackTest::Driver)
35
+
36
+ raise ArgumentError, 'Formatic::Testing::Search requires Capybara RackTest'
37
+ end
38
+
39
+ def search_component(query_field)
40
+ query_field.find(:xpath, 'ancestor::div[contains(@class, "js-formatic-search")][1]')
41
+ end
42
+
43
+ def search_records(component)
44
+ response = search_request(configured_value(component, 'url'), search_params(component))
45
+
46
+ raise SelectionError, "Search for #{from.inspect} returned HTTP #{response.status}" unless response.ok?
47
+
48
+ JSON.parse(response.body).fetch('records')
49
+ end
50
+
51
+ def search_params(component)
52
+ {
53
+ query: query,
54
+ record_name: configured_value(component, 'record-name'),
55
+ attribute_name: configured_value(component, 'attribute-name'),
56
+ exclude_ids: configured_value(component, 'exclude-ids')
57
+ }
58
+ end
59
+
60
+ def configured_value(component, class_suffix)
61
+ component.find(".js-formatic-search__#{class_suffix}", visible: :all).value
62
+ end
63
+
64
+ def search_request(url, params)
65
+ browser = page.driver.browser
66
+ browser.current_session.restore_state do
67
+ browser.get(url, params, 'HTTP_ACCEPT' => 'application/json')
68
+ end
69
+ end
70
+
71
+ def matching_record(records, query)
72
+ records.find do |record|
73
+ text = record.fetch('choice').gsub(/<[^>]*>/, ' ')
74
+ text.downcase.include?(query.downcase)
75
+ end
76
+ end
77
+
78
+ def apply_selection(component, query_field, record)
79
+ component.find('.js-formatic-search__value', visible: :all).set(record.fetch('id').to_s)
80
+ selected = component.find('.js-formatic-search__selected', visible: :all)
81
+ selected.native.inner_html = record.fetch('selected')
82
+ component.find('.js-formatic-search__results', visible: :all).native.inner_html = ''
83
+ query_field.set('')
84
+ end
85
+ end
86
+ end
87
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Formatic
4
- VERSION = '0.4.4'
4
+ VERSION = '0.6.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.4.4
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - halo
@@ -119,6 +119,9 @@ files:
119
119
  - README.md
120
120
  - app/assets/javascript/declarations/date.d.ts
121
121
  - app/assets/javascript/declarations/file.d.ts
122
+ - app/assets/javascript/declarations/search.d.ts
123
+ - app/assets/javascript/declarations/search_record.d.ts
124
+ - app/assets/javascript/declarations/search_requests.d.ts
122
125
  - app/assets/javascript/declarations/select.d.ts
123
126
  - app/assets/javascript/declarations/setup.d.ts
124
127
  - app/assets/javascript/declarations/stepper.d.ts
@@ -129,6 +132,12 @@ files:
129
132
  - app/assets/javascript/formatic/date.js.map
130
133
  - app/assets/javascript/formatic/file.js
131
134
  - app/assets/javascript/formatic/file.js.map
135
+ - app/assets/javascript/formatic/search.js
136
+ - app/assets/javascript/formatic/search.js.map
137
+ - app/assets/javascript/formatic/search_record.js
138
+ - app/assets/javascript/formatic/search_record.js.map
139
+ - app/assets/javascript/formatic/search_requests.js
140
+ - app/assets/javascript/formatic/search_requests.js.map
132
141
  - app/assets/javascript/formatic/select.js
133
142
  - app/assets/javascript/formatic/select.js.map
134
143
  - app/assets/javascript/formatic/setup.js
@@ -142,6 +151,9 @@ files:
142
151
  - app/assets/javascript/formatic/toggle.js.map
143
152
  - app/assets/javascript/src/date.ts
144
153
  - app/assets/javascript/src/file.ts
154
+ - app/assets/javascript/src/search.ts
155
+ - app/assets/javascript/src/search_record.ts
156
+ - app/assets/javascript/src/search_requests.ts
145
157
  - app/assets/javascript/src/select.ts
146
158
  - app/assets/javascript/src/stepper.ts
147
159
  - app/assets/javascript/src/string.ts
@@ -150,6 +162,7 @@ files:
150
162
  - app/assets/stylesheets/formatic.sass
151
163
  - app/assets/stylesheets/formatic/components/checklist.sass
152
164
  - app/assets/stylesheets/formatic/components/date.sass
165
+ - app/assets/stylesheets/formatic/components/search.sass
153
166
  - app/assets/stylesheets/formatic/components/select.sass
154
167
  - app/assets/stylesheets/formatic/components/stepper.sass
155
168
  - app/assets/stylesheets/formatic/components/string.sass
@@ -176,6 +189,11 @@ files:
176
189
  - app/components/formatic/date.rb
177
190
  - app/components/formatic/file.rb
178
191
  - app/components/formatic/files.rb
192
+ - app/components/formatic/search.rb
193
+ - app/components/formatic/search/choice.rb
194
+ - app/components/formatic/search/clear.rb
195
+ - app/components/formatic/search/frontend.rb
196
+ - app/components/formatic/search/selected.rb
179
197
  - app/components/formatic/select.rb
180
198
  - app/components/formatic/stepper.rb
181
199
  - app/components/formatic/string.rb
@@ -196,8 +214,11 @@ files:
196
214
  - lib/formatic/engine.rb
197
215
  - lib/formatic/safe_join.rb
198
216
  - lib/formatic/templates/date.rb
217
+ - lib/formatic/templates/search.rb
199
218
  - lib/formatic/templates/select.rb
200
219
  - lib/formatic/templates/wrapper.rb
220
+ - lib/formatic/testing/capybara.rb
221
+ - lib/formatic/testing/search.rb
201
222
  - lib/formatic/version.rb
202
223
  - lib/formatic/vscode_snippets.rb
203
224
  - lib/formatic/wrappers/alternative_attribute_name.rb