Skip to content

Commit 47e1fb1

Browse files
committed
Merge pull request #14 from ykzts/fix-use-activemodel
Fix undefined method `auto_html5_validation' for Model not inherit ActiveRecord::Base fixes #6, #10
2 parents 1b50bb5 + 7c1dda4 commit 47e1fb1

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1+
module Html5Validators
2+
module ActiveModelExtension
3+
extend ActiveSupport::Concern
4+
5+
included do
6+
cattr_accessor :auto_html5_validation, :instance_accessor => false, :instance_reader => false, :instance_writer => false
7+
end
8+
end
9+
end
10+
111
module ActiveModel
212
module Validations
313
attr_accessor :auto_html5_validation
414
end
515
end
16+
17+
ActiveModel::Validations.send(:include, Html5Validators::ActiveModelExtension)

spec/fake_app.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,28 @@
1919
get :new_with_required_true
2020
end
2121
end
22+
resources :items, :only => [:new, :create] do
23+
collection do
24+
get :new_without_html5_validation
25+
get :new_with_required_true
26+
end
27+
end
2228
end
2329

2430
# models
2531
class Person < ActiveRecord::Base
2632
end
33+
class Item
34+
if ActiveModel::VERSION::STRING >= '4'
35+
include ActiveModel::Model
36+
else
37+
include ActiveModel::Validations
38+
include ActiveModel::Conversion
39+
def persisted?; false; end
40+
end
41+
42+
attr_accessor :name, :description
43+
end
2744

2845
# controllers
2946
class ApplicationController < ActionController::Base; end
@@ -57,6 +74,36 @@ def new_with_required_true
5774
ERB
5875
end
5976
end
77+
class ItemsController < ApplicationController
78+
def new
79+
@item = Item.new
80+
render :inline => <<-ERB
81+
<%= form_for @item do |f| %>
82+
<%= f.text_field :name %>
83+
<%= f.text_area :description %>
84+
<% end %>
85+
ERB
86+
end
87+
88+
def new_without_html5_validation
89+
@item = Item.new
90+
render :inline => <<-ERB
91+
<%= form_for @item, :auto_html5_validation => false do |f| %>
92+
<%= f.text_field :name %>
93+
<%= f.text_area :description %>
94+
<% end %>
95+
ERB
96+
end
97+
98+
def new_with_required_true
99+
@item = Item.new
100+
render :inline => <<-ERB
101+
<%= form_for @item do |f| %>
102+
<%= f.text_field :name, :required => true %>
103+
<% end %>
104+
ERB
105+
end
106+
end
60107

61108
# helpers
62109
module ApplicationHelper; end

spec/features/validation_spec.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,91 @@
8787
end
8888
end
8989
end
90+
91+
feature 'item#new' do
92+
context 'without validation' do
93+
scenario 'new form' do
94+
visit '/items/new'
95+
page.should have_css('input#item_name')
96+
page.should_not have_css('input#item_name[required=required]')
97+
end
98+
99+
scenario 'new_without_html5_validation form' do
100+
visit '/items/new_without_html5_validation'
101+
page.should have_css('textarea#item_description')
102+
page.should_not have_css('textarea#item_description[required=required]')
103+
end
104+
end
105+
106+
context 'with required validation' do
107+
background do
108+
Item.validates_presence_of :name, :description
109+
end
110+
after do
111+
Item._validators.clear
112+
end
113+
scenario 'new form' do
114+
visit '/items/new'
115+
116+
find('input#item_name')[:required].should == 'required'
117+
find('textarea#item_description')[:required].should == 'required'
118+
end
119+
scenario 'new_without_html5_validation form' do
120+
visit '/items/new_without_html5_validation'
121+
122+
find('input#item_name')[:required].should be_nil
123+
end
124+
scenario 'new_with_required_true form' do
125+
visit '/items/new_with_required_true'
126+
127+
find('input#item_name')[:required].should == 'required'
128+
end
129+
130+
context 'disabling html5_validation in class level' do
131+
background do
132+
Item.class_eval do |kls|
133+
kls.auto_html5_validation = false
134+
end
135+
end
136+
after do
137+
Item.class_eval do |kls|
138+
kls.auto_html5_validation = nil
139+
end
140+
end
141+
scenario 'new form' do
142+
visit '/items/new'
143+
144+
find('input#item_name')[:required].should be_nil
145+
end
146+
end
147+
148+
context 'disabling html5_validations in gem' do
149+
background do
150+
Html5Validators.enabled = false
151+
end
152+
after do
153+
Html5Validators.enabled = true
154+
end
155+
scenario 'new form' do
156+
visit '/items/new'
157+
158+
find('input#item_name')[:required].should be_nil
159+
find('textarea#item_description')[:required].should be_nil
160+
end
161+
end
162+
end
163+
164+
context 'with maxlength validation' do
165+
background do
166+
Item.validates_length_of :name, {:maximum => 20 }
167+
Item.validates_length_of :description, {:maximum => 100}
168+
end
169+
170+
scenario 'new form' do
171+
visit '/items/new'
172+
173+
find('input#item_name')[:maxlength].should == '20'
174+
find('textarea#item_description')[:maxlength].should == '100'
175+
end
176+
end
177+
end

0 commit comments

Comments
 (0)