with_form 0.2.0 → 0.2.1

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: 7a78f39980ba03659bd61b33c94e65e8987dc590647ba8870271141a879eafca
4
- data.tar.gz: ad43310ff4fd4a42ddb3f154af5c9e799f1a25c82dafdd46c9d18a6f41f48304
3
+ metadata.gz: 66df9d30fbbd5e884be9e81fe23e5641ed5dabc6ef6fb5a1ce06925b42f80892
4
+ data.tar.gz: 3e8da161fbd871b3be23be62d5f7ba83b9105c49a67333195987a7a9359364a8
5
5
  SHA512:
6
- metadata.gz: 2a300ce3b20953400496bc7b7cf09e0bb9206870526c8d63cfb6987db7537ffe702788cd1543ad9eb28f83b99ac44bb39063b628be3677eac73069874a0bdbd6
7
- data.tar.gz: b596e4300be6ac53de36e9e81e85514722d1763f3c877ed82aa1b5b38a04adb8d02995d625d89eb800a07938f42343a76d565aa62c0523e338e16845aa675e95
6
+ metadata.gz: 138a371036a91a32ccec7edd66a5dbf80a69c7def7d608d47413a3fa07c44219a34c1a6b8183acd0ef95dd71841c80ebba59ba3a40e21f96ea5362b3a06cb879
7
+ data.tar.gz: 4a736600b9813271f9993394d3fc0186215ee76d8abd030b9d0e2d9303dcc6e4519335e074f04aa4e4f185bcc6206d570b74b5d45d61dbf0ca7881d3c8c1b453
data/README.md CHANGED
@@ -199,6 +199,165 @@ Those include:
199
199
 
200
200
  [actions]: https://www.rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions
201
201
 
202
+ ### `check` and `uncheck` support
203
+
204
+ The `check` and `uncheck` helpers can support a mixture of argument types and
205
+ use cases.
206
+
207
+ #### `with_form(scope:)`
208
+
209
+ When a call to `with_form` is passed the `scope:` option, the `check` and
210
+ `uncheck` helpers can accept both a `String` argument, or an `Array` argument
211
+ populated with `String` values.
212
+
213
+ For example, consider the following `features/new` template:
214
+
215
+ ```html+erb
216
+ <%# app/views/features/new.html.erb %>
217
+
218
+ <%= form_with(scope: :features) do |form| %>
219
+ <%= form.label(:supported) %>
220
+ <%= form.check_box(:supported) %>
221
+
222
+ <%= form.label(:languages) %>
223
+ <%= form.collection_check_boxes(
224
+ :languages,
225
+ [
226
+ [ "Ruby", "ruby" ],
227
+ [ "JavaScript", "js" ],
228
+ ],
229
+ :last,
230
+ :first,
231
+ ) %>
232
+ <% end %>
233
+ ```
234
+
235
+ There are two styles of [`<input type="checkbox">` elements][mdn-checkbox]
236
+ at-play in this template:
237
+
238
+ * a singular `<input type="checkbox">` element that corresponds to a
239
+ `Boolean`-backed `supported` attribute, constructed by
240
+ [`ActionView::Helpers::FormBuilder#check_box`][check_box]
241
+
242
+ * a collection of `<input type="checkbox">` elements that correspond to an
243
+ association of related `language` models, constructed by
244
+ [`ActionView::Helpers::FormBuilder#collection_check_boxes`][collection_check_boxes]
245
+
246
+ The corresponding `check` and `uncheck` method exposed by
247
+ `WithForm::TestHelpers` can interact with both.
248
+
249
+ To check or checked the `Boolean`-backed `<input type="checkbox">` elements,
250
+ pass the attribute's name as a [`Symbol`][ruby-symbol]:
251
+
252
+ ```ruby
253
+ with_form scope: :features do |form| %>
254
+ form.check :supported
255
+
256
+ form.uncheck :supported
257
+ end
258
+ ```
259
+
260
+ To check or checked the `Array`-backed `<input type="checkbox">` elements,
261
+ pass the values as either an `Array` of `String` values, or a singular `String`
262
+ value:
263
+
264
+ ```ruby
265
+ with_form scope: :features do |form| %>
266
+ form.check ["Ruby", "JavaScript"]
267
+
268
+ form.uncheck "JavaScript"
269
+ end
270
+ ```
271
+
272
+ #### `with_form(model:)`
273
+
274
+ When a call to `with_form` is passed the `model:` option, the `check` and
275
+ `uncheck` helpers can accept a `String` argument, an `Array` argument populated
276
+ with `String` values, or a singular [`Symbol` argument][ruby-symbol].
277
+
278
+ For example, consider the following hypothetical models:
279
+
280
+ Next, consider rendering a `<form>` element within the `features/new` template:
281
+
282
+ ```html+erb
283
+ <%# app/views/features/new.html.erb %>
284
+
285
+ <%= form_with(model: Feature.new) do |form| %>
286
+ <%= form.label(:supported) %>
287
+ <%= form.check_box(:supported) %>
288
+
289
+ <%= form.label(:language_ids) %>
290
+ <%= form.collection_check_boxes(
291
+ :language_ids,
292
+ Language.all,
293
+ :id,
294
+ :name,
295
+ ) %>
296
+ <% end %>
297
+ ```
298
+
299
+ There are two styles of [`<input type="checkbox">` elements][mdn-checkbox]
300
+ at-play in this template:
301
+
302
+ * a singular `<input type="checkbox">` element that corresponds to a
303
+ `Boolean`-backed `supported` attribute, constructed by
304
+ [`ActionView::Helpers::FormBuilder#check_box`][check_box]
305
+
306
+ * a collection of `<input type="checkbox">` elements that correspond to an
307
+ association of related `Language` models, constructed by
308
+ [`ActionView::Helpers::FormBuilder#collection_check_boxes`][collection_check_boxes]
309
+
310
+ The corresponding `check` and `uncheck` method exposed by
311
+ `WithForm::TestHelpers` can interact with both.
312
+
313
+ To check or checked the `Boolean`-backed `<input type="checkbox">` elements,
314
+ pass the attribute's name as a [`Symbol`][ruby-symbol]:
315
+
316
+ ```ruby
317
+ with_form model: Feature.new(supported: false) do |form| %>
318
+ form.check :supported
319
+
320
+ form.uncheck :supported
321
+ end
322
+ ```
323
+
324
+ To check or checked the `Array`-backed `<input type="checkbox">` elements,
325
+ pass the values as either an `Array` of `String` values, or a singular `String`
326
+ value:
327
+
328
+ ```ruby
329
+ feature = Feature.new(languages: Language.all, supported: true)
330
+
331
+ with_form model: feature do |form| %>
332
+ form.uncheck :supported
333
+
334
+ form.uncheck feature.languages.map(&:name)
335
+
336
+ form.check ["Ruby", "JavaScript"]
337
+
338
+ form.uncheck "JavaScript"
339
+ end
340
+ ```
341
+
342
+ When interacting with the `Boolean`-backed variation of the `<input
343
+ type="checkbox">` element through the `form.check` or `form.uncheck` calls, the
344
+ end-state of the `<input>` element will **always** correspond to the variation
345
+ of `check` or `uncheck`.
346
+
347
+ More directly stated: calls to `check` **will always** result in `<input
348
+ type="checkbox" checked>`, and calls to `uncheck` **will always** result in
349
+ `<input type="checkbox">`, regardless of the value of `Feature#supported`.
350
+
351
+ If your intention is that the `<input>` have the [`checked`
352
+ attribute][mdn-checked], call `check`. If your intention is that the `<input>`
353
+ _not_ have the [`checked` attribute][mdn-checked], call `uncheck`.
354
+
355
+ [mdn-checkbox]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input/checkbox
356
+ [check_box]: https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-check_box
357
+ [collection_check_boxes]: https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-collection_check_boxes
358
+ [ruby-symbol]: https://ruby-doc.org/core-2.7.1/Symbol.html
359
+ [mdn-checked]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input/checkbox#checked
360
+
202
361
  #### ActionText `rich_text_area` support
203
362
 
204
363
  When [`ActionText`][actiontext] is available, `with_form` provides a
@@ -1,4 +1,4 @@
1
- class CreateWidgetRecords < ActiveRecord::Migration[Rails.version.to_f]
1
+ class CreateWidgetRecords < ActiveRecord::Migration[5.2]
2
2
  def change
3
3
  create_table :widget_records do |t|
4
4
  t.text :text_field
@@ -0,0 +1,7 @@
1
+ class AddBooleanCheckBoxFieldToWidgetRecords < ActiveRecord::Migration[5.2]
2
+ def change
3
+ change_table :widget_records do |t|
4
+ t.boolean :boolean_check_box_field
5
+ end
6
+ end
7
+ end
@@ -32,9 +32,8 @@ module WithForm
32
32
  end
33
33
 
34
34
  def choose(attribute, **options)
35
- case attribute
36
- when Symbol
37
- value = @model.public_send(attribute)
35
+ if attribute.kind_of? Symbol
36
+ value = read_attribute(attribute)
38
37
  else
39
38
  value = attribute
40
39
  end
@@ -43,31 +42,28 @@ module WithForm
43
42
  end
44
43
 
45
44
  def check(attribute, **options)
46
- case attribute
47
- when Symbol
48
- values = Array(@model.public_send(attribute))
45
+ if attribute.kind_of? Symbol
46
+ value = read_attribute(attribute)
49
47
  else
50
- values = Array(attribute)
48
+ value = attribute
51
49
  end
52
50
 
53
- values.each { |value| scope_form.check(value, **options) }
51
+ Array(value).each { |value| scope_form.check(value, **options) }
54
52
  end
55
53
 
56
54
  def uncheck(attribute, **options)
57
- case attribute
58
- when Symbol
59
- values = Array(@model.public_send(attribute))
55
+ if attribute.kind_of? Symbol
56
+ value = read_attribute(attribute)
60
57
  else
61
- values = Array(attribute)
58
+ value = attribute
62
59
  end
63
60
 
64
- values.each { |value| scope_form.uncheck(value, **options) }
61
+ Array(value).each { |value| scope_form.uncheck(value, **options) }
65
62
  end
66
63
 
67
64
  def select(attribute, from: nil, **options)
68
- case attribute
69
- when Symbol
70
- value = @model.public_send(attribute)
65
+ if attribute.kind_of? Symbol
66
+ value = read_attribute(attribute)
71
67
  else
72
68
  value = attribute
73
69
  end
@@ -76,9 +72,8 @@ module WithForm
76
72
  end
77
73
 
78
74
  def unselect(attribute, from: nil, **options)
79
- case attribute
80
- when Symbol
81
- value = @model.public_send(attribute)
75
+ if attribute.kind_of? Symbol
76
+ value = read_attribute(attribute)
82
77
  else
83
78
  value = attribute
84
79
  end
@@ -102,5 +97,15 @@ module WithForm
102
97
  def scope_form
103
98
  WithForm::ScopeForm.new(scope: @model.model_name.i18n_key, page: @page)
104
99
  end
100
+
101
+ def read_attribute(attribute)
102
+ attribute_value = @model.public_send(attribute)
103
+
104
+ if attribute_value.in?([true, false, nil])
105
+ attribute
106
+ else
107
+ attribute_value
108
+ end
109
+ end
105
110
  end
106
111
  end
@@ -1,3 +1,3 @@
1
1
  module WithForm
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: with_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Doyle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-12 00:00:00.000000000 Z
11
+ date: 2020-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -106,6 +106,7 @@ files:
106
106
  - README.md
107
107
  - Rakefile
108
108
  - db/migrate/20200310042146_create_widget_records.rb
109
+ - db/migrate/20200417133424_add_boolean_check_box_field_to_widget_records.rb
109
110
  - lib/tasks/with_form_tasks.rake
110
111
  - lib/with_form.rb
111
112
  - lib/with_form/engine.rb