From ba725db09d35d93df74de4fcf4ab14bddd30fc66 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Fri, 10 Oct 2025 11:09:07 -0400 Subject: [PATCH] Fix undefined method `add_default_name_and_id` This resolves https://github.com/thoughtbot/form_props/issues/26, by using Rails 8 methods, but aliasing for Rails 7 compatibility. A CI run should update to 8.0.3 where the error happened, and test older for Rails 7. --- lib/form_props/inputs/base.rb | 8 +++++++- lib/form_props/inputs/check_box.rb | 4 ++-- lib/form_props/inputs/radio_button.rb | 2 +- lib/form_props/inputs/text_area.rb | 2 +- lib/form_props/inputs/text_field.rb | 2 +- lib/form_props/select_renderer.rb | 2 +- 6 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/form_props/inputs/base.rb b/lib/form_props/inputs/base.rb index aa407f9..cce854d 100644 --- a/lib/form_props/inputs/base.rb +++ b/lib/form_props/inputs/base.rb @@ -3,6 +3,12 @@ module FormProps module Inputs class Base < ::ActionView::Helpers::Tags::Base + # Remove when no longer supporting Rails 7 + if ActionView::VERSION::STRING < "8" + alias_method :add_default_name_and_field_for_value, :add_default_name_and_id_for_value + alias_method :add_default_name_and_field, :add_default_name_and_id + end + def json @json ||= @template_object.instance_variable_get(:@__json) end @@ -13,7 +19,7 @@ def initialize(object_name, method_name, template_object, options = {}) @controlled = options.delete(:controlled) @key = options.delete(:key) - super(object_name, method_name, template_object, options) + super end private diff --git a/lib/form_props/inputs/check_box.rb b/lib/form_props/inputs/check_box.rb index 3678f67..8aa1717 100644 --- a/lib/form_props/inputs/check_box.rb +++ b/lib/form_props/inputs/check_box.rb @@ -28,10 +28,10 @@ def render(flatten = false) body_block = -> { if options[:multiple] - add_default_name_and_id_for_value(@checked_value, options) + add_default_name_and_field_for_value(@checked_value, options) options.delete(:multiple) else - add_default_name_and_id(options) + add_default_name_and_field(options) end input_props(options) diff --git a/lib/form_props/inputs/radio_button.rb b/lib/form_props/inputs/radio_button.rb index 022604c..65e94d8 100644 --- a/lib/form_props/inputs/radio_button.rb +++ b/lib/form_props/inputs/radio_button.rb @@ -23,7 +23,7 @@ def render(flatten = false) @options[:checked] = true if input_checked?(@options) body_block = -> { - add_default_name_and_id_for_value(@tag_value, @options) + add_default_name_and_field_for_value(@tag_value, @options) input_props(@options) } diff --git a/lib/form_props/inputs/text_area.rb b/lib/form_props/inputs/text_area.rb index 785113d..b32b1f2 100644 --- a/lib/form_props/inputs/text_area.rb +++ b/lib/form_props/inputs/text_area.rb @@ -9,7 +9,7 @@ class TextArea < Base def render json.set!(sanitized_key) do - add_default_name_and_id(@options) + add_default_name_and_field(@options) @options[:type] ||= field_type @options[:value] = @options.fetch(:value) { value_before_type_cast } diff --git a/lib/form_props/inputs/text_field.rb b/lib/form_props/inputs/text_field.rb index 06a1bf3..352842a 100644 --- a/lib/form_props/inputs/text_field.rb +++ b/lib/form_props/inputs/text_field.rb @@ -13,7 +13,7 @@ def render @options[:value] = @options.fetch(:value) { value_before_type_cast } unless field_type == "file" json.set!(sanitized_key) do - add_default_name_and_id(@options) + add_default_name_and_field(@options) input_props(@options) end end diff --git a/lib/form_props/select_renderer.rb b/lib/form_props/select_renderer.rb index 7e36b38..e0f25f6 100644 --- a/lib/form_props/select_renderer.rb +++ b/lib/form_props/select_renderer.rb @@ -23,7 +23,7 @@ def add_options(option_tags, options, value = nil) def select_content_props(option_tags, options, html_options) html_options = html_options.stringify_keys - add_default_name_and_id(html_options) + add_default_name_and_field(html_options) if placeholder_required?(html_options) raise ArgumentError, "include_blank cannot be false for a required field." if options[:include_blank] == false