template_form 0.4.5 → 0.4.7

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: 3ab5ed3be11107463cb37eb661448c8ca84b3e7406c614ddb96e304f20ac00a2
4
- data.tar.gz: af663d70babbee7cdd0252944b782594d16974741013abdec5d8ea3fa2219280
3
+ metadata.gz: 9da5ef52af3c84bb5b484452ecb693f4ce8ffe4c908f045c52ab9abede6030a7
4
+ data.tar.gz: 425de0736d09aeca3f27d73b0970c8bce1036ff10fd9d7d0ad496159fae9d537
5
5
  SHA512:
6
- metadata.gz: 26f2892859cbc20d66efc5a4ead2e270ca7512ab913477a5c6805bdb9449214e40d7eb4e4120ce1d63be079574ba4920f798ed85c5f8da9cdef76a7fcea599b6
7
- data.tar.gz: b38d06e77b48a4e06e4dd8dfb3866b27cc63ecb926944d6189c0c45b921f4184c78e48dc22bb319ddcb393895ed4a9d1f0ca336be6a93ec5ac1d5edbc3157e0f
6
+ metadata.gz: 710691873b95b172be68cda8357d68e6b1cf9d8ac733896fc18ca00485ca5687e46ed94ff0a73064f83d88dfa9045ed7fe7f0b6e9bf8909d2786a51173b5e009
7
+ data.tar.gz: 29752a9dcd330f0c95a7e682cdfb6747094fe784335d89884c96fd66d0487339a436a69cd9e6c1cc74723652ff0d3efd07bb2b948a347c23bf3379694e59c4be
data/README.md CHANGED
@@ -111,6 +111,8 @@ They should have these names:
111
111
 
112
112
  Use whatever extension goes with your template engine.
113
113
 
114
+ You can have several variations of a template, for example a [text input with inline add-on](https://tailwindui.com/components/application-ui/forms/input-groups#component-3620fb44e0a49ebce229526254018508). First, write your template at `<BASE>_<VARIANT>.html.erb`; say, `text_input_inline_add_on.html.erb`. Then use a `with: <VARIANT>` option in your view, e.g. `<%= form.input :name, with: :inline_add_on %>`.
115
+
114
116
  Inside each template you can use the normal Rails form helpers. Here's the template for Bulma's [text input](https://github.com/airblade/template_form/blob/master/lib/template_form/templates/bulma/text_input.html.erb):
115
117
 
116
118
  ```erb
@@ -159,6 +161,13 @@ def render
159
161
  end
160
162
  ```
161
163
 
164
+ You also have access to the `view` in which the template is contained. This is handy if you need to call methods on the view inside your template. For example, translating values for data attributes.
165
+
166
+ ```erb
167
+ <div data-controller="foo" data-foo-show-value=view.translate("foo.show")>
168
+ </div>
169
+ ```
170
+
162
171
  ### Attribute types and form inputs
163
172
 
164
173
  Template Form looks up the type for the attribute, falling back to `:string` if it can't find one. Then it uses the corresponding form input.
@@ -6,6 +6,7 @@ module TemplateForm
6
6
  def initialize(builder, attribute_name, options)
7
7
  @builder = builder
8
8
  @attribute_name = attribute_name
9
+ @view = options.delete(:view)
9
10
 
10
11
  @form_type = options.delete(:form_type) || builder.form_type
11
12
 
@@ -37,7 +38,8 @@ module TemplateForm
37
38
  hint_text: hint_text,
38
39
 
39
40
  options: options,
40
- errors: errors
41
+ errors: errors,
42
+ view: view
41
43
  ).html_safe
42
44
  end
43
45
 
@@ -48,6 +50,7 @@ module TemplateForm
48
50
  form_type
49
51
  builder
50
52
  attribute_name
53
+ view
51
54
 
52
55
  has_label
53
56
  label_text
@@ -74,7 +77,10 @@ module TemplateForm
74
77
 
75
78
  # TemplateForm::TextInput -> text_input
76
79
  def template_name
77
- self.class.name.demodulize.underscore
80
+ @template_name ||= [
81
+ self.class.name.demodulize.underscore,
82
+ options.delete(:with)
83
+ ].compact.join('_')
78
84
  end
79
85
 
80
86
  def detect_template_path(name)
@@ -22,6 +22,7 @@ module TemplateForm
22
22
  # associated record, the second and subsequent ones have the
23
23
  # correct form type.
24
24
  @form_type = options[:form_type] || TemplateForm.form_type
25
+ @view = options[:view]
25
26
  end
26
27
 
27
28
  def input(attribute_name, options = {})
@@ -31,6 +32,8 @@ module TemplateForm
31
32
 
32
33
  options[:type] ||= 'password' if attribute_name.match(/password/)
33
34
 
35
+ options.merge! view: @view
36
+
34
37
  input_for(attribute_type).new(self, attribute_name, options).render
35
38
  end
36
39
 
@@ -5,6 +5,7 @@ module TemplateForm
5
5
 
6
6
  def template_form_with(model: nil, scope: nil, url: nil, format: nil, **options, &block)
7
7
  options[:builder] = TemplateForm::FormBuilder
8
+ options[:view] = self
8
9
 
9
10
  with_template_form_field_error_proc do
10
11
  form_with model: model, scope: scope, url: url, format: format, **options, &block
@@ -8,6 +8,7 @@ module TemplateForm
8
8
  def initialize(builder, attribute_name, options)
9
9
  @builder = builder
10
10
  @attribute_name = attribute_name
11
+ @view = options.delete(:view)
11
12
 
12
13
  @form_type = options.delete(:form_type) || builder.form_type
13
14
 
@@ -54,7 +55,9 @@ module TemplateForm
54
55
  group_method: group_method,
55
56
  group_label_method: group_label_method,
56
57
  option_key_method: option_key_method,
57
- option_value_method: option_value_method
58
+ option_value_method: option_value_method,
59
+
60
+ view: view
58
61
  ).html_safe
59
62
  end
60
63
 
@@ -62,6 +65,7 @@ module TemplateForm
62
65
 
63
66
  attr_reader *%i[
64
67
  collection
68
+ view
65
69
 
66
70
  html_options
67
71
 
@@ -8,6 +8,7 @@ module TemplateForm
8
8
  def initialize(builder, attribute_name, options)
9
9
  @builder = builder
10
10
  @attribute_name = attribute_name
11
+ @view = options.delete(:view)
11
12
 
12
13
  @form_type = options.delete(:form_type) || builder.form_type
13
14
 
@@ -37,6 +38,7 @@ module TemplateForm
37
38
  builder,
38
39
  attribute_name: attribute_name,
39
40
  collection: collection,
41
+ view: view,
40
42
 
41
43
  has_label: has_label,
42
44
  label_text: label_text,
@@ -58,6 +60,7 @@ module TemplateForm
58
60
 
59
61
  attr_reader *%i[
60
62
  collection
63
+ view
61
64
 
62
65
  html_options
63
66
 
@@ -1,3 +1,3 @@
1
1
  module TemplateForm
2
- VERSION = "0.4.5"
2
+ VERSION = "0.4.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: template_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.4.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Stewart
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-24 00:00:00.000000000 Z
11
+ date: 2023-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -137,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  requirements: []
140
- rubygems_version: 3.1.2
140
+ rubygems_version: 3.4.10
141
141
  signing_key:
142
142
  specification_version: 4
143
143
  summary: A template-based form builder for Rails.