Skip to content

Commit 13a1be5

Browse files
committed
AMC => Module#prepend
1 parent d7a09f2 commit 13a1be5

File tree

1 file changed

+38
-48
lines changed

1 file changed

+38
-48
lines changed

lib/html5_validators/action_view/form_helpers.rb

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,66 @@
11
module Html5Validators
22
module ActionViewExtension
3-
def inject_required_attribute
4-
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
5-
@options["required"] ||= @options[:required] || object.class.attribute_required?(@method_name)
3+
module FormHelper
4+
def form_for(record, options = {}, &block)
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+
super
611
end
712
end
813

9-
def inject_maxlength_attribute
10-
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
11-
@options["maxlength"] ||= @options[:maxlength] || object.class.attribute_maxlength(@method_name)
14+
module PresenceValidator
15+
def render
16+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
17+
@options["required"] ||= @options[:required] || object.class.attribute_required?(@method_name)
18+
end
19+
super
1220
end
1321
end
1422

15-
def inject_numericality_attributes
16-
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
17-
@options["max"] ||= @options["max"] || @options[:max] || object.class.attribute_max(@method_name)
18-
@options["min"] ||= @options["min"] || @options[:min] || object.class.attribute_min(@method_name)
23+
module LengthValidator
24+
def render
25+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
26+
@options["maxlength"] ||= @options[:maxlength] || object.class.attribute_maxlength(@method_name)
27+
end
28+
super
29+
end
30+
end
31+
32+
module NumericalityValidator
33+
def render
34+
if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
35+
@options["max"] ||= @options["max"] || @options[:max] || object.class.attribute_max(@method_name)
36+
@options["min"] ||= @options["min"] || @options[:min] || object.class.attribute_min(@method_name)
37+
end
38+
super
1939
end
2040
end
2141
end
2242
end
2343

2444

2545
module ActionView
26-
module Helpers
27-
module FormHelper
28-
def form_for_with_auto_html5_validation_option(record, options = {}, &proc)
29-
if record.respond_to?(:auto_html5_validation=)
30-
if !Html5Validators.enabled || (options[:auto_html5_validation] == false)
31-
record.auto_html5_validation = false
32-
end
33-
end
34-
form_for_without_auto_html5_validation_option record, options, &proc
35-
end
36-
alias_method_chain :form_for, :auto_html5_validation_option
37-
end
46+
Base.send :prepend, Html5Validators::ActionViewExtension::FormHelper
3847

48+
module Helpers
3949
module Tags
40-
class Base #:nodoc:
41-
include Html5Validators::ActionViewExtension
42-
end
43-
4450
class TextField
45-
def render_with_html5_attributes
46-
inject_required_attribute
47-
inject_maxlength_attribute
48-
inject_numericality_attributes
49-
50-
render_without_html5_attributes
51-
end
52-
alias_method_chain :render, :html5_attributes
51+
prepend Html5Validators::ActionViewExtension::NumericalityValidator
52+
prepend Html5Validators::ActionViewExtension::LengthValidator
53+
prepend Html5Validators::ActionViewExtension::PresenceValidator
5354
end
5455

5556
class TextArea
56-
def render_with_html5_attributes
57-
inject_required_attribute
58-
inject_maxlength_attribute
59-
60-
render_without_html5_attributes
61-
end
62-
alias_method_chain :render, :html5_attributes
57+
prepend Html5Validators::ActionViewExtension::LengthValidator
58+
prepend Html5Validators::ActionViewExtension::PresenceValidator
6359
end
6460

6561
#TODO probably I have to add some more classes here
6662
[RadioButton, CheckBox, Select, DateSelect, TimeZoneSelect].each do |kls|
67-
kls.class_eval do
68-
def render_with_html5_attributes
69-
inject_required_attribute
70-
render_without_html5_attributes
71-
end
72-
alias_method_chain :render, :html5_attributes
73-
end
63+
kls.send :prepend, Html5Validators::ActionViewExtension::PresenceValidator
7464
end
7565
end
7666
end

0 commit comments

Comments
 (0)