Skip to content

Commit

Permalink
Fix undefined method `auto_html5_validation' for Model not inherit Ac…
Browse files Browse the repository at this point in the history
…tiveRecord::Base
  • Loading branch information
ykzts committed Jun 12, 2015
1 parent b8491da commit 7c1dda4
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/html5_validators/active_model/validations.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
module Html5Validators
module ActiveModelExtension
extend ActiveSupport::Concern

included do
cattr_accessor :auto_html5_validation, :instance_accessor => false, :instance_reader => false, :instance_writer => false
end
end
end

module ActiveModel
module Validations
attr_accessor :auto_html5_validation
end
end

ActiveModel::Validations.send(:include, Html5Validators::ActiveModelExtension)
47 changes: 47 additions & 0 deletions spec/fake_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,28 @@
get :new_with_required_true
end
end
resources :items, :only => [:new, :create] do
collection do
get :new_without_html5_validation
get :new_with_required_true
end
end
end

# models
class Person < ActiveRecord::Base
end
class Item
if ActiveModel::VERSION::STRING >= '4'
include ActiveModel::Model
else
include ActiveModel::Validations
include ActiveModel::Conversion
def persisted?; false; end
end

attr_accessor :name, :description
end

# controllers
class ApplicationController < ActionController::Base; end
Expand Down Expand Up @@ -57,6 +74,36 @@ def new_with_required_true
ERB
end
end
class ItemsController < ApplicationController
def new
@item = Item.new
render :inline => <<-ERB
<%= form_for @item do |f| %>
<%= f.text_field :name %>
<%= f.text_area :description %>
<% end %>
ERB
end

def new_without_html5_validation
@item = Item.new
render :inline => <<-ERB
<%= form_for @item, :auto_html5_validation => false do |f| %>
<%= f.text_field :name %>
<%= f.text_area :description %>
<% end %>
ERB
end

def new_with_required_true
@item = Item.new
render :inline => <<-ERB
<%= form_for @item do |f| %>
<%= f.text_field :name, :required => true %>
<% end %>
ERB
end
end

# helpers
module ApplicationHelper; end
Expand Down
88 changes: 88 additions & 0 deletions spec/features/validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,91 @@
end
end
end

feature 'item#new' do
context 'without validation' do
scenario 'new form' do
visit '/items/new'
page.should have_css('input#item_name')
page.should_not have_css('input#item_name[required=required]')
end

scenario 'new_without_html5_validation form' do
visit '/items/new_without_html5_validation'
page.should have_css('textarea#item_description')
page.should_not have_css('textarea#item_description[required=required]')
end
end

context 'with required validation' do
background do
Item.validates_presence_of :name, :description
end
after do
Item._validators.clear
end
scenario 'new form' do
visit '/items/new'

find('input#item_name')[:required].should == 'required'
find('textarea#item_description')[:required].should == 'required'
end
scenario 'new_without_html5_validation form' do
visit '/items/new_without_html5_validation'

find('input#item_name')[:required].should be_nil
end
scenario 'new_with_required_true form' do
visit '/items/new_with_required_true'

find('input#item_name')[:required].should == 'required'
end

context 'disabling html5_validation in class level' do
background do
Item.class_eval do |kls|
kls.auto_html5_validation = false
end
end
after do
Item.class_eval do |kls|
kls.auto_html5_validation = nil
end
end
scenario 'new form' do
visit '/items/new'

find('input#item_name')[:required].should be_nil
end
end

context 'disabling html5_validations in gem' do
background do
Html5Validators.enabled = false
end
after do
Html5Validators.enabled = true
end
scenario 'new form' do
visit '/items/new'

find('input#item_name')[:required].should be_nil
find('textarea#item_description')[:required].should be_nil
end
end
end

context 'with maxlength validation' do
background do
Item.validates_length_of :name, {:maximum => 20 }
Item.validates_length_of :description, {:maximum => 100}
end

scenario 'new form' do
visit '/items/new'

find('input#item_name')[:maxlength].should == '20'
find('textarea#item_description')[:maxlength].should == '100'
end
end
end

0 comments on commit 7c1dda4

Please sign in to comment.