Skip to content

Commit

Permalink
Fix Rails edge compatibility
Browse files Browse the repository at this point in the history
Close #941
  • Loading branch information
tagliala committed Aug 13, 2024
1 parent d404b98 commit cd4e067
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Metrics/MethodLength:
- 'test/**/*'

Metrics/ModuleLength:
Max: 101
Max: 109
Exclude:
- 'test/**/*'

Expand Down
13 changes: 12 additions & 1 deletion lib/client_side_validations/action_view/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand All @@ -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)
Expand Down Expand Up @@ -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|
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/action_view/cases/test_form_for_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/action_view/cases/test_form_with_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cd4e067

Please sign in to comment.