shoelace-rails 0.5.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4e0b7daab9653d58867634fed2dffbf3455c06cd47f084271650507b68128cdb
4
- data.tar.gz: d92ddd6a16d38a0dad1eecfc193534365fb1da246c0ad9a54d09e39d28f7f073
3
+ metadata.gz: 220b7683429f052cb711bac68076e05f812a19c7857864e66bc3863ca5740b78
4
+ data.tar.gz: 3947f8255bdb9a41d7faee272b77e356a78a233719b8e2b1d15e4bc814668fe1
5
5
  SHA512:
6
- metadata.gz: bb8b38ff8c3d03f12ebc72eb85cfe24fe108ba0838054e8878a439fc63083c1fc53be4d507b090da7d67daba440ae09a5cc855e4dcb008382930b0d11d15275c
7
- data.tar.gz: 62faa8f43c33c0c2f5d7690368b4b4694de05d8e7c3f68467f1716757d6f08b26bdd4da8ce252087c68e5b79500ef5016866cacb459f28468148e73e4b0ea3da
6
+ metadata.gz: '039b7be7a76bd89494cc7a95fab19d702c2ec87fc49f35dd6d19a88c7cc2ef0ebc1867d499a0e71b59044421d0199efed9b42417b1d8f38d3513157d5eebaf94'
7
+ data.tar.gz: fc32460b9adadafdf3bc35be7bf8c360a2e9dd5d2645edad50f9f7efeb420275ce2be57df4f3653b88efaf3254686a158b06b512d2201079cae661fade8c0419
data/CHANGELOG.md CHANGED
@@ -1,13 +1,27 @@
1
- ## v0.5.0
1
+ ## v0.6.1
2
2
 
3
- _<sup>released at 2023-03-21 04:03:27 UTC</sup>_
3
+ #### 🐞Bug Fixes
4
+
5
+ - Fixes a bug where form builders fail to render wtih a string `:as` option
6
+
7
+ ## [v0.6.0](https://github.com/yuki24/shoelace-rails/tree/v0.6.0)
8
+
9
+ _<sup>released at 2024-03-13 02:54:02 UTC</sup>_
10
+
11
+ #### ⭐️ Features
12
+
13
+ - Add the ability to use translations with form helpers ([<tt>626f271</tt>](https://github.com/yuki24/shoelace-rails/commit/626f271ca710dd48040907ff6a99e3bba6c5d57c))
14
+
15
+ ## [v0.5.0](https://github.com/yuki24/shoelace-rails/tree/v0.5.0)
16
+
17
+ _<sup>released at 2024-03-09 08:54:30 UTC</sup>_
4
18
 
5
19
  #### ⭐️ Features
6
20
 
7
- - Add support for Ruby 3.3 (`399f25567f964d0ea2e250eba6db28a2bcd038a3`)
8
- - Add support for Rails 7.1 (#4)
9
- - Add `#grouped_collection_select` (`2b91023d51e1d0a218f2102232241afa82aaf872`)
10
- - Make the `<sl-radio>` form helpers compatible with Shoelace [2.0.0-beta.80](https://shoelace.style/resources/changelog#id_2_0_0-beta_80) and above (`ef9a8345f2c5c921847aef15e19cf64a471d6473`)
21
+ - Add support for Ruby 3.3 ([<tt>399f255</tt>](https://github.com/yuki24/shoelace-rails/commit/399f25567f964d0ea2e250eba6db28a2bcd038a3))
22
+ - Add support for Rails 7.1 ([#4](https://github.com/yuki24/shoelace-rails/pull/4))
23
+ - Add `#grouped_collection_select` ([<tt>2b91023</tt>](https://github.com/yuki24/shoelace-rails/commit/2b91023d51e1d0a218f2102232241afa82aaf872))
24
+ - Make the `<sl-radio>` form helpers compatible with Shoelace [2.0.0-beta.80](https://shoelace.style/resources/changelog#id_2_0_0-beta_80) and above ([<tt>ef9a834</tt>](https://github.com/yuki24/shoelace-rails/commit/ef9a8345f2c5c921847aef15e19cf64a471d6473))
11
25
 
12
26
  ## [v0.4.1](https://github.com/yuki24/shoelace-rails/tree/v0.4.1)
13
27
 
@@ -57,8 +57,9 @@ module Shoelace
57
57
  options = @options.stringify_keys
58
58
  options["value"] = options.fetch("value") { value_before_type_cast }
59
59
  add_default_name_and_id(options)
60
+ label = options.delete('label').presence || @method_name.humanize
60
61
 
61
- @template_object.content_tag('sl-switch', @method_name.to_s.humanize, options, &block)
62
+ @template_object.content_tag('sl-switch', label, options, &block)
62
63
  end
63
64
  end
64
65
 
@@ -123,6 +124,7 @@ module Shoelace
123
124
  options = @options.stringify_keys
124
125
  options["value"] = @checked_value
125
126
  options["checked"] = true if input_checked?(options)
127
+ label = options.delete("label") || @method_name.humanize
126
128
 
127
129
  if options["multiple"]
128
130
  add_default_name_and_id_for_value(@checked_value, options)
@@ -136,7 +138,7 @@ module Shoelace
136
138
  sl_checkbox_tag = if block_given?
137
139
  @template_object.content_tag('sl-checkbox', '', options, &block)
138
140
  else
139
- @template_object.content_tag('sl-checkbox', @method_name.to_s.humanize, options)
141
+ @template_object.content_tag('sl-checkbox', label || @method_name.to_s.humanize, options)
140
142
  end
141
143
 
142
144
  if include_hidden
@@ -181,8 +183,9 @@ module Shoelace
181
183
  html_options = @html_options.stringify_keys
182
184
  html_options["value"] = value
183
185
  add_default_name_and_id(html_options)
186
+ html_options["label"] = @options[:label].presence || @method_name.humanize
184
187
 
185
- @template_object.content_tag('sl-radio-group', html_options.with_defaults(label: @method_name.humanize)) { super(&block) }
188
+ @template_object.content_tag('sl-radio-group', html_options) { super(&block) }
186
189
  end
187
190
 
188
191
  def hidden_field
@@ -206,52 +209,56 @@ module Shoelace
206
209
  url: :url
207
210
  }.each do |field_type, field_class|
208
211
  # def email_field(method, **options, &block)
209
- # ShoelaceInputField.new(:email, object_name, method, @template, options.with_defaults(label: method.to_s.humanize)).render(&block)
212
+ # ShoelaceInputField.new(:email, object_name, method, @template, options.with_defaults(label: label_text(method))).render(&block)
210
213
  # end
211
214
  eval <<-RUBY, nil, __FILE__, __LINE__ + 1
212
215
  def #{field_type}_field(method, **options, &block)
213
- ShoelaceInputField.new(:#{field_class}, object_name, method, @template, options.with_defaults(object: @object, label: method.to_s.humanize)).render(&block)
216
+ ShoelaceInputField.new(:#{field_class}, object_name, method, @template, options.with_defaults(object: @object, label: label_text(method))).render(&block)
214
217
  end
215
218
  RUBY
216
219
  end
217
220
 
218
221
  def color_field(method, **options)
219
- ShoelaceColorPicker.new(object_name, method, @template, options.with_defaults(object: @object)).render
222
+ ShoelaceColorPicker.new(object_name, method, @template, options.with_defaults(object: @object, label: label_text(method))).render
220
223
  end
221
224
  alias color_picker color_field
222
225
 
223
226
  def range_field(method, **options)
224
- ShoelaceRange.new(object_name, method, @template, options.with_defaults(object: @object, label: method.to_s.humanize)).render
227
+ ShoelaceRange.new(object_name, method, @template, options.with_defaults(object: @object, label: label_text(method))).render
225
228
  end
226
229
  alias range range_field
227
230
 
228
231
  def switch_field(method, **options, &block)
229
- ShoelaceSwitch.new(object_name, method, @template, options.with_defaults(object: @object)).render(&block)
232
+ if block_given?
233
+ ShoelaceSwitch.new(object_name, method, @template, options.with_defaults(object: @object)).render(&block)
234
+ else
235
+ ShoelaceSwitch.new(object_name, method, @template, options.with_defaults(object: @object, label: label_text(method))).render(&block)
236
+ end
230
237
  end
231
238
  alias switch switch_field
232
239
 
233
240
  def text_area(method, **options, &block)
234
- ShoelaceTextArea.new(object_name, method, @template, options.with_defaults(object: @object, label: method.to_s.humanize, resize: 'auto')).render(&block)
241
+ ShoelaceTextArea.new(object_name, method, @template, options.with_defaults(object: @object, label: label_text(method), resize: 'auto')).render(&block)
235
242
  end
236
243
 
237
244
  def check_box(method, options = {}, checked_value = "1", unchecked_value = "0", &block)
238
- ShoelaceCheckBox.new(object_name, method, @template, checked_value, unchecked_value, options.merge(object: @object)).render(&block)
245
+ ShoelaceCheckBox.new(object_name, method, @template, checked_value, unchecked_value, options.with_defaults(label: label_text(method)).merge(object: @object)).render(&block)
239
246
  end
240
247
 
241
248
  def select(method, choices = nil, options = {}, html_options = {}, &block)
242
- ShoelaceSelect.new(object_name, method, @template, choices, options.with_defaults(object: @object), html_options.with_defaults(label: method.to_s.humanize), &block).render
249
+ ShoelaceSelect.new(object_name, method, @template, choices, options.with_defaults(object: @object), html_options.with_defaults(label: label_text(method)), &block).render
243
250
  end
244
251
 
245
252
  def collection_select(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
246
- ShoelaceCollectionSelect.new(object_name, method, @template, collection, value_method, text_method, options.with_defaults(object: @object), html_options.with_defaults(label: method.to_s.humanize), &block).render
253
+ ShoelaceCollectionSelect.new(object_name, method, @template, collection, value_method, text_method, options.with_defaults(object: @object), html_options.with_defaults(label: label_text(method)), &block).render
247
254
  end
248
255
 
249
256
  def grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
250
- ShoelaceGroupedCollectionSelect.new(object_name, method, @template, collection, group_method, group_label_method, option_key_method, option_value_method, options.with_defaults(object: @object), html_options.with_defaults(label: method.to_s.humanize)).render
257
+ ShoelaceGroupedCollectionSelect.new(object_name, method, @template, collection, group_method, group_label_method, option_key_method, option_value_method, options.with_defaults(object: @object), html_options.with_defaults(label: label_text(method))).render
251
258
  end
252
259
 
253
260
  def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
254
- ShoelaceCollectionRadioButtons.new(object_name, method, @template, collection, value_method, text_method, options.with_defaults(object: @object), html_options).render(&block)
261
+ ShoelaceCollectionRadioButtons.new(object_name, method, @template, collection, value_method, text_method, options.with_defaults(object: @object, label: label_text(method)), html_options).render(&block)
255
262
  end
256
263
 
257
264
  def submit(value = nil, options = {})
@@ -259,6 +266,12 @@ module Shoelace
259
266
 
260
267
  @template.sl_submit_tag(value || submit_default_value, **options)
261
268
  end
269
+
270
+ private
271
+
272
+ def label_text(method, tag_value = nil)
273
+ ::ActionView::Helpers::Tags::Label::LabelBuilder.new(@template, object_name.to_s, method, object, tag_value).translation
274
+ end
262
275
  end
263
276
 
264
277
  DEFAULT_FORM_PARAMETERS = { builder: ShoelaceFormBuilder }
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Shoelace
4
4
  module Rails
5
- VERSION = "0.5.0"
5
+ VERSION = "0.6.1"
6
6
  end
7
7
  end
@@ -149,7 +149,7 @@ class FormHelperTest < ActionView::TestCase
149
149
  test "#color_field" do
150
150
  sl_form_for(User.new, url: "/") do |form|
151
151
  assert_dom_equal <<~HTML, form.color_field(:name)
152
- <sl-color-picker name="user[name]" id="user_name"></sl-color-picker>
152
+ <sl-color-picker name="user[name]" id="user_name" label="Name"></sl-color-picker>
153
153
  HTML
154
154
  end
155
155
  end
@@ -0,0 +1,168 @@
1
+ # frozen_string_literal: true
2
+ require 'test_helper'
3
+
4
+ require_relative '../../app/helpers/shoelace/form_helper'
5
+
6
+ class TranslationTest < ActionView::TestCase
7
+ include ActionView::Helpers::TranslationHelper
8
+ include Shoelace::FormHelper
9
+
10
+ AUTOCOMPLETE_ATTRIBUTE = ActionView::VERSION::STRING >= '6.1.0' ? 'autocomplete="off"' : ''
11
+
12
+ setup do
13
+ I18n.backend.store_translations :en,
14
+ helpers: {
15
+ label: {
16
+ user: { name: "Full Name" }
17
+ }
18
+ }
19
+
20
+ view_paths = ActionController::Base.view_paths
21
+ view_paths.each(&:clear_cache)
22
+ @view = ::ActionView::Base.with_empty_template_cache.with_view_paths(view_paths, {})
23
+ end
24
+
25
+ teardown do
26
+ I18n.backend.reload!
27
+ end
28
+
29
+ test "Form helpers should respect label translations" do
30
+ sl_form_for(User.new, url: "/") do |form|
31
+ assert_dom_equal <<~HTML, form.text_field(:name)
32
+ <sl-input label="Full Name" type="text" name="user[name]" id="user_name"></sl-input>
33
+ HTML
34
+ end
35
+ end
36
+
37
+ test "Form helpers should cast symbol object names to String" do
38
+ sl_form_for(User.new, as: :user, url: "/") do |form|
39
+ assert_dom_equal <<~HTML, form.text_field(:name)
40
+ <sl-input label="Full Name" type="text" name="user[name]" id="user_name"></sl-input>
41
+ HTML
42
+ end
43
+ end
44
+
45
+ test "#color_field should respect label translations" do
46
+ sl_form_for(User.new, url: "/") do |form|
47
+ assert_dom_equal <<~HTML, form.color_field(:name)
48
+ <sl-color-picker name="user[name]" id="user_name" label="Full Name"></sl-color-picker>
49
+ HTML
50
+ end
51
+ end
52
+
53
+ test "#range_field should respect label translations" do
54
+ sl_form_for(User.new, url: "/") do |form|
55
+ assert_dom_equal <<~HTML, form.range_field(:name)
56
+ <sl-range label="Full Name" name="user[name]" id="user_name"></sl-range>
57
+ HTML
58
+ end
59
+ end
60
+
61
+ test "#switch_field should respect label translations" do
62
+ sl_form_for(User.new, url: "/") do |form|
63
+ assert_dom_equal <<~HTML, form.switch_field(:name)
64
+ <sl-switch name="user[name]" id="user_name">Full Name</sl-switch>
65
+ HTML
66
+ end
67
+ end
68
+
69
+ test "#text_area should respect label translations" do
70
+ sl_form_for(User.new, url: "/") do |form|
71
+ assert_dom_equal <<~HTML, form.text_area(:name)
72
+ <sl-textarea label="Full Name" resize="auto" name="user[name]" id="user_name"></sl-textarea>
73
+ HTML
74
+ end
75
+ end
76
+
77
+ test "#check_box should respect label translations" do
78
+ sl_form_for(User.new, url: "/") do |form|
79
+ assert_dom_equal <<~HTML, form.check_box(:name)
80
+ <input name="user[name]" type="hidden" value="0" #{AUTOCOMPLETE_ATTRIBUTE} />
81
+ <sl-checkbox value="1" name="user[name]" id="user_name">Full Name</sl-checkbox>
82
+ HTML
83
+ end
84
+ end
85
+
86
+ test "#selec should respect label translationst" do
87
+ users = {
88
+ "Yuki Nishijima" => 1,
89
+ "Matz" => 2,
90
+ "Koichi Sasada" => 3
91
+ }
92
+
93
+ sl_form_for(User.new, url: "/") do |form|
94
+ assert_dom_equal <<~HTML, form.select(:name, users)
95
+ <sl-select label="Full Name" name="user[name]" id="user_name">
96
+ <sl-option value="1">Yuki Nishijima</sl-option>
97
+ <sl-option value="2">Matz</sl-option>
98
+ <sl-option value="3">Koichi Sasada</sl-option>
99
+ </sl-select>
100
+ HTML
101
+ end
102
+ end
103
+
104
+ test "#collection_select should respect label translations" do
105
+ users = {
106
+ 1 => "Yuki Nishijima",
107
+ 2 => "Matz",
108
+ 3 => "Koichi Sasada",
109
+ }
110
+
111
+ sl_form_for(User.new, url: "/") do |form|
112
+ assert_dom_equal <<~HTML, form.collection_select(:name, users, :first, :last)
113
+ <sl-select label="Full Name" name="user[name]" id="user_name">
114
+ <sl-option value="1">Yuki Nishijima</sl-option>
115
+ <sl-option value="2">Matz</sl-option>
116
+ <sl-option value="3">Koichi Sasada</sl-option>
117
+ </sl-select>
118
+ HTML
119
+ end
120
+ end
121
+
122
+ test "#grouped_collection_select should respect label translations" do
123
+ users = [
124
+ OpenStruct.new(
125
+ group_name: "Main maintainers",
126
+ members: [
127
+ OpenStruct.new(id: 1, name: "Matz"),
128
+ OpenStruct.new(id: 2, name: "Koichi Sasada"),
129
+ ]
130
+ ),
131
+ OpenStruct.new(
132
+ group_name: "Default gem maintainers",
133
+ members: [OpenStruct.new(id: 3, name: "Yuki Nishijima")]
134
+ ),
135
+ ]
136
+
137
+ sl_form_for(User.new(name: "2"), url: "/") do |form|
138
+ assert_dom_equal <<~HTML, form.grouped_collection_select(:name, users, :members, :group_name, :id, :name)
139
+ <sl-select label="Full Name" name="user[name]" id="user_name" value="2">
140
+ <small>Main maintainers</small>
141
+ <sl-option value="1">Matz</sl-option>
142
+ <sl-option value="2" checked="checked">Koichi Sasada</sl-option>
143
+ <sl-divider></sl-divider>
144
+ <small>Default gem maintainers</small>
145
+ <sl-option value="3">Yuki Nishijima</sl-option>
146
+ </sl-select>
147
+ HTML
148
+ end
149
+ end
150
+
151
+ test "#collection_radio_buttons should respect label translations" do
152
+ users = {
153
+ 1 => "Yuki Nishijima",
154
+ 2 => "Matz",
155
+ 3 => "Koichi Sasada",
156
+ }
157
+
158
+ sl_form_for(User.new, url: "/") do |form|
159
+ assert_dom_equal <<~HTML, form.collection_radio_buttons(:name, users, :first, :last)
160
+ <sl-radio-group label="Full Name" name="user[name]" id="user_name">
161
+ <sl-radio value="1" id="user_name_1">Yuki Nishijima</sl-radio>
162
+ <sl-radio value="2" id="user_name_2">Matz</sl-radio>
163
+ <sl-radio value="3" id="user_name_3">Koichi Sasada</sl-radio>
164
+ </sl-radio-group>
165
+ HTML
166
+ end
167
+ end
168
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shoelace-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuki Nishijima
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-09 00:00:00.000000000 Z
11
+ date: 2024-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionview
@@ -118,6 +118,7 @@ files:
118
118
  - shoelace-rails.gemspec
119
119
  - test/helpers/form_helper_test.rb
120
120
  - test/helpers/tag_helper_test.rb
121
+ - test/helpers/translation_test.rb
121
122
  - test/test_helper.rb
122
123
  homepage: https://github.com/yuki24/shoelace-rails
123
124
  licenses: