actionview 7.2.2.1 → 8.1.2
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 +4 -4
- data/CHANGELOG.md +104 -71
- data/README.rdoc +1 -1
- data/lib/action_view/base.rb +11 -11
- data/lib/action_view/buffers.rb +1 -1
- data/lib/action_view/dependency_tracker/erb_tracker.rb +37 -28
- data/lib/action_view/dependency_tracker/ruby_tracker.rb +2 -19
- data/lib/action_view/dependency_tracker/wildcard_resolver.rb +32 -0
- data/lib/action_view/dependency_tracker.rb +7 -1
- data/lib/action_view/digestor.rb +6 -2
- data/lib/action_view/gem_version.rb +3 -3
- data/lib/action_view/helpers/asset_tag_helper.rb +25 -6
- data/lib/action_view/helpers/atom_feed_helper.rb +1 -3
- data/lib/action_view/helpers/cache_helper.rb +10 -2
- data/lib/action_view/helpers/capture_helper.rb +2 -2
- data/lib/action_view/helpers/controller_helper.rb +6 -2
- data/lib/action_view/helpers/date_helper.rb +28 -4
- data/lib/action_view/helpers/form_helper.rb +103 -103
- data/lib/action_view/helpers/form_options_helper.rb +39 -35
- data/lib/action_view/helpers/form_tag_helper.rb +35 -25
- data/lib/action_view/helpers/javascript_helper.rb +5 -1
- data/lib/action_view/helpers/number_helper.rb +14 -0
- data/lib/action_view/helpers/output_safety_helper.rb +1 -2
- data/lib/action_view/helpers/rendering_helper.rb +160 -50
- data/lib/action_view/helpers/sanitize_helper.rb +6 -0
- data/lib/action_view/helpers/tag_helper.rb +57 -73
- data/lib/action_view/helpers/tags/base.rb +11 -9
- data/lib/action_view/helpers/tags/check_box.rb +9 -3
- data/lib/action_view/helpers/tags/collection_check_boxes.rb +4 -3
- data/lib/action_view/helpers/tags/collection_helpers.rb +2 -1
- data/lib/action_view/helpers/tags/datetime_field.rb +1 -1
- data/lib/action_view/helpers/tags/file_field.rb +7 -2
- data/lib/action_view/helpers/tags/hidden_field.rb +1 -1
- data/lib/action_view/helpers/tags/label.rb +3 -10
- data/lib/action_view/helpers/tags/radio_button.rb +1 -1
- data/lib/action_view/helpers/tags/select.rb +6 -1
- data/lib/action_view/helpers/tags/select_renderer.rb +6 -4
- data/lib/action_view/helpers/tags/text_area.rb +1 -1
- data/lib/action_view/helpers/tags/text_field.rb +1 -1
- data/lib/action_view/helpers/text_helper.rb +10 -3
- data/lib/action_view/helpers/translation_helper.rb +6 -1
- data/lib/action_view/helpers/url_helper.rb +39 -13
- data/lib/action_view/layouts.rb +7 -7
- data/lib/action_view/locale/en.yml +3 -0
- data/lib/action_view/log_subscriber.rb +1 -4
- data/lib/action_view/railtie.rb +12 -1
- data/lib/action_view/record_identifier.rb +22 -1
- data/lib/action_view/render_parser/prism_render_parser.rb +13 -1
- data/lib/action_view/render_parser/ripper_render_parser.rb +10 -1
- data/lib/action_view/renderer/partial_renderer.rb +18 -2
- data/lib/action_view/renderer/streaming_template_renderer.rb +8 -2
- data/lib/action_view/renderer/template_renderer.rb +3 -3
- data/lib/action_view/rendering.rb +2 -3
- data/lib/action_view/structured_event_subscriber.rb +97 -0
- data/lib/action_view/template/error.rb +18 -3
- data/lib/action_view/template/handlers/erb/erubi.rb +1 -1
- data/lib/action_view/template/handlers/erb.rb +77 -44
- data/lib/action_view/template/raw_file.rb +4 -0
- data/lib/action_view/template/resolver.rb +0 -1
- data/lib/action_view/template.rb +3 -4
- data/lib/action_view/test_case.rb +50 -53
- data/lib/action_view.rb +4 -0
- metadata +15 -16
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "cgi"
|
|
4
3
|
require "action_view/helpers/date_helper"
|
|
5
4
|
require "action_view/helpers/url_helper"
|
|
6
5
|
require "action_view/helpers/form_tag_helper"
|
|
@@ -30,20 +29,21 @@ module ActionView
|
|
|
30
29
|
# when the form is initially displayed, input fields corresponding to attributes
|
|
31
30
|
# of the resource should show the current values of those attributes.
|
|
32
31
|
#
|
|
33
|
-
# In \Rails, this is usually achieved by creating the form using
|
|
34
|
-
# a number of related helper methods.
|
|
35
|
-
#
|
|
36
|
-
#
|
|
37
|
-
#
|
|
32
|
+
# In \Rails, this is usually achieved by creating the form using either
|
|
33
|
+
# #form_with or #form_for and a number of related helper methods. These
|
|
34
|
+
# methods generate an appropriate <tt>form</tt> tag and yield a form
|
|
35
|
+
# builder object that knows the model the form is about. Input fields are
|
|
36
|
+
# created by calling methods defined on the form builder, which means they
|
|
37
|
+
# are able to generate the appropriate names and default values
|
|
38
38
|
# corresponding to the model attributes, as well as convenient IDs, etc.
|
|
39
|
-
# Conventions in the generated field names allow controllers to receive form
|
|
40
|
-
# nicely structured in +params+ with no effort on your side.
|
|
39
|
+
# Conventions in the generated field names allow controllers to receive form
|
|
40
|
+
# data nicely structured in +params+ with no effort on your side.
|
|
41
41
|
#
|
|
42
42
|
# For example, to create a new person you typically set up a new instance of
|
|
43
43
|
# +Person+ in the <tt>PeopleController#new</tt> action, <tt>@person</tt>, and
|
|
44
|
-
# in the view template pass that object to
|
|
44
|
+
# in the view template pass that object to #form_with or #form_for:
|
|
45
45
|
#
|
|
46
|
-
# <%=
|
|
46
|
+
# <%= form_with model: @person do |f| %>
|
|
47
47
|
# <%= f.label :first_name %>:
|
|
48
48
|
# <%= f.text_field :first_name %><br />
|
|
49
49
|
#
|
|
@@ -132,8 +132,8 @@ module ActionView
|
|
|
132
132
|
# <%= form_for :person do |f| %>
|
|
133
133
|
# First name: <%= f.text_field :first_name %><br />
|
|
134
134
|
# Last name : <%= f.text_field :last_name %><br />
|
|
135
|
-
# Biography : <%= f.
|
|
136
|
-
# Admin? : <%= f.
|
|
135
|
+
# Biography : <%= f.textarea :biography %><br />
|
|
136
|
+
# Admin? : <%= f.checkbox :admin %><br />
|
|
137
137
|
# <%= f.submit %>
|
|
138
138
|
# <% end %>
|
|
139
139
|
#
|
|
@@ -199,8 +199,8 @@ module ActionView
|
|
|
199
199
|
# <%= form_for :person do |f| %>
|
|
200
200
|
# First name: <%= f.text_field :first_name %>
|
|
201
201
|
# Last name : <%= f.text_field :last_name %>
|
|
202
|
-
# Biography : <%=
|
|
203
|
-
# Admin? : <%=
|
|
202
|
+
# Biography : <%= textarea :person, :biography %>
|
|
203
|
+
# Admin? : <%= checkbox_tag "person[admin]", "1", @person.company.admin? %>
|
|
204
204
|
# <%= f.submit %>
|
|
205
205
|
# <% end %>
|
|
206
206
|
#
|
|
@@ -208,7 +208,7 @@ module ActionView
|
|
|
208
208
|
# are designed to work with an object as base, like
|
|
209
209
|
# FormOptionsHelper#collection_select and DateHelper#datetime_select.
|
|
210
210
|
#
|
|
211
|
-
# ===
|
|
211
|
+
# === +form_for+ with a model object
|
|
212
212
|
#
|
|
213
213
|
# In the examples above, the object to be created or edited was
|
|
214
214
|
# represented by a symbol passed to +form_for+, and we noted that
|
|
@@ -363,7 +363,7 @@ module ActionView
|
|
|
363
363
|
#
|
|
364
364
|
# === Removing hidden model id's
|
|
365
365
|
#
|
|
366
|
-
# The form_for method automatically includes the model id as a hidden field in the form.
|
|
366
|
+
# The +form_for+ method automatically includes the model id as a hidden field in the form.
|
|
367
367
|
# This is used to maintain the correlation between the form data and its associated model.
|
|
368
368
|
# Some ORM systems do not use IDs on nested models so in this case you want to be able
|
|
369
369
|
# to disable the hidden id.
|
|
@@ -389,8 +389,8 @@ module ActionView
|
|
|
389
389
|
# <%= form_for @person, url: { action: "create" }, builder: LabellingFormBuilder do |f| %>
|
|
390
390
|
# <%= f.text_field :first_name %>
|
|
391
391
|
# <%= f.text_field :last_name %>
|
|
392
|
-
# <%= f.
|
|
393
|
-
# <%= f.
|
|
392
|
+
# <%= f.textarea :biography %>
|
|
393
|
+
# <%= f.checkbox :admin %>
|
|
394
394
|
# <%= f.submit %>
|
|
395
395
|
# <% end %>
|
|
396
396
|
#
|
|
@@ -668,8 +668,8 @@ module ActionView
|
|
|
668
668
|
# <%= form.text_field :first_name %>
|
|
669
669
|
# <%= form.text_field :last_name %>
|
|
670
670
|
#
|
|
671
|
-
# <%=
|
|
672
|
-
# <%=
|
|
671
|
+
# <%= textarea :person, :biography %>
|
|
672
|
+
# <%= checkbox_tag "person[admin]", "1", @person.company.admin? %>
|
|
673
673
|
#
|
|
674
674
|
# <%= form.submit %>
|
|
675
675
|
# <% end %>
|
|
@@ -730,8 +730,8 @@ module ActionView
|
|
|
730
730
|
# <%= form_with model: @person, url: { action: "create" }, builder: LabellingFormBuilder do |form| %>
|
|
731
731
|
# <%= form.text_field :first_name %>
|
|
732
732
|
# <%= form.text_field :last_name %>
|
|
733
|
-
# <%= form.
|
|
734
|
-
# <%= form.
|
|
733
|
+
# <%= form.textarea :biography %>
|
|
734
|
+
# <%= form.checkbox :admin %>
|
|
735
735
|
# <%= form.submit %>
|
|
736
736
|
# <% end %>
|
|
737
737
|
#
|
|
@@ -753,7 +753,7 @@ module ActionView
|
|
|
753
753
|
# form_with(**options.merge(builder: LabellingFormBuilder), &block)
|
|
754
754
|
# end
|
|
755
755
|
def form_with(model: false, scope: nil, url: nil, format: nil, **options, &block)
|
|
756
|
-
|
|
756
|
+
raise ArgumentError, "Passed nil to the :model argument, expect an object or false" if model.nil?
|
|
757
757
|
|
|
758
758
|
options = { allow_method_names_outside_object: true, skip_default_ids: !form_with_generates_ids }.merge!(options)
|
|
759
759
|
|
|
@@ -783,12 +783,12 @@ module ActionView
|
|
|
783
783
|
end
|
|
784
784
|
end
|
|
785
785
|
|
|
786
|
-
# Creates a scope around a specific model object like form_with, but
|
|
787
|
-
# doesn't create the form tags themselves. This makes fields_for
|
|
788
|
-
# for specifying additional model objects in the same form.
|
|
786
|
+
# Creates a scope around a specific model object like #form_with, but
|
|
787
|
+
# doesn't create the form tags themselves. This makes +fields_for+
|
|
788
|
+
# suitable for specifying additional model objects in the same form.
|
|
789
789
|
#
|
|
790
|
-
# Although the usage and purpose of +fields_for+ is similar to
|
|
791
|
-
# its method signature is slightly different. Like
|
|
790
|
+
# Although the usage and purpose of +fields_for+ is similar to #form_with's,
|
|
791
|
+
# its method signature is slightly different. Like #form_with, it yields
|
|
792
792
|
# a FormBuilder object associated with a particular model object to a block,
|
|
793
793
|
# and within the block allows methods to be called on the builder to
|
|
794
794
|
# generate fields associated with the model object. Fields may reflect
|
|
@@ -804,7 +804,7 @@ module ActionView
|
|
|
804
804
|
# Last name : <%= person_form.text_field :last_name %>
|
|
805
805
|
#
|
|
806
806
|
# <%= fields_for :permission, @person.permission do |permission_fields| %>
|
|
807
|
-
# Admin? : <%= permission_fields.
|
|
807
|
+
# Admin? : <%= permission_fields.checkbox :admin %>
|
|
808
808
|
# <% end %>
|
|
809
809
|
#
|
|
810
810
|
# <%= person_form.submit %>
|
|
@@ -821,7 +821,7 @@ module ActionView
|
|
|
821
821
|
# object to +fields_for+ -
|
|
822
822
|
#
|
|
823
823
|
# <%= fields_for :permission do |permission_fields| %>
|
|
824
|
-
# Admin?: <%= permission_fields.
|
|
824
|
+
# Admin?: <%= permission_fields.checkbox :admin %>
|
|
825
825
|
# <% end %>
|
|
826
826
|
#
|
|
827
827
|
# ...in which case, if <tt>:permission</tt> also happens to be the name of an
|
|
@@ -833,7 +833,7 @@ module ActionView
|
|
|
833
833
|
# name has been omitted) -
|
|
834
834
|
#
|
|
835
835
|
# <%= fields_for @person.permission do |permission_fields| %>
|
|
836
|
-
# Admin?: <%= permission_fields.
|
|
836
|
+
# Admin?: <%= permission_fields.checkbox :admin %>
|
|
837
837
|
# <% end %>
|
|
838
838
|
#
|
|
839
839
|
# and +fields_for+ will derive the required name of the field from the
|
|
@@ -847,7 +847,7 @@ module ActionView
|
|
|
847
847
|
# === Nested Attributes Examples
|
|
848
848
|
#
|
|
849
849
|
# When the object belonging to the current scope has a nested attribute
|
|
850
|
-
# writer for a certain attribute, fields_for will yield a new scope
|
|
850
|
+
# writer for a certain attribute, +fields_for+ will yield a new scope
|
|
851
851
|
# for that attribute. This allows you to create forms that set or change
|
|
852
852
|
# the attributes of a parent object and its associations in one go.
|
|
853
853
|
#
|
|
@@ -914,7 +914,7 @@ module ActionView
|
|
|
914
914
|
# ...
|
|
915
915
|
# <%= person_form.fields_for :address do |address_fields| %>
|
|
916
916
|
# ...
|
|
917
|
-
# Delete: <%= address_fields.
|
|
917
|
+
# Delete: <%= address_fields.checkbox :_destroy %>
|
|
918
918
|
# <% end %>
|
|
919
919
|
# ...
|
|
920
920
|
# <% end %>
|
|
@@ -936,7 +936,7 @@ module ActionView
|
|
|
936
936
|
# end
|
|
937
937
|
#
|
|
938
938
|
# Note that the <tt>projects_attributes=</tt> writer method is in fact
|
|
939
|
-
# required for fields_for to correctly identify <tt>:projects</tt> as a
|
|
939
|
+
# required for +fields_for+ to correctly identify <tt>:projects</tt> as a
|
|
940
940
|
# collection, and the correct indices to be set in the form markup.
|
|
941
941
|
#
|
|
942
942
|
# When projects is already an association on Person you can use
|
|
@@ -948,7 +948,7 @@ module ActionView
|
|
|
948
948
|
# end
|
|
949
949
|
#
|
|
950
950
|
# This model can now be used with a nested fields_for. The block given to
|
|
951
|
-
# the nested fields_for call will be repeated for each instance in the
|
|
951
|
+
# the nested +fields_for+ call will be repeated for each instance in the
|
|
952
952
|
# collection:
|
|
953
953
|
#
|
|
954
954
|
# <%= form_with model: @person do |person_form| %>
|
|
@@ -1002,7 +1002,7 @@ module ActionView
|
|
|
1002
1002
|
# <%= form_with model: @person do |person_form| %>
|
|
1003
1003
|
# ...
|
|
1004
1004
|
# <%= person_form.fields_for :projects do |project_fields| %>
|
|
1005
|
-
# Delete: <%= project_fields.
|
|
1005
|
+
# Delete: <%= project_fields.checkbox :_destroy %>
|
|
1006
1006
|
# <% end %>
|
|
1007
1007
|
# ...
|
|
1008
1008
|
# <% end %>
|
|
@@ -1020,10 +1020,10 @@ module ActionView
|
|
|
1020
1020
|
# ...
|
|
1021
1021
|
# <% end %>
|
|
1022
1022
|
#
|
|
1023
|
-
# Note that fields_for will automatically generate a hidden field
|
|
1023
|
+
# Note that +fields_for+ will automatically generate a hidden field
|
|
1024
1024
|
# to store the ID of the record if it responds to <tt>persisted?</tt>.
|
|
1025
1025
|
# There are circumstances where this hidden field is not needed and you
|
|
1026
|
-
# can pass <tt>include_id: false</tt> to prevent fields_for from
|
|
1026
|
+
# can pass <tt>include_id: false</tt> to prevent +fields_for+ from
|
|
1027
1027
|
# rendering it automatically.
|
|
1028
1028
|
def fields_for(record_name, record_object = nil, options = {}, &block)
|
|
1029
1029
|
options = { model: record_object, allow_method_names_outside_object: false, skip_default_ids: false }.merge!(options)
|
|
@@ -1032,7 +1032,7 @@ module ActionView
|
|
|
1032
1032
|
end
|
|
1033
1033
|
|
|
1034
1034
|
# Scopes input fields with either an explicit scope or model.
|
|
1035
|
-
# Like
|
|
1035
|
+
# Like #form_with does with <tt>:scope</tt> or <tt>:model</tt>,
|
|
1036
1036
|
# except it doesn't output the form tags.
|
|
1037
1037
|
#
|
|
1038
1038
|
# # Using a scope prefixes the input field names:
|
|
@@ -1047,7 +1047,7 @@ module ActionView
|
|
|
1047
1047
|
# <% end %>
|
|
1048
1048
|
# # => <input type="text" name="comment[body]" value="full bodied">
|
|
1049
1049
|
#
|
|
1050
|
-
# # Using
|
|
1050
|
+
# # Using `fields` with `form_with`:
|
|
1051
1051
|
# <%= form_with model: @article do |form| %>
|
|
1052
1052
|
# <%= form.text_field :title %>
|
|
1053
1053
|
#
|
|
@@ -1056,21 +1056,21 @@ module ActionView
|
|
|
1056
1056
|
# <% end %>
|
|
1057
1057
|
# <% end %>
|
|
1058
1058
|
#
|
|
1059
|
-
# Much like
|
|
1059
|
+
# Much like #form_with a FormBuilder instance associated with the scope
|
|
1060
1060
|
# or model is yielded, so any generated field names are prefixed with
|
|
1061
1061
|
# either the passed scope or the scope inferred from the <tt>:model</tt>.
|
|
1062
1062
|
#
|
|
1063
1063
|
# === Mixing with other form helpers
|
|
1064
1064
|
#
|
|
1065
|
-
# While
|
|
1065
|
+
# While #form_with uses a FormBuilder object it's possible to mix and
|
|
1066
1066
|
# match the stand-alone FormHelper methods and methods
|
|
1067
1067
|
# from FormTagHelper:
|
|
1068
1068
|
#
|
|
1069
1069
|
# <%= fields model: @comment do |fields| %>
|
|
1070
1070
|
# <%= fields.text_field :body %>
|
|
1071
1071
|
#
|
|
1072
|
-
# <%=
|
|
1073
|
-
# <%=
|
|
1072
|
+
# <%= textarea :commenter, :biography %>
|
|
1073
|
+
# <%= checkbox_tag "comment[all_caps]", "1", @comment.commenter.hulk_mode? %>
|
|
1074
1074
|
# <% end %>
|
|
1075
1075
|
#
|
|
1076
1076
|
# Same goes for the methods in FormOptionsHelper and DateHelper designed
|
|
@@ -1220,7 +1220,7 @@ module ActionView
|
|
|
1220
1220
|
# hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
|
|
1221
1221
|
# shown.
|
|
1222
1222
|
#
|
|
1223
|
-
# Using this method inside a
|
|
1223
|
+
# Using this method inside a #form_with block will set the enclosing form's encoding to <tt>multipart/form-data</tt>.
|
|
1224
1224
|
#
|
|
1225
1225
|
# ==== Options
|
|
1226
1226
|
# * Creates standard HTML attributes for the tag.
|
|
@@ -1255,28 +1255,29 @@ module ActionView
|
|
|
1255
1255
|
# hash with +options+.
|
|
1256
1256
|
#
|
|
1257
1257
|
# ==== Examples
|
|
1258
|
-
#
|
|
1258
|
+
# textarea(:article, :body, cols: 20, rows: 40)
|
|
1259
1259
|
# # => <textarea cols="20" rows="40" id="article_body" name="article[body]">
|
|
1260
1260
|
# # #{@article.body}
|
|
1261
1261
|
# # </textarea>
|
|
1262
1262
|
#
|
|
1263
|
-
#
|
|
1263
|
+
# textarea(:comment, :text, size: "20x30")
|
|
1264
1264
|
# # => <textarea cols="20" rows="30" id="comment_text" name="comment[text]">
|
|
1265
1265
|
# # #{@comment.text}
|
|
1266
1266
|
# # </textarea>
|
|
1267
1267
|
#
|
|
1268
|
-
#
|
|
1268
|
+
# textarea(:application, :notes, cols: 40, rows: 15, class: 'app_input')
|
|
1269
1269
|
# # => <textarea cols="40" rows="15" id="application_notes" name="application[notes]" class="app_input">
|
|
1270
1270
|
# # #{@application.notes}
|
|
1271
1271
|
# # </textarea>
|
|
1272
1272
|
#
|
|
1273
|
-
#
|
|
1273
|
+
# textarea(:entry, :body, size: "20x20", disabled: 'disabled')
|
|
1274
1274
|
# # => <textarea cols="20" rows="20" id="entry_body" name="entry[body]" disabled="disabled">
|
|
1275
1275
|
# # #{@entry.body}
|
|
1276
1276
|
# # </textarea>
|
|
1277
|
-
def
|
|
1277
|
+
def textarea(object_name, method, options = {})
|
|
1278
1278
|
Tags::TextArea.new(object_name, method, self, options).render
|
|
1279
1279
|
end
|
|
1280
|
+
alias_method :text_area, :textarea
|
|
1280
1281
|
|
|
1281
1282
|
# Returns a checkbox tag tailored for accessing a specified attribute (identified by +method+) on an object
|
|
1282
1283
|
# assigned to the template (identified by +object+). This object must be an instance object (@object) and not a local object.
|
|
@@ -1316,7 +1317,7 @@ module ActionView
|
|
|
1316
1317
|
# within an array-like parameter, as in
|
|
1317
1318
|
#
|
|
1318
1319
|
# <%= fields_for "project[invoice_attributes][]", invoice, index: nil do |form| %>
|
|
1319
|
-
# <%= form.
|
|
1320
|
+
# <%= form.checkbox :paid %>
|
|
1320
1321
|
# ...
|
|
1321
1322
|
# <% end %>
|
|
1322
1323
|
#
|
|
@@ -1324,27 +1325,28 @@ module ActionView
|
|
|
1324
1325
|
# the elements of the array. For each item with a checked check box you
|
|
1325
1326
|
# get an extra ghost item with only that attribute, assigned to "0".
|
|
1326
1327
|
#
|
|
1327
|
-
# In that case it is preferable to either use
|
|
1328
|
+
# In that case it is preferable to either use FormTagHelper#checkbox_tag or to use
|
|
1328
1329
|
# hashes instead of arrays.
|
|
1329
1330
|
#
|
|
1330
1331
|
# ==== Examples
|
|
1331
1332
|
#
|
|
1332
1333
|
# # Let's say that @article.validated? is 1:
|
|
1333
|
-
#
|
|
1334
|
+
# checkbox("article", "validated")
|
|
1334
1335
|
# # => <input name="article[validated]" type="hidden" value="0" />
|
|
1335
1336
|
# # <input checked="checked" type="checkbox" id="article_validated" name="article[validated]" value="1" />
|
|
1336
1337
|
#
|
|
1337
1338
|
# # Let's say that @puppy.gooddog is "no":
|
|
1338
|
-
#
|
|
1339
|
+
# checkbox("puppy", "gooddog", {}, "yes", "no")
|
|
1339
1340
|
# # => <input name="puppy[gooddog]" type="hidden" value="no" />
|
|
1340
1341
|
# # <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" />
|
|
1341
1342
|
#
|
|
1342
|
-
#
|
|
1343
|
+
# checkbox("eula", "accepted", { class: 'eula_check' }, "yes", "no")
|
|
1343
1344
|
# # => <input name="eula[accepted]" type="hidden" value="no" />
|
|
1344
1345
|
# # <input type="checkbox" class="eula_check" id="eula_accepted" name="eula[accepted]" value="yes" />
|
|
1345
|
-
def
|
|
1346
|
+
def checkbox(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
|
|
1346
1347
|
Tags::CheckBox.new(object_name, method, self, checked_value, unchecked_value, options).render
|
|
1347
1348
|
end
|
|
1349
|
+
alias_method :check_box, :checkbox
|
|
1348
1350
|
|
|
1349
1351
|
# Returns a radio button tag for accessing a specified attribute (identified by +method+) on an object
|
|
1350
1352
|
# assigned to the template (identified by +object+). If the current value of +method+ is +tag_value+ the
|
|
@@ -1629,17 +1631,18 @@ module ActionView
|
|
|
1629
1631
|
#
|
|
1630
1632
|
# A +FormBuilder+ object is associated with a particular model object and
|
|
1631
1633
|
# allows you to generate fields associated with the model object. The
|
|
1632
|
-
# +FormBuilder+ object is yielded when using
|
|
1634
|
+
# +FormBuilder+ object is yielded when using
|
|
1635
|
+
# {form_with}[rdof-ref:ActionView::Helpers::FormHelper#form_with] or #fields_for.
|
|
1633
1636
|
# For example:
|
|
1634
1637
|
#
|
|
1635
1638
|
# <%= form_with model: @person do |person_form| %>
|
|
1636
1639
|
# Name: <%= person_form.text_field :name %>
|
|
1637
|
-
# Admin: <%= person_form.
|
|
1640
|
+
# Admin: <%= person_form.checkbox :admin %>
|
|
1638
1641
|
# <% end %>
|
|
1639
1642
|
#
|
|
1640
1643
|
# In the above block, a +FormBuilder+ object is yielded as the
|
|
1641
1644
|
# +person_form+ variable. This allows you to generate the +text_field+
|
|
1642
|
-
# and +
|
|
1645
|
+
# and +checkbox+ fields by specifying their eponymous methods, which
|
|
1643
1646
|
# modify the underlying template and associates the <tt>@person</tt> model object
|
|
1644
1647
|
# with the form.
|
|
1645
1648
|
#
|
|
@@ -1681,7 +1684,7 @@ module ActionView
|
|
|
1681
1684
|
# The methods which wrap a form helper call.
|
|
1682
1685
|
class_attribute :field_helpers, default: [
|
|
1683
1686
|
:fields_for, :fields, :label, :text_field, :password_field,
|
|
1684
|
-
:hidden_field, :file_field, :
|
|
1687
|
+
:hidden_field, :file_field, :textarea, :checkbox,
|
|
1685
1688
|
:radio_button, :color_field, :search_field,
|
|
1686
1689
|
:telephone_field, :phone_field, :date_field,
|
|
1687
1690
|
:time_field, :datetime_field, :datetime_local_field,
|
|
@@ -1767,7 +1770,7 @@ module ActionView
|
|
|
1767
1770
|
# <% end %>
|
|
1768
1771
|
#
|
|
1769
1772
|
# In the example above, the <tt><input type="text"></tt> element built by
|
|
1770
|
-
# the call to
|
|
1773
|
+
# the call to #text_field declares an
|
|
1771
1774
|
# <tt>aria-describedby</tt> attribute referencing the <tt><span></tt>
|
|
1772
1775
|
# element, sharing a common <tt>id</tt> root (<tt>article_title</tt>, in this
|
|
1773
1776
|
# case).
|
|
@@ -1824,14 +1827,14 @@ module ActionView
|
|
|
1824
1827
|
# Please refer to the documentation of the base helper for details.
|
|
1825
1828
|
|
|
1826
1829
|
##
|
|
1827
|
-
# :method:
|
|
1830
|
+
# :method: textarea
|
|
1828
1831
|
#
|
|
1829
|
-
# :call-seq:
|
|
1832
|
+
# :call-seq: textarea(method, options = {})
|
|
1830
1833
|
#
|
|
1831
|
-
# Wraps ActionView::Helpers::FormHelper#
|
|
1834
|
+
# Wraps ActionView::Helpers::FormHelper#textarea for form builders:
|
|
1832
1835
|
#
|
|
1833
1836
|
# <%= form_with model: @user do |f| %>
|
|
1834
|
-
# <%= f.
|
|
1837
|
+
# <%= f.textarea :detail %>
|
|
1835
1838
|
# <% end %>
|
|
1836
1839
|
#
|
|
1837
1840
|
# Please refer to the documentation of the base helper for details.
|
|
@@ -2019,27 +2022,23 @@ module ActionView
|
|
|
2019
2022
|
# Please refer to the documentation of the base helper for details.
|
|
2020
2023
|
|
|
2021
2024
|
ActiveSupport::CodeGenerator.batch(self, __FILE__, __LINE__) do |code_generator|
|
|
2022
|
-
(field_helpers - [:label, :
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
@template
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
method, # method,
|
|
2030
|
-
objectify_options(options)) # objectify_options(options))
|
|
2031
|
-
end # end
|
|
2032
|
-
RUBY_EVAL
|
|
2025
|
+
(field_helpers - [:label, :checkbox, :radio_button, :fields_for, :fields, :hidden_field, :file_field]).each do |selector|
|
|
2026
|
+
code_generator.class_eval do |batch|
|
|
2027
|
+
batch <<
|
|
2028
|
+
"def #{selector}(method, options = {})" <<
|
|
2029
|
+
" @template.#{selector}(@object_name, method, objectify_options(options))" <<
|
|
2030
|
+
"end"
|
|
2031
|
+
end
|
|
2033
2032
|
end
|
|
2034
|
-
end
|
|
2035
2033
|
end
|
|
2034
|
+
alias_method :text_area, :textarea
|
|
2036
2035
|
|
|
2037
|
-
# Creates a scope around a specific model object like form_with, but
|
|
2038
|
-
# doesn't create the form tags themselves. This makes fields_for
|
|
2039
|
-
# for specifying additional model objects in the same form.
|
|
2036
|
+
# Creates a scope around a specific model object like #form_with, but
|
|
2037
|
+
# doesn't create the form tags themselves. This makes +fields_for+
|
|
2038
|
+
# suitable for specifying additional model objects in the same form.
|
|
2040
2039
|
#
|
|
2041
|
-
# Although the usage and purpose of +fields_for+ is similar to
|
|
2042
|
-
# its method signature is slightly different. Like
|
|
2040
|
+
# Although the usage and purpose of +fields_for+ is similar to #form_with's,
|
|
2041
|
+
# its method signature is slightly different. Like #form_with, it yields
|
|
2043
2042
|
# a FormBuilder object associated with a particular model object to a block,
|
|
2044
2043
|
# and within the block allows methods to be called on the builder to
|
|
2045
2044
|
# generate fields associated with the model object. Fields may reflect
|
|
@@ -2055,7 +2054,7 @@ module ActionView
|
|
|
2055
2054
|
# Last name : <%= person_form.text_field :last_name %>
|
|
2056
2055
|
#
|
|
2057
2056
|
# <%= fields_for :permission, @person.permission do |permission_fields| %>
|
|
2058
|
-
# Admin? : <%= permission_fields.
|
|
2057
|
+
# Admin? : <%= permission_fields.checkbox :admin %>
|
|
2059
2058
|
# <% end %>
|
|
2060
2059
|
#
|
|
2061
2060
|
# <%= person_form.submit %>
|
|
@@ -2072,7 +2071,7 @@ module ActionView
|
|
|
2072
2071
|
# object to +fields_for+ -
|
|
2073
2072
|
#
|
|
2074
2073
|
# <%= fields_for :permission do |permission_fields| %>
|
|
2075
|
-
# Admin?: <%= permission_fields.
|
|
2074
|
+
# Admin?: <%= permission_fields.checkbox :admin %>
|
|
2076
2075
|
# <% end %>
|
|
2077
2076
|
#
|
|
2078
2077
|
# ...in which case, if <tt>:permission</tt> also happens to be the name of an
|
|
@@ -2084,7 +2083,7 @@ module ActionView
|
|
|
2084
2083
|
# name has been omitted) -
|
|
2085
2084
|
#
|
|
2086
2085
|
# <%= fields_for @person.permission do |permission_fields| %>
|
|
2087
|
-
# Admin?: <%= permission_fields.
|
|
2086
|
+
# Admin?: <%= permission_fields.checkbox :admin %>
|
|
2088
2087
|
# <% end %>
|
|
2089
2088
|
#
|
|
2090
2089
|
# and +fields_for+ will derive the required name of the field from the
|
|
@@ -2102,7 +2101,7 @@ module ActionView
|
|
|
2102
2101
|
# <%= form_with model: @person do |person_form| %>
|
|
2103
2102
|
# ...
|
|
2104
2103
|
# <%= fields_for :permission, @person.permission, {} do |permission_fields| %>
|
|
2105
|
-
# Admin?: <%=
|
|
2104
|
+
# Admin?: <%= checkbox_tag permission_fields.field_name(:admin), @person.permission[:admin] %>
|
|
2106
2105
|
# <% end %>
|
|
2107
2106
|
# ...
|
|
2108
2107
|
# <% end %>
|
|
@@ -2110,7 +2109,7 @@ module ActionView
|
|
|
2110
2109
|
# === Nested Attributes Examples
|
|
2111
2110
|
#
|
|
2112
2111
|
# When the object belonging to the current scope has a nested attribute
|
|
2113
|
-
# writer for a certain attribute, fields_for will yield a new scope
|
|
2112
|
+
# writer for a certain attribute, +fields_for+ will yield a new scope
|
|
2114
2113
|
# for that attribute. This allows you to create forms that set or change
|
|
2115
2114
|
# the attributes of a parent object and its associations in one go.
|
|
2116
2115
|
#
|
|
@@ -2141,7 +2140,7 @@ module ActionView
|
|
|
2141
2140
|
# end
|
|
2142
2141
|
# end
|
|
2143
2142
|
#
|
|
2144
|
-
# This model can now be used with a nested fields_for
|
|
2143
|
+
# This model can now be used with a nested +fields_for+, like so:
|
|
2145
2144
|
#
|
|
2146
2145
|
# <%= form_with model: @person do |person_form| %>
|
|
2147
2146
|
# ...
|
|
@@ -2177,7 +2176,7 @@ module ActionView
|
|
|
2177
2176
|
# ...
|
|
2178
2177
|
# <%= person_form.fields_for :address do |address_fields| %>
|
|
2179
2178
|
# ...
|
|
2180
|
-
# Delete: <%= address_fields.
|
|
2179
|
+
# Delete: <%= address_fields.checkbox :_destroy %>
|
|
2181
2180
|
# <% end %>
|
|
2182
2181
|
# ...
|
|
2183
2182
|
# <% end %>
|
|
@@ -2199,7 +2198,7 @@ module ActionView
|
|
|
2199
2198
|
# end
|
|
2200
2199
|
#
|
|
2201
2200
|
# Note that the <tt>projects_attributes=</tt> writer method is in fact
|
|
2202
|
-
# required for fields_for to correctly identify <tt>:projects</tt> as a
|
|
2201
|
+
# required for +fields_for+ to correctly identify <tt>:projects</tt> as a
|
|
2203
2202
|
# collection, and the correct indices to be set in the form markup.
|
|
2204
2203
|
#
|
|
2205
2204
|
# When projects is already an association on Person you can use
|
|
@@ -2210,8 +2209,8 @@ module ActionView
|
|
|
2210
2209
|
# accepts_nested_attributes_for :projects
|
|
2211
2210
|
# end
|
|
2212
2211
|
#
|
|
2213
|
-
# This model can now be used with a nested fields_for
|
|
2214
|
-
# the nested fields_for call will be repeated for each instance in the
|
|
2212
|
+
# This model can now be used with a nested +fields_for+. The block given to
|
|
2213
|
+
# the nested +fields_for+ call will be repeated for each instance in the
|
|
2215
2214
|
# collection:
|
|
2216
2215
|
#
|
|
2217
2216
|
# <%= form_with model: @person do |person_form| %>
|
|
@@ -2265,7 +2264,7 @@ module ActionView
|
|
|
2265
2264
|
# <%= form_with model: @person do |person_form| %>
|
|
2266
2265
|
# ...
|
|
2267
2266
|
# <%= person_form.fields_for :projects do |project_fields| %>
|
|
2268
|
-
# Delete: <%= project_fields.
|
|
2267
|
+
# Delete: <%= project_fields.checkbox :_destroy %>
|
|
2269
2268
|
# <% end %>
|
|
2270
2269
|
# ...
|
|
2271
2270
|
# <% end %>
|
|
@@ -2283,10 +2282,10 @@ module ActionView
|
|
|
2283
2282
|
# ...
|
|
2284
2283
|
# <% end %>
|
|
2285
2284
|
#
|
|
2286
|
-
# Note that fields_for will automatically generate a hidden field
|
|
2285
|
+
# Note that +fields_for+ will automatically generate a hidden field
|
|
2287
2286
|
# to store the ID of the record. There are circumstances where this
|
|
2288
2287
|
# hidden field is not needed and you can pass <tt>include_id: false</tt>
|
|
2289
|
-
# to prevent fields_for from rendering it automatically.
|
|
2288
|
+
# to prevent +fields_for+ from rendering it automatically.
|
|
2290
2289
|
def fields_for(record_name, record_object = nil, fields_options = nil, &block)
|
|
2291
2290
|
fields_options, record_object = record_object, nil if fields_options.nil? && record_object.is_a?(Hash) && record_object.extractable_options?
|
|
2292
2291
|
fields_options ||= {}
|
|
@@ -2444,7 +2443,7 @@ module ActionView
|
|
|
2444
2443
|
# within an array-like parameter, as in
|
|
2445
2444
|
#
|
|
2446
2445
|
# <%= fields_for "project[invoice_attributes][]", invoice, index: nil do |form| %>
|
|
2447
|
-
# <%= form.
|
|
2446
|
+
# <%= form.checkbox :paid %>
|
|
2448
2447
|
# ...
|
|
2449
2448
|
# <% end %>
|
|
2450
2449
|
#
|
|
@@ -2452,28 +2451,29 @@ module ActionView
|
|
|
2452
2451
|
# the elements of the array. For each item with a checked check box you
|
|
2453
2452
|
# get an extra ghost item with only that attribute, assigned to "0".
|
|
2454
2453
|
#
|
|
2455
|
-
# In that case it is preferable to either use
|
|
2454
|
+
# In that case it is preferable to either use FormTagHelper#checkbox_tag or to use
|
|
2456
2455
|
# hashes instead of arrays.
|
|
2457
2456
|
#
|
|
2458
2457
|
# ==== Examples
|
|
2459
2458
|
#
|
|
2460
2459
|
# # Let's say that @article.validated? is 1:
|
|
2461
|
-
#
|
|
2460
|
+
# checkbox("validated")
|
|
2462
2461
|
# # => <input name="article[validated]" type="hidden" value="0" />
|
|
2463
2462
|
# # <input checked="checked" type="checkbox" id="article_validated" name="article[validated]" value="1" />
|
|
2464
2463
|
#
|
|
2465
2464
|
# # Let's say that @puppy.gooddog is "no":
|
|
2466
|
-
#
|
|
2465
|
+
# checkbox("gooddog", {}, "yes", "no")
|
|
2467
2466
|
# # => <input name="puppy[gooddog]" type="hidden" value="no" />
|
|
2468
2467
|
# # <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" />
|
|
2469
2468
|
#
|
|
2470
2469
|
# # Let's say that @eula.accepted is "no":
|
|
2471
|
-
#
|
|
2470
|
+
# checkbox("accepted", { class: 'eula_check' }, "yes", "no")
|
|
2472
2471
|
# # => <input name="eula[accepted]" type="hidden" value="no" />
|
|
2473
2472
|
# # <input type="checkbox" class="eula_check" id="eula_accepted" name="eula[accepted]" value="yes" />
|
|
2474
|
-
def
|
|
2475
|
-
@template.
|
|
2473
|
+
def checkbox(method, options = {}, checked_value = "1", unchecked_value = "0")
|
|
2474
|
+
@template.checkbox(@object_name, method, objectify_options(options), checked_value, unchecked_value)
|
|
2476
2475
|
end
|
|
2476
|
+
alias_method :check_box, :checkbox
|
|
2477
2477
|
|
|
2478
2478
|
# Returns a radio button tag for accessing a specified attribute (identified by +method+) on an object
|
|
2479
2479
|
# assigned to the template (identified by +object+). If the current value of +method+ is +tag_value+ the
|
|
@@ -2525,7 +2525,7 @@ module ActionView
|
|
|
2525
2525
|
# hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
|
|
2526
2526
|
# shown.
|
|
2527
2527
|
#
|
|
2528
|
-
# Using this method inside a
|
|
2528
|
+
# Using this method inside a #form_with block will set the enclosing form's encoding to <tt>multipart/form-data</tt>.
|
|
2529
2529
|
#
|
|
2530
2530
|
# ==== Options
|
|
2531
2531
|
# * Creates standard HTML attributes for the tag.
|