Skip to content

Commit d7a09f2

Browse files
committed
Still we're supporting legacy Ruby that doesn't have Module#prepend...
1 parent 0b17230 commit d7a09f2

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

lib/html5_validators.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ class Railtie < ::Rails::Railtie #:nodoc:
2323
end
2424
ActiveSupport.on_load(:action_view) do
2525
if ActionPack::VERSION::STRING >= '4'
26-
require 'html5_validators/action_view/form_helpers'
26+
if RUBY_VERSION > '2'
27+
require 'html5_validators/action_view/form_helpers'
28+
else
29+
require 'html5_validators/action_view/form_helpers_ruby1'
30+
end
2731
else
2832
require 'html5_validators/action_view/form_helpers_rails3'
2933
end
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Legacy Ruby 1.x on Rails 4.x support
2+
module Html5Validators
3+
module ActionViewExtension
4+
def inject_required_attribute
5+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
6+
@options["required"] ||= @options[:required] || object.class.attribute_required?(@method_name)
7+
end
8+
end
9+
10+
def inject_maxlength_attribute
11+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
12+
@options["maxlength"] ||= @options[:maxlength] || object.class.attribute_maxlength(@method_name)
13+
end
14+
end
15+
16+
def inject_numericality_attributes
17+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
18+
@options["max"] ||= @options["max"] || @options[:max] || object.class.attribute_max(@method_name)
19+
@options["min"] ||= @options["min"] || @options[:min] || object.class.attribute_min(@method_name)
20+
end
21+
end
22+
end
23+
end
24+
25+
26+
module ActionView
27+
module Helpers
28+
module FormHelper
29+
def form_for_with_auto_html5_validation_option(record, options = {}, &proc)
30+
if record.respond_to?(:auto_html5_validation=)
31+
if !Html5Validators.enabled || (options[:auto_html5_validation] == false)
32+
record.auto_html5_validation = false
33+
end
34+
end
35+
form_for_without_auto_html5_validation_option record, options, &proc
36+
end
37+
alias_method_chain :form_for, :auto_html5_validation_option
38+
end
39+
40+
module Tags
41+
class Base #:nodoc:
42+
include Html5Validators::ActionViewExtension
43+
end
44+
45+
class TextField
46+
def render_with_html5_attributes
47+
inject_required_attribute
48+
inject_maxlength_attribute
49+
inject_numericality_attributes
50+
51+
render_without_html5_attributes
52+
end
53+
alias_method_chain :render, :html5_attributes
54+
end
55+
56+
class TextArea
57+
def render_with_html5_attributes
58+
inject_required_attribute
59+
inject_maxlength_attribute
60+
61+
render_without_html5_attributes
62+
end
63+
alias_method_chain :render, :html5_attributes
64+
end
65+
66+
#TODO probably I have to add some more classes here
67+
[RadioButton, CheckBox, Select, DateSelect, TimeZoneSelect].each do |kls|
68+
kls.class_eval do
69+
def render_with_html5_attributes
70+
inject_required_attribute
71+
render_without_html5_attributes
72+
end
73+
alias_method_chain :render, :html5_attributes
74+
end
75+
end
76+
end
77+
end
78+
end

0 commit comments

Comments
 (0)