template_form 0.4.4 → 0.4.6

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: 831afd10695d17fed79e6a62a3ea6ecb2c51bcc437b65575d0f0779dfa55f922
4
- data.tar.gz: 2973a09954d830e56832cea3acd70c365cfcd7f395292f29e7b2d81b6defb247
3
+ metadata.gz: a2dffdc1b897b10460d029723d7f286651388e59c7a637eded104e78898edbab
4
+ data.tar.gz: bf8b340b369c8113fae4d5d6311a83383de8cb9bd832e10fbe41c2d7917cc960
5
5
  SHA512:
6
- metadata.gz: bb73aa0f7cfe2402e1b2515396f3553de092c76a9f50330fed9b4c9f2ebc2fadb2ba72d759954d6a54b12bd203ff2227f4199ba8a05e234eeecf58113c4ca837
7
- data.tar.gz: f280f82dce862022e1663a1d7ce981a285c35776dd6e6c0b952e0de361ce70325f0bbe8312df435e8318cc37a11c77662d2b8c06d805e3e1fc79b5715bc201e4
6
+ metadata.gz: 0ac600b29d24bb2d9bce1b63d7d1d75cd9b94b7c25c836ab3c23a734db71f2f22b361be6a49b7b16055585c7b3dc642e5f010d3846831ea0ed152cea5e26412f
7
+ data.tar.gz: 1b2e8c6caf8c4fa2d897ae42941c6d4d6f234c9bee95422f307614dc69836146cb1f87a3e451321e033113bcce0e003f00b2b2acbf113c5eb85b0a3ecec47371
data/README.md CHANGED
@@ -159,6 +159,13 @@ def render
159
159
  end
160
160
  ```
161
161
 
162
+ 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.
163
+
164
+ ```erb
165
+ <div data-controller="foo" data-foo-show-value=view.translate("foo.show")>
166
+ </div>
167
+ ```
168
+
162
169
  ### Attribute types and form inputs
163
170
 
164
171
  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
@@ -67,9 +70,18 @@ module TemplateForm
67
70
  end
68
71
 
69
72
  def template_file
70
- name = self.class.name.demodulize.underscore # TemplateForm::TextInput -> text_input
71
- f = paths(name).map { |p| Pathname.glob(p).first }.detect &:itself
72
- f or raise TemplateForm::MissingTemplateError, "No template found at: #{paths(name).join ', '}"
73
+ detect_template_path(template_name) or
74
+ raise TemplateForm::MissingTemplateError,
75
+ "No template found at: #{paths(template_name).join ', '}"
76
+ end
77
+
78
+ # TemplateForm::TextInput -> text_input
79
+ def template_name
80
+ self.class.name.demodulize.underscore
81
+ end
82
+
83
+ def detect_template_path(name)
84
+ paths(name).map { |p| Pathname.glob(p).first }.detect &:itself
73
85
  end
74
86
 
75
87
  def paths(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
 
@@ -3,5 +3,15 @@ require 'tilt'
3
3
  module TemplateForm
4
4
  class TextInput < BaseInput
5
5
 
6
+ private
7
+
8
+ def template_file
9
+ if options[:type] == 'password'
10
+ f = detect_template_path 'password_input'
11
+ return f if f
12
+ end
13
+ super
14
+ end
15
+
6
16
  end
7
17
  end
@@ -1,3 +1,3 @@
1
1
  module TemplateForm
2
- VERSION = "0.4.4"
2
+ VERSION = "0.4.6"
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.4
4
+ version: 0.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Stewart
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-09-25 00:00:00.000000000 Z
11
+ date: 2022-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -122,7 +122,7 @@ homepage: https://github.com/airblade/template_form
122
122
  licenses:
123
123
  - MIT
124
124
  metadata: {}
125
- post_install_message:
125
+ post_install_message:
126
126
  rdoc_options: []
127
127
  require_paths:
128
128
  - lib
@@ -137,8 +137,8 @@ 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
141
- signing_key:
140
+ rubygems_version: 3.2.33
141
+ signing_key:
142
142
  specification_version: 4
143
143
  summary: A template-based form builder for Rails.
144
144
  test_files: []