Skip to content

Commit 4a3d8e8

Browse files
committed
Extract Rails 3 specific legacy code into a separate file
1 parent 4cd1f50 commit 4a3d8e8

File tree

3 files changed

+59
-43
lines changed

3 files changed

+59
-43
lines changed

lib/html5_validators.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ class Railtie < ::Rails::Railtie #:nodoc:
2222
require 'html5_validators/active_record/base'
2323
end
2424
ActiveSupport.on_load(:action_view) do
25-
require 'html5_validators/action_view/form_helpers'
25+
if ActionPack::VERSION::STRING >= '4'
26+
require 'html5_validators/action_view/form_helpers'
27+
else
28+
require 'html5_validators/action_view/form_helpers_rails3'
29+
end
2630
end
2731
end
2832
end

lib/html5_validators/action_view/form_helpers.rb

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def inject_maxlength_field
1212
end
1313
end
1414
end
15-
end if ActionPack::VERSION::STRING >= '4'
15+
end
1616

1717

1818
module ActionView
@@ -29,7 +29,6 @@ def form_for_with_auto_html5_validation_option(record, options = {}, &proc)
2929
alias_method_chain :form_for, :auto_html5_validation_option
3030
end
3131

32-
if ActionPack::VERSION::STRING >= '4'
3332
module Tags
3433
class Base #:nodoc:
3534
include Html5Validators::ActionViewExtension
@@ -70,45 +69,5 @@ def render_with_html5_attributes
7069
end
7170
end
7271
end
73-
# ActionPack::VERSION::STRING == '3'
74-
else
75-
class InstanceTag
76-
def to_input_field_tag_with_html5_attributes(field_type, options = {})
77-
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
78-
options["required"] ||= object.class.attribute_required?(method_name)
79-
options["maxlength"] ||= object.class.attribute_maxlength(method_name)
80-
options["max"] ||= object.class.attribute_max(method_name)
81-
options["min"] ||= object.class.attribute_min(method_name)
82-
end
83-
to_input_field_tag_without_html5_attributes field_type, options
84-
end
85-
alias_method_chain :to_input_field_tag, :html5_attributes
86-
87-
def to_text_area_tag_with_html5_attributes(options = {})
88-
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
89-
options["required"] ||= object.class.attribute_required?(method_name)
90-
options["maxlength"] ||= object.class.attribute_maxlength(method_name)
91-
end
92-
to_text_area_tag_without_html5_attributes options
93-
end
94-
alias_method_chain :to_text_area_tag, :html5_attributes
95-
96-
def to_radio_button_tag_with_html5_attributes(tag_value, options = {})
97-
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
98-
options["required"] ||= object.class.attribute_required?(method_name)
99-
end
100-
to_radio_button_tag_without_html5_attributes tag_value, options
101-
end
102-
alias_method_chain :to_radio_button_tag, :html5_attributes
103-
104-
def to_check_box_tag_with_html5_attributes(options = {}, checked_value = "1", unchecked_value = "0")
105-
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
106-
options["required"] ||= object.class.attribute_required?(method_name)
107-
end
108-
to_check_box_tag_without_html5_attributes options, checked_value, unchecked_value
109-
end
110-
alias_method_chain :to_check_box_tag, :html5_attributes
111-
end
112-
end
11372
end
11473
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module ActionView
2+
module Helpers
3+
module FormHelper
4+
def form_for_with_auto_html5_validation_option(record, options = {}, &proc)
5+
if record.respond_to?(:auto_html5_validation=)
6+
if !Html5Validators.enabled || (options[:auto_html5_validation] == false)
7+
record.auto_html5_validation = false
8+
end
9+
end
10+
form_for_without_auto_html5_validation_option record, options, &proc
11+
end
12+
alias_method_chain :form_for, :auto_html5_validation_option
13+
end
14+
15+
class InstanceTag
16+
def to_input_field_tag_with_html5_attributes(field_type, options = {})
17+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
18+
options["required"] ||= object.class.attribute_required?(method_name)
19+
options["maxlength"] ||= object.class.attribute_maxlength(method_name)
20+
options["max"] ||= object.class.attribute_max(method_name)
21+
options["min"] ||= object.class.attribute_min(method_name)
22+
end
23+
to_input_field_tag_without_html5_attributes field_type, options
24+
end
25+
alias_method_chain :to_input_field_tag, :html5_attributes
26+
27+
def to_text_area_tag_with_html5_attributes(options = {})
28+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
29+
options["required"] ||= object.class.attribute_required?(method_name)
30+
options["maxlength"] ||= object.class.attribute_maxlength(method_name)
31+
end
32+
to_text_area_tag_without_html5_attributes options
33+
end
34+
alias_method_chain :to_text_area_tag, :html5_attributes
35+
36+
def to_radio_button_tag_with_html5_attributes(tag_value, options = {})
37+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
38+
options["required"] ||= object.class.attribute_required?(method_name)
39+
end
40+
to_radio_button_tag_without_html5_attributes tag_value, options
41+
end
42+
alias_method_chain :to_radio_button_tag, :html5_attributes
43+
44+
def to_check_box_tag_with_html5_attributes(options = {}, checked_value = "1", unchecked_value = "0")
45+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
46+
options["required"] ||= object.class.attribute_required?(method_name)
47+
end
48+
to_check_box_tag_without_html5_attributes options, checked_value, unchecked_value
49+
end
50+
alias_method_chain :to_check_box_tag, :html5_attributes
51+
end
52+
end
53+
end

0 commit comments

Comments
 (0)