Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

`auto_html5_validation` can also be set via models' class method

  • Loading branch information...
commit 107bffc3ed39e6eb34ad9fe0c814457a1065cb61 1 parent b46175a
@amatsuda authored
View
1  lib/html5_validators.rb
@@ -9,6 +9,7 @@ class Railtie < ::Rails::Railtie #:nodoc:
if (Rails.version < '3.1.0.beta2') && (Rails.version != '3.1.0')
require 'html5_validators/active_model/initializer_monkey_patches'
end
+ require 'html5_validators/active_record/base'
end
ActiveSupport.on_load(:action_view) do
require 'html5_validators/action_view/form_helpers'
View
12 lib/html5_validators/action_view/form_helpers.rb
@@ -1,7 +1,7 @@
module Html5Validators
module ActionViewExtension
def inject_required_field
- if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false)
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
@options["required"] ||= object.class.attribute_required?(@method_name)
end
end
@@ -29,7 +29,7 @@ class TextField
def render_with_html5_attributes
inject_required_field
- if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false)
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
@options["maxlength"] ||= object.class.attribute_maxlength(@method_name)
@options["max"] ||= object.class.attribute_max(@method_name)
@options["min"] ||= object.class.attribute_min(@method_name)
@@ -54,7 +54,7 @@ def render_with_html5_attributes
else
class InstanceTag
def to_input_field_tag_with_html5_attributes(field_type, options = {})
- if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false)
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
options["required"] ||= object.class.attribute_required?(method_name)
options["maxlength"] ||= object.class.attribute_maxlength(method_name)
options["max"] ||= object.class.attribute_max(method_name)
@@ -65,7 +65,7 @@ def to_input_field_tag_with_html5_attributes(field_type, options = {})
alias_method_chain :to_input_field_tag, :html5_attributes
def to_text_area_tag_with_html5_attributes(options = {})
- if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false)
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
options["required"] ||= object.class.attribute_required?(method_name)
end
to_text_area_tag_without_html5_attributes options
@@ -73,7 +73,7 @@ def to_text_area_tag_with_html5_attributes(options = {})
alias_method_chain :to_text_area_tag, :html5_attributes
def to_radio_button_tag_with_html5_attributes(tag_value, options = {})
- if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false)
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
options["required"] ||= object.class.attribute_required?(method_name)
end
to_radio_button_tag_without_html5_attributes tag_value, options
@@ -81,7 +81,7 @@ def to_radio_button_tag_with_html5_attributes(tag_value, options = {})
alias_method_chain :to_radio_button_tag, :html5_attributes
def to_check_box_tag_with_html5_attributes(options = {}, checked_value = "1", unchecked_value = "0")
- if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false)
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
options["required"] ||= object.class.attribute_required?(method_name)
end
to_check_box_tag_without_html5_attributes options, checked_value, unchecked_value
View
19 lib/html5_validators/active_record/base.rb
@@ -0,0 +1,19 @@
+module ActiveRecord
+ class Base
+ # Future subclasses will pick up the model extension
+ class << self
+ def inherited_with_html5_validation(kls) #:nodoc:
+ inherited_without_html5_validation kls
+ kls.class_eval do
+ cattr_accessor :auto_html5_validation, :instance_accessor => false
+ end if kls.superclass == ActiveRecord::Base
+ end
+ alias_method_chain :inherited, :html5_validation
+ end
+
+ # Existing subclasses pick up the model extension as well
+ self.descendants.each do |kls|
+ cattr_accessor :auto_html5_validation, :instance_accessor => false if kls.superclass == ActiveRecord::Base
+ end
+ end
+end
View
18 spec/requests/validation_spec.rb
@@ -32,5 +32,23 @@
page.should have_css('input#person_name')
page.should_not have_css('input#person_name[required=required]')
end
+
+ context 'disabling html5_validation in class level' do
+ background do
+ Person.class_eval do |kls|
+ kls.auto_html5_validation = false
+ end
+ end
+ after do
+ Person.class_eval do |kls|
+ kls.auto_html5_validation = nil
+ end
+ end
+ scenario 'new form' do
+ visit '/people/new'
+ page.should have_css('input#person_name')
+ page.should_not have_css('input#person_name[required=required]')
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.