shoelace-rails 0.5.0 → 0.6.0

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: 206ca8361dedd910c96e3c13469e9bad996af7d2a91a0ca0af2cb8e1a7430808
4
+ data.tar.gz: 64df314d89557bd02dbf791bddcc49185017f5cb1d22c5046a23f1767df5bf18
5
5
  SHA512:
6
- metadata.gz: bb8b38ff8c3d03f12ebc72eb85cfe24fe108ba0838054e8878a439fc63083c1fc53be4d507b090da7d67daba440ae09a5cc855e4dcb008382930b0d11d15275c
7
- data.tar.gz: 62faa8f43c33c0c2f5d7690368b4b4694de05d8e7c3f68467f1716757d6f08b26bdd4da8ce252087c68e5b79500ef5016866cacb459f28468148e73e4b0ea3da
6
+ metadata.gz: 284ca5c08643f593de6554d02bb8e4857d141c8a4ddc02def92e573a273a3b45694e2ce14783779fbafe83eea1b9cbc85fd80ba52f36dc235e11d5cec6e9f6b1
7
+ data.tar.gz: 22ca68a850b08eebb0ad6d8fd48a8a7b65a3a4647acd34171f04cac20eca635475147804194957efe75fb4aeef68741b4b65fc3f10edc214165b3eb51addefcb
data/CHANGELOG.md CHANGED
@@ -1,13 +1,19 @@
1
- ## v0.5.0
1
+ ## v0.6.0
2
2
 
3
- _<sup>released at 2023-03-21 04:03:27 UTC</sup>_
3
+ #### ⭐️ Features
4
+
5
+ - Add the ability to use translations with form helpers
6
+
7
+ ## [v0.5.0](https://github.com/yuki24/shoelace-rails/tree/v0.5.0)
8
+
9
+ _<sup>released at 2024-03-09 08:54:30 UTC</sup>_
4
10
 
5
11
  #### ⭐️ Features
6
12
 
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`)
13
+ - Add support for Ruby 3.3 ([<tt>399f255</tt>](https://github.com/yuki24/shoelace-rails/commit/399f25567f964d0ea2e250eba6db28a2bcd038a3))
14
+ - Add support for Rails 7.1 ([#4](https://github.com/yuki24/shoelace-rails/pull/4))
15
+ - Add `#grouped_collection_select` ([<tt>2b91023</tt>](https://github.com/yuki24/shoelace-rails/commit/2b91023d51e1d0a218f2102232241afa82aaf872))
16
+ - 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
17
 
12
18
  ## [v0.4.1](https://github.com/yuki24/shoelace-rails/tree/v0.4.1)
13
19
 
@@ -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, 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.0"
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,160 @@
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 "#color_field should respect label translations" do
38
+ sl_form_for(User.new, url: "/") do |form|
39
+ assert_dom_equal <<~HTML, form.color_field(:name)
40
+ <sl-color-picker name="user[name]" id="user_name" label="Full Name"></sl-color-picker>
41
+ HTML
42
+ end
43
+ end
44
+
45
+ test "#range_field should respect label translations" do
46
+ sl_form_for(User.new, url: "/") do |form|
47
+ assert_dom_equal <<~HTML, form.range_field(:name)
48
+ <sl-range label="Full Name" name="user[name]" id="user_name"></sl-range>
49
+ HTML
50
+ end
51
+ end
52
+
53
+ test "#switch_field should respect label translations" do
54
+ sl_form_for(User.new, url: "/") do |form|
55
+ assert_dom_equal <<~HTML, form.switch_field(:name)
56
+ <sl-switch name="user[name]" id="user_name">Full Name</sl-switch>
57
+ HTML
58
+ end
59
+ end
60
+
61
+ test "#text_area should respect label translations" do
62
+ sl_form_for(User.new, url: "/") do |form|
63
+ assert_dom_equal <<~HTML, form.text_area(:name)
64
+ <sl-textarea label="Full Name" resize="auto" name="user[name]" id="user_name"></sl-textarea>
65
+ HTML
66
+ end
67
+ end
68
+
69
+ test "#check_box should respect label translations" do
70
+ sl_form_for(User.new, url: "/") do |form|
71
+ assert_dom_equal <<~HTML, form.check_box(:name)
72
+ <input name="user[name]" type="hidden" value="0" #{AUTOCOMPLETE_ATTRIBUTE} />
73
+ <sl-checkbox value="1" name="user[name]" id="user_name">Full Name</sl-checkbox>
74
+ HTML
75
+ end
76
+ end
77
+
78
+ test "#selec should respect label translationst" do
79
+ users = {
80
+ "Yuki Nishijima" => 1,
81
+ "Matz" => 2,
82
+ "Koichi Sasada" => 3
83
+ }
84
+
85
+ sl_form_for(User.new, url: "/") do |form|
86
+ assert_dom_equal <<~HTML, form.select(:name, users)
87
+ <sl-select label="Full Name" name="user[name]" id="user_name">
88
+ <sl-option value="1">Yuki Nishijima</sl-option>
89
+ <sl-option value="2">Matz</sl-option>
90
+ <sl-option value="3">Koichi Sasada</sl-option>
91
+ </sl-select>
92
+ HTML
93
+ end
94
+ end
95
+
96
+ test "#collection_select should respect label translations" do
97
+ users = {
98
+ 1 => "Yuki Nishijima",
99
+ 2 => "Matz",
100
+ 3 => "Koichi Sasada",
101
+ }
102
+
103
+ sl_form_for(User.new, url: "/") do |form|
104
+ assert_dom_equal <<~HTML, form.collection_select(:name, users, :first, :last)
105
+ <sl-select label="Full Name" name="user[name]" id="user_name">
106
+ <sl-option value="1">Yuki Nishijima</sl-option>
107
+ <sl-option value="2">Matz</sl-option>
108
+ <sl-option value="3">Koichi Sasada</sl-option>
109
+ </sl-select>
110
+ HTML
111
+ end
112
+ end
113
+
114
+ test "#grouped_collection_select should respect label translations" do
115
+ users = [
116
+ OpenStruct.new(
117
+ group_name: "Main maintainers",
118
+ members: [
119
+ OpenStruct.new(id: 1, name: "Matz"),
120
+ OpenStruct.new(id: 2, name: "Koichi Sasada"),
121
+ ]
122
+ ),
123
+ OpenStruct.new(
124
+ group_name: "Default gem maintainers",
125
+ members: [OpenStruct.new(id: 3, name: "Yuki Nishijima")]
126
+ ),
127
+ ]
128
+
129
+ sl_form_for(User.new(name: "2"), url: "/") do |form|
130
+ assert_dom_equal <<~HTML, form.grouped_collection_select(:name, users, :members, :group_name, :id, :name)
131
+ <sl-select label="Full Name" name="user[name]" id="user_name" value="2">
132
+ <small>Main maintainers</small>
133
+ <sl-option value="1">Matz</sl-option>
134
+ <sl-option value="2" checked="checked">Koichi Sasada</sl-option>
135
+ <sl-divider></sl-divider>
136
+ <small>Default gem maintainers</small>
137
+ <sl-option value="3">Yuki Nishijima</sl-option>
138
+ </sl-select>
139
+ HTML
140
+ end
141
+ end
142
+
143
+ test "#collection_radio_buttons should respect label translations" do
144
+ users = {
145
+ 1 => "Yuki Nishijima",
146
+ 2 => "Matz",
147
+ 3 => "Koichi Sasada",
148
+ }
149
+
150
+ sl_form_for(User.new, url: "/") do |form|
151
+ assert_dom_equal <<~HTML, form.collection_radio_buttons(:name, users, :first, :last)
152
+ <sl-radio-group label="Full Name" name="user[name]" id="user_name">
153
+ <sl-radio value="1" id="user_name_1">Yuki Nishijima</sl-radio>
154
+ <sl-radio value="2" id="user_name_2">Matz</sl-radio>
155
+ <sl-radio value="3" id="user_name_3">Koichi Sasada</sl-radio>
156
+ </sl-radio-group>
157
+ HTML
158
+ end
159
+ end
160
+ 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.0
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: