html5_validators 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,5 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ tmp
6
+ log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=d
data/README.rdoc CHANGED
@@ -5,7 +5,8 @@ Automatic client-side validation using HTML5 Form Validation
5
5
 
6
6
  == What is this?
7
7
 
8
- html5_validators is a gem/plugin for Rails 3 that enables client-side validation using ActiveModel + HTML5
8
+ html5_validators is a gem/plugin for Rails 3 that enables client-side validation using ActiveModel + HTML5.
9
+ Once you bundle this gem on your app, the gem will automatically translate your model validation code into HTML5 validation attributes on every `form_for` invocation unless you explicitly cancel it.
9
10
 
10
11
 
11
12
  == Features
@@ -28,7 +29,6 @@ html5_validators is a gem/plugin for Rails 3 that enables client-side validation
28
29
  SPEC:
29
30
  http://dev.w3.org/html5/spec/Overview.html#attr-input-required
30
31
 
31
- SCREENSHOTS:
32
32
  http://img.skitch.com/20110517-8sagqrkjnmkinapmcc5tduy2b8.jpg
33
33
 
34
34
  === LengthValidator => maxlength
@@ -66,13 +66,55 @@ http://img.skitch.com/20110517-8sagqrkjnmkinapmcc5tduy2b8.jpg
66
66
  http://dev.w3.org/html5/spec/Overview.html#attr-input-max
67
67
  http://dev.w3.org/html5/spec/Overview.html#attr-input-min
68
68
 
69
- SCREENSHOT:
70
69
  http://img.skitch.com/20110516-n3jhu5m4gan8iy1j8er1qb7yfa.jpg
71
70
 
72
71
 
73
72
  * And more (coming soon...?)
74
73
 
75
74
 
75
+ == Disabling automatic client-side validation
76
+
77
+ There are three ways to cancel the automatic HTML5 validation
78
+
79
+ * form_for option
80
+
81
+ Set `auto_html5_validation: false` to `form_for` parameter
82
+
83
+ View:
84
+ <%= form_for @user, :auto_html5_validation => false do |f| %>
85
+ ...
86
+ <% end %>
87
+
88
+ * model attribute
89
+
90
+ Set `auto_html5_validation = false` attribute to ActiveModelish object
91
+
92
+ Controller:
93
+ @user = User.new auto_html5_validation: false
94
+
95
+ View:
96
+ <%= form_for @user do |f| %>
97
+ ...
98
+ <% end %>
99
+
100
+ * model class configuration
101
+
102
+ Write `auto_html5_validation = false` in ActiveModelish class
103
+
104
+ Model:
105
+ class User < ActiveRecord::Base
106
+ auto_html5_validation = false
107
+ end
108
+
109
+ Controller:
110
+ @user = User.new
111
+
112
+ View:
113
+ <%= form_for @user do |f| %>
114
+ ...
115
+ <% end %>
116
+
117
+
76
118
  == Supported versions
77
119
 
78
120
  * Ruby 1.8.7, 1.9.2, 1.9.3, 2.0 (trunk)
@@ -98,7 +140,6 @@ When accessed by an HTML5 incompatible lagacy browser, these extra attributes wi
98
140
 
99
141
  == Todo
100
142
 
101
- * specs
102
143
  * more validations
103
144
 
104
145
 
data/Rakefile CHANGED
@@ -1,2 +1,10 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+
7
+ task :noop do; end
8
+ task :default => :spec
9
+
10
+ RSpec::Core::RakeTask.new(:spec => :noop)
@@ -18,4 +18,8 @@ Gem::Specification.new do |s|
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ['lib']
21
+ s.add_development_dependency 'activerecord'
22
+ s.add_development_dependency 'rspec-rails'
23
+ s.add_development_dependency 'capybara'
24
+ s.add_development_dependency 'sqlite3'
21
25
  end
@@ -1,7 +1,7 @@
1
1
  module Html5Validators
2
2
  module ActionViewExtension
3
3
  def inject_required_field
4
- if object.class.ancestors.include?(ActiveModel::Validations)
4
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
5
5
  @options["required"] ||= object.class.attribute_required?(@method_name)
6
6
  end
7
7
  end
@@ -11,6 +11,14 @@ end if ActionPack::VERSION::STRING >= '4'
11
11
 
12
12
  module ActionView
13
13
  module Helpers
14
+ module FormHelper
15
+ def form_for_with_auto_html5_validation_option(record, options = {}, &proc)
16
+ record.auto_html5_validation = false if (options[:auto_html5_validation] == false) && (record.respond_to? :auto_html5_validation=)
17
+ form_for_without_auto_html5_validation_option record, options, &proc
18
+ end
19
+ alias_method_chain :form_for, :auto_html5_validation_option
20
+ end
21
+
14
22
  if ActionPack::VERSION::STRING >= '4'
15
23
  module Tags
16
24
  class Base #:nodoc:
@@ -21,7 +29,7 @@ module ActionView
21
29
  def render_with_html5_attributes
22
30
  inject_required_field
23
31
 
24
- if object.class.ancestors.include?(ActiveModel::Validations)
32
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
25
33
  @options["maxlength"] ||= object.class.attribute_maxlength(@method_name)
26
34
  @options["max"] ||= object.class.attribute_max(@method_name)
27
35
  @options["min"] ||= object.class.attribute_min(@method_name)
@@ -46,7 +54,7 @@ module ActionView
46
54
  else
47
55
  class InstanceTag
48
56
  def to_input_field_tag_with_html5_attributes(field_type, options = {})
49
- if object.class.ancestors.include?(ActiveModel::Validations)
57
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
50
58
  options["required"] ||= object.class.attribute_required?(method_name)
51
59
  options["maxlength"] ||= object.class.attribute_maxlength(method_name)
52
60
  options["max"] ||= object.class.attribute_max(method_name)
@@ -57,7 +65,7 @@ module ActionView
57
65
  alias_method_chain :to_input_field_tag, :html5_attributes
58
66
 
59
67
  def to_text_area_tag_with_html5_attributes(options = {})
60
- if object.class.ancestors.include?(ActiveModel::Validations)
68
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
61
69
  options["required"] ||= object.class.attribute_required?(method_name)
62
70
  end
63
71
  to_text_area_tag_without_html5_attributes options
@@ -65,7 +73,7 @@ module ActionView
65
73
  alias_method_chain :to_text_area_tag, :html5_attributes
66
74
 
67
75
  def to_radio_button_tag_with_html5_attributes(tag_value, options = {})
68
- if object.class.ancestors.include?(ActiveModel::Validations)
76
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
69
77
  options["required"] ||= object.class.attribute_required?(method_name)
70
78
  end
71
79
  to_radio_button_tag_without_html5_attributes tag_value, options
@@ -73,7 +81,7 @@ module ActionView
73
81
  alias_method_chain :to_radio_button_tag, :html5_attributes
74
82
 
75
83
  def to_check_box_tag_with_html5_attributes(options = {}, checked_value = "1", unchecked_value = "0")
76
- if object.class.ancestors.include?(ActiveModel::Validations)
84
+ if object.class.ancestors.include?(ActiveModel::Validations) && (object.auto_html5_validation != false) && (object.class.auto_html5_validation != false)
77
85
  options["required"] ||= object.class.attribute_required?(method_name)
78
86
  end
79
87
  to_check_box_tag_without_html5_attributes options, checked_value, unchecked_value
@@ -0,0 +1,5 @@
1
+ module ActiveModel
2
+ module Validations
3
+ attr_accessor :auto_html5_validation
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ module ActiveRecord
2
+ class Base
3
+ # Future subclasses will pick up the model extension
4
+ class << self
5
+ def inherited_with_html5_validation(kls) #:nodoc:
6
+ inherited_without_html5_validation kls
7
+ kls.class_eval do
8
+ cattr_accessor :auto_html5_validation, :instance_accessor => false
9
+ end if kls.superclass == ActiveRecord::Base
10
+ end
11
+ alias_method_chain :inherited, :html5_validation
12
+ end
13
+
14
+ # Existing subclasses pick up the model extension as well
15
+ self.descendants.each do |kls|
16
+ cattr_accessor :auto_html5_validation, :instance_accessor => false if kls.superclass == ActiveRecord::Base
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module Html5Validators
2
- VERSION = '0.2.0'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -4,13 +4,15 @@ module Html5Validators
4
4
  class Railtie < ::Rails::Railtie #:nodoc:
5
5
  initializer 'html5_validators' do |app|
6
6
  ActiveSupport.on_load(:active_record) do
7
- require File.join(File.dirname(__FILE__), 'html5_validators/active_model/helper_methods')
7
+ require 'html5_validators/active_model/helper_methods'
8
+ require 'html5_validators/active_model/validations'
8
9
  if (Rails.version < '3.1.0.beta2') && (Rails.version != '3.1.0')
9
- require File.join(File.dirname(__FILE__), 'html5_validators/active_model/initializer_monkey_patches')
10
+ require 'html5_validators/active_model/initializer_monkey_patches'
10
11
  end
12
+ require 'html5_validators/active_record/base'
11
13
  end
12
14
  ActiveSupport.on_load(:action_view) do
13
- require File.join(File.dirname(__FILE__), 'html5_validators/action_view/form_helpers')
15
+ require 'html5_validators/action_view/form_helpers'
14
16
  end
15
17
  end
16
18
  end
data/spec/fake_app.rb ADDED
@@ -0,0 +1,57 @@
1
+ require 'active_record'
2
+ require 'action_controller/railtie'
3
+
4
+ # config
5
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
6
+
7
+ app = Class.new(Rails::Application)
8
+ app.config.secret_token = "You know I'm born to lose, and gambling's for fools, But that's the way I like it baby, I don't wanna live for ever, And don't forget the joker!"
9
+ app.config.session_store :cookie_store, :key => '_myapp_session'
10
+ app.config.active_support.deprecation = :log
11
+ app.initialize!
12
+
13
+ # routes
14
+ app.routes.draw do
15
+ resources :people, :only => [:new, :create] do
16
+ collection do
17
+ get :new_without_html5_validation
18
+ end
19
+ end
20
+ end
21
+
22
+ # models
23
+ class Person < ActiveRecord::Base
24
+ end
25
+
26
+ # controllers
27
+ class ApplicationController < ActionController::Base; end
28
+ class PeopleController < ApplicationController
29
+ def new
30
+ @person = Person.new
31
+ render :inline => <<-ERB
32
+ <%= form_for @person do |f| %>
33
+ <%= f.text_field :name %>
34
+ <% end %>
35
+ ERB
36
+ end
37
+
38
+ def new_without_html5_validation
39
+ @person = Person.new
40
+ render :inline => <<-ERB
41
+ <%= form_for @person, :auto_html5_validation => false do |f| %>
42
+ <%= f.text_field :name %>
43
+ <%= f.text_field :email %>
44
+ <% end %>
45
+ ERB
46
+ end
47
+ end
48
+
49
+ # helpers
50
+ module ApplicationHelper; end
51
+
52
+ #migrations
53
+ class CreateAllTables < ActiveRecord::Migration
54
+ def self.up
55
+ create_table(:people) {|t| t.string :name; t.string :email; t.integer :age}
56
+ end
57
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ feature 'person#new' do
4
+ context 'without validation' do
5
+ scenario 'new form' do
6
+ visit '/people/new'
7
+ page.should have_css('input#person_name')
8
+ page.should_not have_css('input#person_name[required=required]')
9
+ end
10
+
11
+ scenario 'new_without_html5_validation form' do
12
+ visit '/people/new_without_html5_validation'
13
+ page.should have_css('input#person_email')
14
+ page.should_not have_css('input#person_email[required=required]')
15
+ end
16
+ end
17
+
18
+ context 'with required validation' do
19
+ background do
20
+ Person.validates_presence_of :name
21
+ end
22
+ after do
23
+ Person._validators.clear
24
+ end
25
+ scenario 'new form' do
26
+ visit '/people/new'
27
+ page.should have_css('input#person_name')
28
+ page.should have_css('input#person_name[required=required]')
29
+ end
30
+ scenario 'new_without_html5_validation form' do
31
+ 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]')
34
+ end
35
+
36
+ context 'disabling html5_validation in class level' do
37
+ background do
38
+ Person.class_eval do |kls|
39
+ kls.auto_html5_validation = false
40
+ end
41
+ end
42
+ after do
43
+ Person.class_eval do |kls|
44
+ kls.auto_html5_validation = nil
45
+ end
46
+ end
47
+ scenario 'new form' do
48
+ visit '/people/new'
49
+ page.should have_css('input#person_name')
50
+ page.should_not have_css('input#person_name[required=required]')
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,17 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ # load Rails first
4
+ require 'rails'
5
+ require 'html5_validators'
6
+ # needs to load the app before loading rspec/rails => capybara
7
+ require 'fake_app'
8
+ require 'rspec/rails'
9
+ # Requires supporting files with custom matchers and macros, etc,
10
+ # in ./support/ and its subdirectories.
11
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
12
+
13
+ RSpec.configure do |config|
14
+ config.before :all do
15
+ CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'people'
16
+ end
17
+ 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: 0.2.0
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,52 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-02 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-05-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &70174708849260 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70174708849260
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec-rails
27
+ requirement: &70174708848720 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70174708848720
36
+ - !ruby/object:Gem::Dependency
37
+ name: capybara
38
+ requirement: &70174708848200 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70174708848200
47
+ - !ruby/object:Gem::Dependency
48
+ name: sqlite3
49
+ requirement: &70174708847560 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70174708847560
14
58
  description: A gem/plugin for Rails 3 that enables client-side validation using ActiveModel
15
59
  + HTML5
16
60
  email:
@@ -20,6 +64,7 @@ extensions: []
20
64
  extra_rdoc_files: []
21
65
  files:
22
66
  - .gitignore
67
+ - .rspec
23
68
  - Gemfile
24
69
  - MIT-LICENSE
25
70
  - README.rdoc
@@ -29,7 +74,12 @@ files:
29
74
  - lib/html5_validators/action_view/form_helpers.rb
30
75
  - lib/html5_validators/active_model/helper_methods.rb
31
76
  - lib/html5_validators/active_model/initializer_monkey_patches.rb
77
+ - lib/html5_validators/active_model/validations.rb
78
+ - lib/html5_validators/active_record/base.rb
32
79
  - lib/html5_validators/version.rb
80
+ - spec/fake_app.rb
81
+ - spec/requests/validation_spec.rb
82
+ - spec/spec_helper.rb
33
83
  homepage: https://github.com/amatsuda/html5_validators
34
84
  licenses: []
35
85
  post_install_message:
@@ -54,4 +104,7 @@ rubygems_version: 1.8.16
54
104
  signing_key:
55
105
  specification_version: 3
56
106
  summary: Automatic client side validation using HTML5 Form Validation
57
- test_files: []
107
+ test_files:
108
+ - spec/fake_app.rb
109
+ - spec/requests/validation_spec.rb
110
+ - spec/spec_helper.rb