From b901f630cb2ab82c04482e959a99d2f3a4b9bb6e Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Tue, 13 Aug 2024 18:56:06 +0200 Subject: [PATCH] Fix Rails edge compatibility Close #941 --- .rubocop.yml | 2 +- .../action_view/form_builder.rb | 13 ++++++++++++- test/action_view/cases/test_form_for_helpers.rb | 2 +- test/action_view/cases/test_form_with_helpers.rb | 2 +- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 790124c1..8abb2cfe 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -49,7 +49,7 @@ Metrics/MethodLength: - 'test/**/*' Metrics/ModuleLength: - Max: 101 + Max: 109 Exclude: - 'test/**/*' diff --git a/lib/client_side_validations/action_view/form_builder.rb b/lib/client_side_validations/action_view/form_builder.rb index 2dbaceb5..6ac62537 100644 --- a/lib/client_side_validations/action_view/form_builder.rb +++ b/lib/client_side_validations/action_view/form_builder.rb @@ -5,7 +5,9 @@ module ActionView module Helpers module FormBuilder def self.prepended(base) - (base.field_helpers - %i[label check_box radio_button fields_for fields hidden_field file_field]).each do |selector| + selectors = base.field_helpers - %i[label check_box checkbox radio_button fields_for fields hidden_field file_field] + + selectors.each do |selector| base.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 # Cannot call super here, rewrite all def #{selector}(method, options = {}) # def text_field(method, options = {}) @@ -19,6 +21,10 @@ def #{selector}(method, options = {}) # def text_field(method, options = { end # end RUBY_EVAL end + + base.class_eval do + alias_method :text_area, :textarea if ::ActionView::Helpers::FormBuilder.field_helpers.include?(:textarea) + end end def initialize(object_name, object, template, options) @@ -47,6 +53,7 @@ def check_box(method, options = {}, checked_value = '1', unchecked_value = '0') options.delete(:validate) super end + alias checkbox check_box if ::ActionView::Helpers::FormBuilder.field_helpers.include?(:checkbox) %i[collection_check_boxes collection_radio_buttons].each do |method_name| define_method method_name do |method, collection, value_method, text_method, options = {}, html_options = {}, &block| @@ -56,6 +63,10 @@ def check_box(method, options = {}, checked_value = '1', unchecked_value = '0') end end + if ::ActionView::Helpers::FormBuilder.public_instance_methods.include?(:collection_checkboxes) + alias collection_checkboxes collection_check_boxes + end + def collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) build_validation_options(method, html_options.merge(name: options[:name])) html_options.delete(:validate) diff --git a/test/action_view/cases/test_form_for_helpers.rb b/test/action_view/cases/test_form_for_helpers.rb index 19053fdc..4f727529 100644 --- a/test/action_view/cases/test_form_for_helpers.rb +++ b/test/action_view/cases/test_form_for_helpers.rb @@ -456,7 +456,7 @@ def test_collection_check_boxes assert_dom_equal expected, output_buffer end - if ::ActionView::Helpers::FormBuilder.field_helpers.include?(:collection_checkboxes) + if ::ActionView::Helpers::FormBuilder.public_instance_methods.include?(:collection_checkboxes) def test_collection_checkboxes form_for(@post, validate: true) do |f| concat f.collection_checkboxes(:cost, [], :id, :name) diff --git a/test/action_view/cases/test_form_with_helpers.rb b/test/action_view/cases/test_form_with_helpers.rb index 8abeb0a4..9cf5a3c3 100644 --- a/test/action_view/cases/test_form_with_helpers.rb +++ b/test/action_view/cases/test_form_with_helpers.rb @@ -471,7 +471,7 @@ def test_form_with_collection_check_boxes assert_dom_equal expected, output_buffer end - if ::ActionView::Helpers::FormBuilder.field_helpers.include?(:collection_checkboxes) + if ::ActionView::Helpers::FormBuilder.public_instance_methods.include?(:collection_checkboxes) def test_form_with_collection_checkboxes form_with(model: @post, validate: true) do |f| concat f.collection_checkboxes(:cost, [], :id, :name)