html5_validators 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -41,6 +41,7 @@ http://img.skitch.com/20110517-8sagqrkjnmkinapmcc5tduy2b8.jpg
41
41
 
42
42
  View:
43
43
  <%= f.text_field :name %>
44
+ text_area is also available
44
45
 
45
46
  HTML:
46
47
  <input id="user_name" maxlength="10" name="user[name]" size="10" type="text" />
@@ -5,6 +5,12 @@ module Html5Validators
5
5
  @options["required"] ||= object.class.attribute_required?(@method_name)
6
6
  end
7
7
  end
8
+
9
+ def inject_maxlength_field
10
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
11
+ @options["maxlength"] ||= object.class.attribute_maxlength(@method_name)
12
+ end
13
+ end
8
14
  end
9
15
  end if ActionPack::VERSION::STRING >= '4'
10
16
 
@@ -28,9 +34,9 @@ module ActionView
28
34
  class TextField
29
35
  def render_with_html5_attributes
30
36
  inject_required_field
37
+ inject_maxlength_field
31
38
 
32
39
  if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
33
- @options["maxlength"] ||= object.class.attribute_maxlength(@method_name)
34
40
  @options["max"] ||= object.class.attribute_max(@method_name)
35
41
  @options["min"] ||= object.class.attribute_min(@method_name)
36
42
  end
@@ -39,8 +45,17 @@ module ActionView
39
45
  alias_method_chain :render, :html5_attributes
40
46
  end
41
47
 
48
+ class TextArea
49
+ def render_with_html5_attributes
50
+ inject_required_field
51
+ inject_maxlength_field
52
+
53
+ render_without_html5_attributes
54
+ end
55
+ end
56
+
42
57
  #TODO probably I have to add some more classes here
43
- [TextArea, RadioButton, CheckBox, Select, DateSelect, TimeZoneSelect].each do |kls|
58
+ [RadioButton, CheckBox, Select, DateSelect, TimeZoneSelect].each do |kls|
44
59
  kls.class_eval do
45
60
  def render_with_html5_attributes
46
61
  inject_required_field
@@ -67,6 +82,7 @@ module ActionView
67
82
  def to_text_area_tag_with_html5_attributes(options = {})
68
83
  if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
69
84
  options["required"] ||= object.class.attribute_required?(method_name)
85
+ options["maxlength"] ||= object.class.attribute_maxlength(method_name)
70
86
  end
71
87
  to_text_area_tag_without_html5_attributes options
72
88
  end
@@ -1,3 +1,3 @@
1
1
  module Html5Validators
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
data/spec/fake_app.rb CHANGED
@@ -31,6 +31,7 @@ class PeopleController < ApplicationController
31
31
  render :inline => <<-ERB
32
32
  <%= form_for @person do |f| %>
33
33
  <%= f.text_field :name %>
34
+ <%= f.text_area :bio %>
34
35
  <% end %>
35
36
  ERB
36
37
  end
@@ -52,6 +53,11 @@ module ApplicationHelper; end
52
53
  #migrations
53
54
  class CreateAllTables < ActiveRecord::Migration
54
55
  def self.up
55
- create_table(:people) {|t| t.string :name; t.string :email; t.integer :age}
56
+ create_table :people do |t|
57
+ t.string :name
58
+ t.string :email
59
+ t.integer :age
60
+ t.text :bio
61
+ end
56
62
  end
57
63
  end
@@ -17,20 +17,21 @@ feature 'person#new' do
17
17
 
18
18
  context 'with required validation' do
19
19
  background do
20
- Person.validates_presence_of :name
20
+ Person.validates_presence_of :name, :bio
21
21
  end
22
22
  after do
23
23
  Person._validators.clear
24
24
  end
25
25
  scenario 'new form' do
26
26
  visit '/people/new'
27
- page.should have_css('input#person_name')
28
- page.should have_css('input#person_name[required=required]')
27
+
28
+ find('input#person_name')[:required].should == 'required'
29
+ find('textarea#person_bio')[:required].should == 'required'
29
30
  end
30
31
  scenario 'new_without_html5_validation form' do
31
32
  visit '/people/new_without_html5_validation'
32
- page.should have_css('input#person_name')
33
- page.should_not have_css('input#person_name[required=required]')
33
+
34
+ find('input#person_name')[:required].should be_nil
34
35
  end
35
36
 
36
37
  context 'disabling html5_validation in class level' do
@@ -46,9 +47,23 @@ feature 'person#new' do
46
47
  end
47
48
  scenario 'new form' do
48
49
  visit '/people/new'
49
- page.should have_css('input#person_name')
50
- page.should_not have_css('input#person_name[required=required]')
50
+
51
+ find('input#person_name')[:required].should be_nil
51
52
  end
52
53
  end
53
54
  end
55
+
56
+ context 'with maxlength validation' do
57
+ background do
58
+ Person.validates_length_of :name, {:maximum => 20}
59
+ Person.validates_length_of :bio, {:maximum => 100}
60
+ end
61
+
62
+ scenario 'new form' do
63
+ visit '/people/new'
64
+
65
+ find('input#person_name')[:maxlength].should == '20'
66
+ find('textarea#person_bio')[:maxlength].should == '100'
67
+ end
68
+ end
54
69
  end
data/spec/spec_helper.rb CHANGED
@@ -12,6 +12,7 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
12
12
 
13
13
  RSpec.configure do |config|
14
14
  config.before :all do
15
+ ActiveRecord::Migration.verbose = false
15
16
  CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'people'
16
17
  end
17
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html5_validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-15 00:00:00.000000000 Z
12
+ date: 2012-06-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70174708849260 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70174708849260
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: rspec-rails
27
- requirement: &70174708848720 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *70174708848720
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: capybara
38
- requirement: &70174708848200 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *70174708848200
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: sqlite3
49
- requirement: &70174708847560 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,7 +69,12 @@ dependencies:
54
69
  version: '0'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *70174708847560
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  description: A gem/plugin for Rails 3 that enables client-side validation using ActiveModel
59
79
  + HTML5
60
80
  email:
@@ -100,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
120
  version: '0'
101
121
  requirements: []
102
122
  rubyforge_project: html5_validators
103
- rubygems_version: 1.8.16
123
+ rubygems_version: 1.8.24
104
124
  signing_key:
105
125
  specification_version: 3
106
126
  summary: Automatic client side validation using HTML5 Form Validation