html5_validators 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in html5_validators.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,100 @@
1
+ = HTML5Validators
2
+
3
+ Automatic client-side validation using HTML5 Form Validation
4
+
5
+
6
+ == What is this?
7
+
8
+ html5_validators is a gem/plugin for Rails 3 that enables client-side validation using ActiveModel + HTML5
9
+
10
+
11
+ == Features
12
+
13
+ * PresenceValidator => requried
14
+
15
+ Model:
16
+ class User
17
+ include ActiveModel::Validations
18
+ validates_presence_of :name
19
+ end
20
+
21
+ View:
22
+ <%= f.text_field :name %>
23
+
24
+ HTML:
25
+ <input id="user_name" name="user[name]" required="required" type="text" />
26
+
27
+ SPEC:
28
+ http://dev.w3.org/html5/spec/Overview.html#attr-input-requiredhttp://dev.w3.org/html5/spec/Overview.html#attr-input-required
29
+
30
+ * LengthValidator => maxlength
31
+
32
+ Model:
33
+ class User
34
+ include ActiveModel::Validations
35
+ validates_length_of :name, :maximum => 5
36
+ end
37
+
38
+ View:
39
+ <%= f.text_field :name %>
40
+
41
+ HTML:
42
+ <input id="user_name" maxlength="5" name="user[name]" size="5" type="text" />
43
+
44
+ SPEC:
45
+ http://dev.w3.org/html5/spec/Overview.html#attr-input-maxlength
46
+
47
+ * NumericalityValidator => max, min
48
+
49
+ Model:
50
+ class User
51
+ include ActiveModel::Validations
52
+ validates_numericality_of :age, :greater_than_or_equal_to => 20
53
+ end
54
+
55
+ View: (be sure to use number_field)
56
+ <%= f.number_field :age %>
57
+
58
+ HTML:
59
+ <input id="user_age" min="20" name="user[age]" size="30" type="number" />
60
+
61
+ SPEC:
62
+ http://dev.w3.org/html5/spec/Overview.html#attr-input-max
63
+ http://dev.w3.org/html5/spec/Overview.html#attr-input-min
64
+
65
+
66
+ * And more (coming soon...?)
67
+
68
+
69
+ == Supported versions
70
+
71
+ * Ruby 1.8.7, 1.9.2, 1.9.3 (trunk)
72
+
73
+ * Rails 3.0.x, 3.1.beta1, 3.1 (edge)
74
+
75
+ * HTML5 compatible browsers
76
+
77
+
78
+ == Installation
79
+
80
+ Put this line into your Gemfile:
81
+ gem 'html5_validators'
82
+
83
+ Then bundle:
84
+ % bundle
85
+
86
+
87
+ == Notes
88
+
89
+ When accessed by an HTML5 incompatible lagacy brwoser, these extra attributes will just be ignored.
90
+
91
+
92
+ == Todo
93
+
94
+ * specs
95
+ * more validations
96
+
97
+
98
+ == Copyright
99
+
100
+ Copyright (c) 2011 Asakusa.rb. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'html5_validators/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'html5_validators'
7
+ s.version = Html5Validators::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Akira Matsuda']
10
+ s.email = ['ronnie@dio.jp']
11
+ s.homepage = 'https://github.com/amatsuda/html5_validators'
12
+ s.summary = 'Automatic client side validation using HTML5 Form Validation'
13
+ s.description = 'A gem/plugin for Rails 3 that enables client-side validation using ActiveModel + HTML5'
14
+
15
+ s.rubyforge_project = 'html5_validators'
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ['lib']
21
+ end
@@ -0,0 +1,16 @@
1
+ require 'rails'
2
+
3
+ module Html5Validators
4
+ class Railtie < ::Rails::Railtie #:nodoc:
5
+ initializer 'html5_validators' do |app|
6
+ ActiveSupport.on_load(:active_record) do
7
+ require File.join(File.dirname(__FILE__), 'html5_validators/active_model/helper_methods')
8
+ # see: https://github.com/rails/rails/pull/1085
9
+ require File.join(File.dirname(__FILE__), 'html5_validators/active_model/initializer_monkey_patches')
10
+ end
11
+ ActiveSupport.on_load(:action_view) do
12
+ require File.join(File.dirname(__FILE__), 'html5_validators/action_view/form_helpers')
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module ActionView
2
+ module Helpers
3
+ class InstanceTag
4
+ def to_input_field_tag_with_html5_attributes(field_type, options = {})
5
+ if object.class.ancestors.include?(ActiveModel::Validations)
6
+ options["required"] ||= object.class.attribute_required?(method_name)
7
+ options["maxlength"] ||= object.class.attribute_maxlength(method_name)
8
+ options["max"] ||= object.class.attribute_max(method_name)
9
+ options["min"] ||= object.class.attribute_min(method_name)
10
+ end
11
+ to_input_field_tag_without_html5_attributes field_type, options
12
+ end
13
+
14
+ alias_method_chain :to_input_field_tag, :html5_attributes
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ module ActiveModel
2
+ module Validations
3
+ module HelperMethods
4
+ def attribute_required?(attribute)
5
+ self.validators.grep(PresenceValidator).any? do |v|
6
+ v.attributes.include?(attribute.to_sym) && (v.options.keys & [:if, :unless]).empty?
7
+ end
8
+ end
9
+
10
+ def attribute_maxlength(attribute)
11
+ self.validators.grep(LengthValidator).select {|v|
12
+ v.attributes.include?(attribute.to_sym) && (v.options.keys & [:maximum, :is]).any? && (v.options.keys & [:if, :unless, :allow_nil, :allow_blank, :tokenizer]).empty?
13
+ }.map {|v| v.options.slice(:maximum, :is)}.map(&:values).flatten.max
14
+ end
15
+
16
+ def attribute_max(attribute)
17
+ self.validators.grep(NumericalityValidator).select {|v|
18
+ v.attributes.include?(attribute.to_sym) && (v.options.keys & [:less_than, :less_than_or_equal_to]).any? && (v.options.keys & [:if, :unless, :allow_nil, :allow_blank]).empty?
19
+ }.map {|v| v.options.slice(:less_than, :less_than_or_equal_to)}.map(&:values).flatten.max
20
+ end
21
+
22
+ def attribute_min(attribute)
23
+ self.validators.grep(NumericalityValidator).select {|v|
24
+ v.attributes.include?(attribute.to_sym) && (v.options.keys & [:greater_than, :greater_than_or_equal_to]).any? && (v.options.keys & [:if, :unless, :allow_nil, :allow_blank]).empty?
25
+ }.map {|v| v.options.slice(:greater_than, :greater_than_or_equal_to)}.map(&:values).flatten.min
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class LengthValidator
4
+ def initialize(options)
5
+ if range = (options.delete(:in) || options.delete(:within))
6
+ raise ArgumentError, ":in and :within must be a Range" unless range.is_a?(Range)
7
+ options[:minimum], options[:maximum] = range.begin, range.end
8
+ options[:maximum] -= 1 if range.exclude_end?
9
+ end
10
+
11
+ super
12
+ end
13
+ end
14
+
15
+ class NumericalityValidator
16
+ def initialize(options)
17
+ super
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Html5Validators
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html5_validators
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Akira Matsuda
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-16 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: A gem/plugin for Rails 3 that enables client-side validation using ActiveModel + HTML5
22
+ email:
23
+ - ronnie@dio.jp
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - README.rdoc
34
+ - Rakefile
35
+ - html5_validators.gemspec
36
+ - lib/html5_validators.rb
37
+ - lib/html5_validators/action_view/form_helpers.rb
38
+ - lib/html5_validators/active_model/helper_methods.rb
39
+ - lib/html5_validators/active_model/initializer_monkey_patches.rb
40
+ - lib/html5_validators/version.rb
41
+ homepage: https://github.com/amatsuda/html5_validators
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project: html5_validators
70
+ rubygems_version: 1.8.2
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Automatic client side validation using HTML5 Form Validation
74
+ test_files: []
75
+