Skip to content

Commit 0803405

Browse files
seanpdoylebyroot
authored andcommitted
Batch define FormBuilder methods with CodeGenerator
Define the `ActionView::Helpers::FormBuilder` methods that wrap the `@template` instance methods inside an `ActiveSupport::CodeGenerator.batch` call so that the underlying `class` extensions aren't invoked more than once.
1 parent fac9a03 commit 0803405

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

actionview/lib/action_view/helpers/form_helper.rb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require "action_view/helpers/active_model_helper"
88
require "action_view/model_naming"
99
require "action_view/record_identifier"
10+
require "active_support/code_generator"
1011
require "active_support/core_ext/module/attribute_accessors"
1112
require "active_support/core_ext/hash/slice"
1213
require "active_support/core_ext/string/output_safety"
@@ -2014,16 +2015,20 @@ def field_name(method, *methods, multiple: false, index: @options[:index])
20142015
#
20152016
# Please refer to the documentation of the base helper for details.
20162017

2017-
(field_helpers - [:label, :check_box, :radio_button, :fields_for, :fields, :hidden_field, :file_field]).each do |selector|
2018-
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
2019-
def #{selector}(method, options = {}) # def text_field(method, options = {})
2020-
@template.public_send( # @template.public_send(
2021-
#{selector.inspect}, # :text_field,
2022-
@object_name, # @object_name,
2023-
method, # method,
2024-
objectify_options(options)) # objectify_options(options))
2025-
end # end
2026-
RUBY_EVAL
2018+
ActiveSupport::CodeGenerator.batch(self, __FILE__, __LINE__) do |code_generator|
2019+
(field_helpers - [:label, :check_box, :radio_button, :fields_for, :fields, :hidden_field, :file_field]).each do |selector|
2020+
code_generator.define_cached_method(selector, namespace: :form_builder) do |batch|
2021+
batch.push <<-RUBY_EVAL
2022+
def #{selector}(method, options = {}) # def text_field(method, options = {})
2023+
@template.public_send( # @template.public_send(
2024+
#{selector.inspect}, # :text_field,
2025+
@object_name, # @object_name,
2026+
method, # method,
2027+
objectify_options(options)) # objectify_options(options))
2028+
end # end
2029+
RUBY_EVAL
2030+
end
2031+
end
20272032
end
20282033

20292034
# Creates a scope around a specific model object like form_for, but

0 commit comments

Comments
 (0)