midas-live_validator 1.0.0
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/History.txt +4 -0
- data/Manifest.txt +18 -0
- data/PostInstall.txt +7 -0
- data/README.rdoc +52 -0
- data/Rakefile +29 -0
- data/lib/live_validator.rb +19 -0
- data/lib/live_validator/active_record_extensions.rb +72 -0
- data/lib/live_validator/view_helpers.rb +178 -0
- data/rails_generators/live_validator_assets/live_validator_assets_generator.rb +15 -0
- data/rails_generators/live_validator_assets/templates/guilded.live_validator.js +75 -0
- data/rails_generators/live_validator_assets/templates/livevalidation-1.3.compressed.js +4 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/live_validator_spec.rb +11 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +10 -0
- data/tasks/rspec.rake +21 -0
- metadata +104 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
PostInstall.txt
|
4
|
+
README.rdoc
|
5
|
+
Rakefile
|
6
|
+
lib/live_validator.rb
|
7
|
+
lib/live_validator/active_record_extensions.rb
|
8
|
+
lib/live_validator/view_helpers.rb
|
9
|
+
rails_generators/live_validator_assets/live_validator_assets_generator.rb
|
10
|
+
rails_generators/live_validator_assets/templates/guilded.live_validator.js
|
11
|
+
rails_generators/live_validator_assets/templates/livevalidation-1.3.compressed.js
|
12
|
+
script/console
|
13
|
+
script/destroy
|
14
|
+
script/generate
|
15
|
+
spec/live_validator_spec.rb
|
16
|
+
spec/spec.opts
|
17
|
+
spec/spec_helper.rb
|
18
|
+
tasks/rspec.rake
|
data/PostInstall.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
= live_validator
|
2
|
+
|
3
|
+
http://github.com/midas/live_validator/tree/master
|
4
|
+
|
5
|
+
|
6
|
+
== DESCRIPTION:
|
7
|
+
|
8
|
+
Live validator is a Rails Guilded component that will reflect ActiveRecord validations and use them to live validate forms. Live
|
9
|
+
validator uses the Live Validation (http://www.livevalidation.com) JavaScript library to accomplish this task. It also uses an
|
10
|
+
ActiveRecord extension authored by Michael Schuerig.
|
11
|
+
|
12
|
+
|
13
|
+
== FEATURES/PROBLEMS:
|
14
|
+
|
15
|
+
|
16
|
+
== INSTALL:
|
17
|
+
|
18
|
+
sudo gem install midas-live_validator
|
19
|
+
|
20
|
+
|
21
|
+
== USE:
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
== REQUIREMENTS:
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
== LICENSE:
|
30
|
+
|
31
|
+
(The MIT License)
|
32
|
+
|
33
|
+
Copyright (c) 2009 midas (excluding included material copyrighte by others)
|
34
|
+
|
35
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
36
|
+
a copy of this software and associated documentation files (the
|
37
|
+
'Software'), to deal in the Software without restriction, including
|
38
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
39
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
40
|
+
permit persons to whom the Software is furnished to do so, subject to
|
41
|
+
the following conditions:
|
42
|
+
|
43
|
+
The above copyright notice and this permission notice shall be
|
44
|
+
included in all copies or substantial portions of the Software.
|
45
|
+
|
46
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
47
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
48
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
49
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
50
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
51
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
52
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
%w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
|
2
|
+
require File.dirname(__FILE__) + '/lib/live_validator'
|
3
|
+
|
4
|
+
# Generate all the Rake tasks
|
5
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
6
|
+
$hoe = Hoe.new('live_validator', LiveValidator::VERSION) do |p|
|
7
|
+
p.developer('midas', 'jason@lookforwardenterprises.com')
|
8
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
9
|
+
p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
|
10
|
+
p.rubyforge_name = p.name # TODO this is default value
|
11
|
+
# p.extra_deps = [
|
12
|
+
# ['activesupport','>= 2.0.2'],
|
13
|
+
# ]
|
14
|
+
p.extra_dev_deps = [
|
15
|
+
['newgem', ">= #{::Newgem::VERSION}"],
|
16
|
+
['midas-guilded', ">=0.0.5"]
|
17
|
+
]
|
18
|
+
|
19
|
+
p.clean_globs |= %w[**/.DS_Store tmp *.log]
|
20
|
+
path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
|
21
|
+
p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
|
22
|
+
p.rsync_args = '-av --delete --ignore-errors'
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'newgem/tasks' # load /tasks/*.rake
|
26
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
27
|
+
|
28
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
29
|
+
# task :default => [:spec, :features]
|
@@ -0,0 +1,19 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'live_validator/view_helpers'
|
5
|
+
require 'live_validator/active_record_extensions'
|
6
|
+
|
7
|
+
module LiveValidator
|
8
|
+
VERSION = '1.0.0'
|
9
|
+
end
|
10
|
+
|
11
|
+
if defined?( ActiveRecord::Base )
|
12
|
+
ActiveRecord::Base.class_eval do
|
13
|
+
include LiveValidator::ActiveRecordExtensions::ValidationReflection
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
if defined?( ActionView )
|
18
|
+
ActionView::Base.send( :include, LiveValidator::ViewHelpers ) unless ActionView.include?( LiveValidator::ViewHelpers )
|
19
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2006, Michael Schuerig, michael@schuerig.de
|
3
|
+
#
|
4
|
+
# == License
|
5
|
+
# This library is free software; you can redistribute it and/or
|
6
|
+
# modify it under the terms of the GNU Lesser General Public
|
7
|
+
# License as published by the Free Software Foundation; either
|
8
|
+
# version 2.1 of the License, or (at your option) any later version.
|
9
|
+
# See http://www.gnu.org/copyleft/lesser.html
|
10
|
+
#++
|
11
|
+
require 'active_record/reflection'
|
12
|
+
|
13
|
+
module LiveValidator # :nodoc:
|
14
|
+
module ActiveRecordExtensions # :nodoc:
|
15
|
+
module ValidationReflection # :nodoc:
|
16
|
+
|
17
|
+
VALIDATIONS = %w(
|
18
|
+
validates_acceptance_of
|
19
|
+
validates_associated
|
20
|
+
validates_confirmation_of
|
21
|
+
validates_exclusion_of
|
22
|
+
validates_format_of
|
23
|
+
validates_inclusion_of
|
24
|
+
validates_length_of
|
25
|
+
validates_size_of
|
26
|
+
validates_numericality_of
|
27
|
+
validates_presence_of
|
28
|
+
validates_uniqueness_of
|
29
|
+
).freeze
|
30
|
+
|
31
|
+
def self.included( base )
|
32
|
+
base.extend( ClassMethods )
|
33
|
+
|
34
|
+
for validation_type in VALIDATIONS
|
35
|
+
base.module_eval <<-"end_eval"
|
36
|
+
class << self
|
37
|
+
alias_method :#{validation_type}_without_reflection, :#{validation_type}
|
38
|
+
|
39
|
+
def #{validation_type}_with_reflection(*attr_names)
|
40
|
+
#{validation_type}_without_reflection(*attr_names)
|
41
|
+
configuration = attr_names.last.is_a?(Hash) ? attr_names.pop : nil
|
42
|
+
for attr_name in attr_names
|
43
|
+
write_inheritable_array "validations", [ ActiveRecord::Reflection::MacroReflection.new(:#{validation_type}, attr_name, configuration, self) ]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
alias_method :#{validation_type}, :#{validation_type}_with_reflection
|
48
|
+
end
|
49
|
+
end_eval
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
module ClassMethods
|
54
|
+
|
55
|
+
# Returns an array of MacroReflection objects for all validations in the class
|
56
|
+
def reflect_on_all_validations
|
57
|
+
read_inheritable_attribute( "validations" ) || []
|
58
|
+
end
|
59
|
+
|
60
|
+
# Returns an array of MacroReflection objects for all validations defined for the field +attr_name+ (expects a symbol)
|
61
|
+
def reflect_on_validations_for( attr_name )
|
62
|
+
reflect_on_all_validations.find_all do |reflection|
|
63
|
+
reflection.name.to_s == attr_name.to_s
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,178 @@
|
|
1
|
+
module LiveValidator
|
2
|
+
module ViewHelpers
|
3
|
+
|
4
|
+
G_VALIDATION_METHODS = {
|
5
|
+
:presence => "Validate.Presence",
|
6
|
+
:numericality => "Validate.Numericality",
|
7
|
+
:format => "Validate.Format",
|
8
|
+
:length => "Validate.Length",
|
9
|
+
:size => "Validate.Length",
|
10
|
+
:acceptance => "Validate.Acceptance",
|
11
|
+
:confirmation => "Validate.Confirmation",
|
12
|
+
:exclusion => "Validate.Exclusion"
|
13
|
+
}
|
14
|
+
|
15
|
+
# Guilded component. This reads from the server side validations and sets up client side
|
16
|
+
# validations that match utilizing the Live Validation library. The following validation
|
17
|
+
# macros and args are implemented:
|
18
|
+
#
|
19
|
+
# validates_presence_of - :message
|
20
|
+
# validates_numericality_of - :less_than, :less_than_or_equal_to, :equal_to, :greater_than,
|
21
|
+
# :greater_then_or_equal_to, :only_integer, :notANumberMessage, :notAnIntegerMessage,
|
22
|
+
# :wrongNumberMessage, :tooLowMessage, :tooHighMessage
|
23
|
+
# validates_length_of / validates_size_of - :maximum, :minimum, :is, :within, :in, :too_long,
|
24
|
+
# :too_short, :wrong_length
|
25
|
+
# validates_confirmation_of - Only works for 2 fields, no more or less.
|
26
|
+
# validates_acceptance_of - :message
|
27
|
+
# validates_inclusion_of - :message
|
28
|
+
# validates_exclusion_of - :message
|
29
|
+
#
|
30
|
+
# If you need to do custom initialization you can implement g.beforeLiveValidatorInit() or
|
31
|
+
# g.afterLiveValidatorInit().
|
32
|
+
#
|
33
|
+
# If you need custom handling of valid or invalid fields, you can implement g.liveValidatorInvalidField()
|
34
|
+
# or g. liveValidatorValidField().
|
35
|
+
#
|
36
|
+
# *parameters*
|
37
|
+
# form:: The form object from the form_for helper.
|
38
|
+
#
|
39
|
+
# *options*
|
40
|
+
# id:: (required)
|
41
|
+
# except:: List of fields to not include. A string, symbol or array of strings or symbols.
|
42
|
+
#
|
43
|
+
def g_live_validator( form, *args )
|
44
|
+
options = args.extract_options!
|
45
|
+
klass = form.object.class
|
46
|
+
class_name = klass.to_s.downcase
|
47
|
+
options.merge! :id => "live-validator-#{class_name}"
|
48
|
+
ar_obj_name = form.object.class.to_s.underscore
|
49
|
+
|
50
|
+
validations = g_map_validations( klass.reflect_on_all_validations )
|
51
|
+
|
52
|
+
# Remove any foreign keys as they will not be present on the form
|
53
|
+
validations.reject! { |field, validation| field.include?( "_id" ) }
|
54
|
+
|
55
|
+
# Remove any excepts, if necessary
|
56
|
+
if options[:except]
|
57
|
+
if options[:except].is_a?( Array )
|
58
|
+
excepts = options[:except]
|
59
|
+
elsif options[:except].is_a?( String ) || options[:except].is_a?( Symbol )
|
60
|
+
excepts = Array.new << options[:except]
|
61
|
+
else
|
62
|
+
throw "'Except' option must be a string symbol or arry of strings or symbols"
|
63
|
+
end
|
64
|
+
|
65
|
+
# Add the AR object name to the field name, as Rails does this on forms
|
66
|
+
excepts.map! { |except| "#{ar_obj_name}_#{except.to_s}" }
|
67
|
+
|
68
|
+
excepts.each do |except|
|
69
|
+
validations.reject! { |field, validation| field == except }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Handle nested form namings, if necessary
|
74
|
+
if form.object.class.to_s.underscore != form.object_name
|
75
|
+
#field_precursor = form.object_name.gsub( /\[/, '_' ).gsub( /\]/, '_' )
|
76
|
+
field_precursor = form.object_name[0...form.object_name.index( "[" )] + '_'
|
77
|
+
nested_validations = Hash.new
|
78
|
+
validations.each do |key, value|
|
79
|
+
nested_validations[ "#{field_precursor}#{key}".to_sym ] = value
|
80
|
+
end
|
81
|
+
validations = nested_validations
|
82
|
+
end
|
83
|
+
|
84
|
+
options.merge! :validations => validations
|
85
|
+
Guilded::Guilder.instance.add( :live_validator, options, [ 'livevalidation-1.3.compressed.js' ] )
|
86
|
+
return ""
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def g_map_validations( validation_defs )
|
92
|
+
validations = {}
|
93
|
+
confirmation_of = []
|
94
|
+
|
95
|
+
validation_defs.each do |validation_def|
|
96
|
+
klass = validation_def.active_record.to_s.underscore
|
97
|
+
temp = { :name => validation_def.macro }
|
98
|
+
options = {}
|
99
|
+
|
100
|
+
unless validation_def.options.nil?
|
101
|
+
|
102
|
+
validation_def.options.each do |key, value|
|
103
|
+
if key == :greater_than
|
104
|
+
options.merge!( :minimum => value + 1 )
|
105
|
+
elsif key == :greater_than_or_equal_to
|
106
|
+
options.merge!( :minimum => value )
|
107
|
+
elsif key == :equal_to
|
108
|
+
options.merge!( :is => value )
|
109
|
+
elsif key == :less_than
|
110
|
+
options.merge!( :maximum => value - 1 )
|
111
|
+
elsif key == :less_than_or_equal_to
|
112
|
+
options.merge!( :maximum => value )
|
113
|
+
elsif ( key == :within || key == :in ) && validation_def.macro == :validates_length_of
|
114
|
+
options.merge!( :minimum => value.begin )
|
115
|
+
options.merge!( :maximum => value.end )
|
116
|
+
elsif ( validation_def.macro == :validates_inclusion_of || validation_def.macro == :validates_exclusion_of ) && key == :in
|
117
|
+
if value.is_a?( Array )
|
118
|
+
options.merge!( :within => value )
|
119
|
+
else
|
120
|
+
options.merge!( :within => value.to_a )
|
121
|
+
end
|
122
|
+
elsif key == :message
|
123
|
+
options.merge!( :failureMessage => value )
|
124
|
+
elsif key == :allow_nil
|
125
|
+
options.merge!( :allowNull => value )
|
126
|
+
elsif key == :wrong_length
|
127
|
+
options.merge!( :wrongLengthMessage => value )
|
128
|
+
elsif key == :too_long
|
129
|
+
options.merge!( :tooLongMessage => value )
|
130
|
+
elsif key == :too_short
|
131
|
+
options.merge!( :tooShortMessage => value )
|
132
|
+
else
|
133
|
+
options.merge!( key.to_s.camelize( :lower ) => value )
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
temp.merge!( :args => options )
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
# Handle validatesconfirmation_of
|
142
|
+
if validation_def.macro.to_sym == :validates_confirmation_of
|
143
|
+
|
144
|
+
confirmation_of.push( validation_def.name )
|
145
|
+
|
146
|
+
if confirmation_of.size ==2
|
147
|
+
key = "#{klass}_#{confirmation_of[1]}"
|
148
|
+
|
149
|
+
temp[:args] = {} if temp[:options].nil?
|
150
|
+
|
151
|
+
temp[:args].merge! :match => "#{klass}_#{confirmation_of[0]}"
|
152
|
+
|
153
|
+
if validations.has_key?( key )
|
154
|
+
validations[key].push( temp )
|
155
|
+
else
|
156
|
+
validations[key] = [ temp ]
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
else # Handle others
|
161
|
+
|
162
|
+
key = "#{klass}_#{validation_def.name}"
|
163
|
+
|
164
|
+
if validations.has_key?( key )
|
165
|
+
validations[key].push( temp )
|
166
|
+
else
|
167
|
+
validations[key] = [ temp ]
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|
171
|
+
|
172
|
+
end
|
173
|
+
|
174
|
+
return validations
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class LiveValidatorAssetsGenerator < Rails::Generator::Base
|
2
|
+
def initialize(runtime_args, runtime_options = {})
|
3
|
+
super
|
4
|
+
end
|
5
|
+
|
6
|
+
def manifest
|
7
|
+
record do |m|
|
8
|
+
m.file "guilded.live_validator.js", "public/javascripts/guilded.live_validator.js"
|
9
|
+
#m.directory "public/stylesheets/guilded"
|
10
|
+
#m.directory "public/stylesheets/guilded/flash_growler"
|
11
|
+
#m.directory "public/stylesheets/guilded/flash_growler/default"
|
12
|
+
m.file "livevalidation-1.3.compressed.js", "public/javascripts/livevalidation-1.3.compressed.js"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
g.doInvalidField = function()
|
2
|
+
{
|
3
|
+
if( g.liveValidatorInvalidField )
|
4
|
+
{
|
5
|
+
g.liveValidatorInvalidField( this );
|
6
|
+
}
|
7
|
+
else
|
8
|
+
{
|
9
|
+
this.insertMessage( this.createMessageSpan() );
|
10
|
+
this.addFieldClass();
|
11
|
+
}
|
12
|
+
};
|
13
|
+
|
14
|
+
g.doValidField = function()
|
15
|
+
{
|
16
|
+
if( g.liveValidatorValidField )
|
17
|
+
g.liveValidatorValidField( this );
|
18
|
+
else
|
19
|
+
{
|
20
|
+
this.insertMessage( this.createMessageSpan() );
|
21
|
+
this.addFieldClass();
|
22
|
+
}
|
23
|
+
};
|
24
|
+
|
25
|
+
g.liveValidatorInit = function( options )
|
26
|
+
{
|
27
|
+
if( g.beforeLiveValidatorInit )
|
28
|
+
g.beforeLiveValidatorInit( options );
|
29
|
+
|
30
|
+
var moreValidationMethods = {
|
31
|
+
presence: Validate.Presence,
|
32
|
+
numericality: Validate.Numericality,
|
33
|
+
format: Validate.Format,
|
34
|
+
length: Validate.Length,
|
35
|
+
acceptance: Validate.Acceptance,
|
36
|
+
confirmation: Validate.Confirmation
|
37
|
+
};
|
38
|
+
|
39
|
+
var validationMethods = {
|
40
|
+
validates_presence_of: Validate.Presence,
|
41
|
+
validates_numericality_of: Validate.Numericality,
|
42
|
+
validates_format_of: Validate.Format,
|
43
|
+
validates_length_of: Validate.Length,
|
44
|
+
validates_size_of: Validate.Length,
|
45
|
+
validates_acceptance_of: Validate.Acceptance,
|
46
|
+
validates_confirmation_of: Validate.Confirmation,
|
47
|
+
validates_inclusion_of: Validate.Inclusion,
|
48
|
+
validates_exclusion_of: Validate.Exclusion
|
49
|
+
};
|
50
|
+
|
51
|
+
var validations = options[ 'validations' ];
|
52
|
+
|
53
|
+
for( field in validations )
|
54
|
+
{
|
55
|
+
/* Guard against fields that we cannot find throwing errors. Just don't
|
56
|
+
* attach if you cannot find it. */
|
57
|
+
fieldEl = $j( '#' + field );
|
58
|
+
if( fieldEl.length == 0 )
|
59
|
+
continue;
|
60
|
+
|
61
|
+
var vList = validations[ field ];
|
62
|
+
var v = null;
|
63
|
+
|
64
|
+
v = new LiveValidation( field, { onlyOnBlur:true, onInvalid:g.doInvalidField, onValid:g.doValidField } );
|
65
|
+
|
66
|
+
for( var i=0; i<vList.length; i++ )
|
67
|
+
{
|
68
|
+
var validation = vList[i];
|
69
|
+
v.add( validationMethods[ validation.name ], validation.args );
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
if( g.afterLiveValidatorInit )
|
74
|
+
g.afterLiveValidatorInit( options );
|
75
|
+
};
|
@@ -0,0 +1,4 @@
|
|
1
|
+
// LiveValidation 1.3 (standalone version)
|
2
|
+
// Copyright (c) 2007-2008 Alec Hill (www.livevalidation.com)
|
3
|
+
// LiveValidation is licensed under the terms of the MIT License
|
4
|
+
var LiveValidation=function(B,A){this.initialize(B,A);};LiveValidation.VERSION="1.3 standalone";LiveValidation.TEXTAREA=1;LiveValidation.TEXT=2;LiveValidation.PASSWORD=3;LiveValidation.CHECKBOX=4;LiveValidation.SELECT=5;LiveValidation.FILE=6;LiveValidation.massValidate=function(C){var D=true;for(var B=0,A=C.length;B<A;++B){var E=C[B].validate();if(D){D=E;}}return D;};LiveValidation.prototype={validClass:"LV_valid",invalidClass:"LV_invalid",messageClass:"LV_validation_message",validFieldClass:"LV_valid_field",invalidFieldClass:"LV_invalid_field",initialize:function(D,C){var A=this;if(!D){throw new Error("LiveValidation::initialize - No element reference or element id has been provided!");}this.element=D.nodeName?D:document.getElementById(D);if(!this.element){throw new Error("LiveValidation::initialize - No element with reference or id of '"+D+"' exists!");}this.validations=[];this.elementType=this.getElementType();this.form=this.element.form;var B=C||{};this.validMessage=B.validMessage||"";var E=B.insertAfterWhatNode||this.element;this.insertAfterWhatNode=E.nodeType?E:document.getElementById(E);this.onValid=B.onValid||function(){this.insertMessage(this.createMessageSpan());this.addFieldClass();};this.onInvalid=B.onInvalid||function(){this.insertMessage(this.createMessageSpan());this.addFieldClass();};this.onlyOnBlur=B.onlyOnBlur||false;this.wait=B.wait||0;this.onlyOnSubmit=B.onlyOnSubmit||false;if(this.form){this.formObj=LiveValidationForm.getInstance(this.form);this.formObj.addField(this);}this.oldOnFocus=this.element.onfocus||function(){};this.oldOnBlur=this.element.onblur||function(){};this.oldOnClick=this.element.onclick||function(){};this.oldOnChange=this.element.onchange||function(){};this.oldOnKeyup=this.element.onkeyup||function(){};this.element.onfocus=function(F){A.doOnFocus(F);return A.oldOnFocus.call(this,F);};if(!this.onlyOnSubmit){switch(this.elementType){case LiveValidation.CHECKBOX:this.element.onclick=function(F){A.validate();return A.oldOnClick.call(this,F);};case LiveValidation.SELECT:case LiveValidation.FILE:this.element.onchange=function(F){A.validate();return A.oldOnChange.call(this,F);};break;default:if(!this.onlyOnBlur){this.element.onkeyup=function(F){A.deferValidation();return A.oldOnKeyup.call(this,F);};}this.element.onblur=function(F){A.doOnBlur(F);return A.oldOnBlur.call(this,F);};}}},destroy:function(){if(this.formObj){this.formObj.removeField(this);this.formObj.destroy();}this.element.onfocus=this.oldOnFocus;if(!this.onlyOnSubmit){switch(this.elementType){case LiveValidation.CHECKBOX:this.element.onclick=this.oldOnClick;case LiveValidation.SELECT:case LiveValidation.FILE:this.element.onchange=this.oldOnChange;break;default:if(!this.onlyOnBlur){this.element.onkeyup=this.oldOnKeyup;}this.element.onblur=this.oldOnBlur;}}this.validations=[];this.removeMessageAndFieldClass();},add:function(A,B){this.validations.push({type:A,params:B||{}});return this;},remove:function(B,D){var E=false;for(var C=0,A=this.validations.length;C<A;C++){if(this.validations[C].type==B){if(this.validations[C].params==D){E=true;break;}}}if(E){this.validations.splice(C,1);}return this;},deferValidation:function(B){if(this.wait>=300){this.removeMessageAndFieldClass();}var A=this;if(this.timeout){clearTimeout(A.timeout);}this.timeout=setTimeout(function(){A.validate();},A.wait);},doOnBlur:function(A){this.focused=false;this.validate(A);},doOnFocus:function(A){this.focused=true;this.removeMessageAndFieldClass();},getElementType:function(){switch(true){case (this.element.nodeName.toUpperCase()=="TEXTAREA"):return LiveValidation.TEXTAREA;case (this.element.nodeName.toUpperCase()=="INPUT"&&this.element.type.toUpperCase()=="TEXT"):return LiveValidation.TEXT;case (this.element.nodeName.toUpperCase()=="INPUT"&&this.element.type.toUpperCase()=="PASSWORD"):return LiveValidation.PASSWORD;case (this.element.nodeName.toUpperCase()=="INPUT"&&this.element.type.toUpperCase()=="CHECKBOX"):return LiveValidation.CHECKBOX;case (this.element.nodeName.toUpperCase()=="INPUT"&&this.element.type.toUpperCase()=="FILE"):return LiveValidation.FILE;case (this.element.nodeName.toUpperCase()=="SELECT"):return LiveValidation.SELECT;case (this.element.nodeName.toUpperCase()=="INPUT"):throw new Error("LiveValidation::getElementType - Cannot use LiveValidation on an "+this.element.type+" input!");default:throw new Error("LiveValidation::getElementType - Element must be an input, select, or textarea!");}},doValidations:function(){this.validationFailed=false;for(var C=0,A=this.validations.length;C<A;++C){var B=this.validations[C];switch(B.type){case Validate.Presence:case Validate.Confirmation:case Validate.Acceptance:this.displayMessageWhenEmpty=true;this.validationFailed=!this.validateElement(B.type,B.params);break;default:this.validationFailed=!this.validateElement(B.type,B.params);break;}if(this.validationFailed){return false;}}this.message=this.validMessage;return true;},validateElement:function(A,C){var D=(this.elementType==LiveValidation.SELECT)?this.element.options[this.element.selectedIndex].value:this.element.value;if(A==Validate.Acceptance){if(this.elementType!=LiveValidation.CHECKBOX){throw new Error("LiveValidation::validateElement - Element to validate acceptance must be a checkbox!");}D=this.element.checked;}var E=true;try{A(D,C);}catch(B){if(B instanceof Validate.Error){if(D!==""||(D===""&&this.displayMessageWhenEmpty)){this.validationFailed=true;this.message=B.message;E=false;}}else{throw B;}}finally{return E;}},validate:function(){if(!this.element.disabled){var A=this.doValidations();if(A){this.onValid();return true;}else{this.onInvalid();return false;}}else{return true;}},enable:function(){this.element.disabled=false;return this;},disable:function(){this.element.disabled=true;this.removeMessageAndFieldClass();return this;},createMessageSpan:function(){var A=document.createElement("span");var B=document.createTextNode(this.message);A.appendChild(B);return A;},insertMessage:function(B){this.removeMessage();if((this.displayMessageWhenEmpty&&(this.elementType==LiveValidation.CHECKBOX||this.element.value==""))||this.element.value!=""){var A=this.validationFailed?this.invalidClass:this.validClass;B.className+=" "+this.messageClass+" "+A;if(this.insertAfterWhatNode.nextSibling){this.insertAfterWhatNode.parentNode.insertBefore(B,this.insertAfterWhatNode.nextSibling);}else{this.insertAfterWhatNode.parentNode.appendChild(B);}}},addFieldClass:function(){this.removeFieldClass();if(!this.validationFailed){if(this.displayMessageWhenEmpty||this.element.value!=""){if(this.element.className.indexOf(this.validFieldClass)==-1){this.element.className+=" "+this.validFieldClass;}}}else{if(this.element.className.indexOf(this.invalidFieldClass)==-1){this.element.className+=" "+this.invalidFieldClass;}}},removeMessage:function(){var A;var B=this.insertAfterWhatNode;while(B.nextSibling){if(B.nextSibling.nodeType===1){A=B.nextSibling;break;}B=B.nextSibling;}if(A&&A.className.indexOf(this.messageClass)!=-1){this.insertAfterWhatNode.parentNode.removeChild(A);}},removeFieldClass:function(){if(this.element.className.indexOf(this.invalidFieldClass)!=-1){this.element.className=this.element.className.split(this.invalidFieldClass).join("");}if(this.element.className.indexOf(this.validFieldClass)!=-1){this.element.className=this.element.className.split(this.validFieldClass).join(" ");}},removeMessageAndFieldClass:function(){this.removeMessage();this.removeFieldClass();}};var LiveValidationForm=function(A){this.initialize(A);};LiveValidationForm.instances={};LiveValidationForm.getInstance=function(A){var B=Math.random()*Math.random();if(!A.id){A.id="formId_"+B.toString().replace(/\./,"")+new Date().valueOf();}if(!LiveValidationForm.instances[A.id]){LiveValidationForm.instances[A.id]=new LiveValidationForm(A);}return LiveValidationForm.instances[A.id];};LiveValidationForm.prototype={initialize:function(B){this.name=B.id;this.element=B;this.fields=[];this.oldOnSubmit=this.element.onsubmit||function(){};var A=this;this.element.onsubmit=function(C){return(LiveValidation.massValidate(A.fields))?A.oldOnSubmit.call(this,C||window.event)!==false:false;};},addField:function(A){this.fields.push(A);},removeField:function(C){var D=[];for(var B=0,A=this.fields.length;B<A;B++){if(this.fields[B]!==C){D.push(this.fields[B]);}}this.fields=D;},destroy:function(A){if(this.fields.length!=0&&!A){return false;}this.element.onsubmit=this.oldOnSubmit;LiveValidationForm.instances[this.name]=null;return true;}};var Validate={Presence:function(B,C){var C=C||{};var A=C.failureMessage||"Can't be empty!";if(B===""||B===null||B===undefined){Validate.fail(A);}return true;},Numericality:function(J,E){var A=J;var J=Number(J);var E=E||{};var F=((E.minimum)||(E.minimum==0))?E.minimum:null;var C=((E.maximum)||(E.maximum==0))?E.maximum:null;var D=((E.is)||(E.is==0))?E.is:null;var G=E.notANumberMessage||"Must be a number!";var H=E.notAnIntegerMessage||"Must be an integer!";var I=E.wrongNumberMessage||"Must be "+D+"!";var B=E.tooLowMessage||"Must not be less than "+F+"!";var K=E.tooHighMessage||"Must not be more than "+C+"!";if(!isFinite(J)){Validate.fail(G);}if(E.onlyInteger&&(/\.0+$|\.$/.test(String(A))||J!=parseInt(J))){Validate.fail(H);}switch(true){case (D!==null):if(J!=Number(D)){Validate.fail(I);}break;case (F!==null&&C!==null):Validate.Numericality(J,{tooLowMessage:B,minimum:F});Validate.Numericality(J,{tooHighMessage:K,maximum:C});break;case (F!==null):if(J<Number(F)){Validate.fail(B);}break;case (C!==null):if(J>Number(C)){Validate.fail(K);}break;}return true;},Format:function(C,E){var C=String(C);var E=E||{};var A=E.failureMessage||"Not valid!";var B=E.pattern||/./;var D=E.negate||false;if(!D&&!B.test(C)){Validate.fail(A);}if(D&&B.test(C)){Validate.fail(A);}return true;},Email:function(B,C){var C=C||{};var A=C.failureMessage||"Must be a valid email address!";Validate.Format(B,{failureMessage:A,pattern:/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i});return true;},Length:function(F,G){var F=String(F);var G=G||{};var E=((G.minimum)||(G.minimum==0))?G.minimum:null;var H=((G.maximum)||(G.maximum==0))?G.maximum:null;var C=((G.is)||(G.is==0))?G.is:null;var A=G.wrongLengthMessage||"Must be "+C+" characters long!";var B=G.tooShortMessage||"Must not be less than "+E+" characters long!";var D=G.tooLongMessage||"Must not be more than "+H+" characters long!";switch(true){case (C!==null):if(F.length!=Number(C)){Validate.fail(A);}break;case (E!==null&&H!==null):Validate.Length(F,{tooShortMessage:B,minimum:E});Validate.Length(F,{tooLongMessage:D,maximum:H});break;case (E!==null):if(F.length<Number(E)){Validate.fail(B);}break;case (H!==null):if(F.length>Number(H)){Validate.fail(D);}break;default:throw new Error("Validate::Length - Length(s) to validate against must be provided!");}return true;},Inclusion:function(H,F){var F=F||{};var K=F.failureMessage||"Must be included in the list!";var G=(F.caseSensitive===false)?false:true;if(F.allowNull&&H==null){return true;}if(!F.allowNull&&H==null){Validate.fail(K);}var D=F.within||[];if(!G){var A=[];for(var C=0,B=D.length;C<B;++C){var I=D[C];if(typeof I=="string"){I=I.toLowerCase();}A.push(I);}D=A;if(typeof H=="string"){H=H.toLowerCase();}}var J=false;for(var E=0,B=D.length;E<B;++E){if(D[E]==H){J=true;}if(F.partialMatch){if(H.indexOf(D[E])!=-1){J=true;}}}if((!F.negate&&!J)||(F.negate&&J)){Validate.fail(K);}return true;},Exclusion:function(A,B){var B=B||{};B.failureMessage=B.failureMessage||"Must not be included in the list!";B.negate=true;Validate.Inclusion(A,B);return true;},Confirmation:function(C,D){if(!D.match){throw new Error("Validate::Confirmation - Error validating confirmation: Id of element to match must be provided!");}var D=D||{};var B=D.failureMessage||"Does not match!";var A=D.match.nodeName?D.match:document.getElementById(D.match);if(!A){throw new Error("Validate::Confirmation - There is no reference with name of, or element with id of '"+D.match+"'!");}if(C!=A.value){Validate.fail(B);}return true;},Acceptance:function(B,C){var C=C||{};var A=C.failureMessage||"Must be accepted!";if(!B){Validate.fail(A);}return true;},Custom:function(D,E){var E=E||{};var B=E.against||function(){return true;};var A=E.args||{};var C=E.failureMessage||"Not valid!";if(!B(D,A)){Validate.fail(C);}return true;},now:function(A,D,C){if(!A){throw new Error("Validate::now - Validation function must be provided!");}var E=true;try{A(D,C||{});}catch(B){if(B instanceof Validate.Error){E=false;}else{throw B;}}finally{return E;}},fail:function(A){throw new Validate.Error(A);},Error:function(A){this.message=A;this.name="ValidationError";}};
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/live_validator.rb'}"
|
9
|
+
puts "Loading live_validator gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/spec/spec_helper.rb
ADDED
data/tasks/rspec.rake
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require 'spec'
|
3
|
+
rescue LoadError
|
4
|
+
require 'rubygems'
|
5
|
+
require 'spec'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'spec/rake/spectask'
|
9
|
+
rescue LoadError
|
10
|
+
puts <<-EOS
|
11
|
+
To use rspec for testing you must install rspec gem:
|
12
|
+
gem install rspec
|
13
|
+
EOS
|
14
|
+
exit(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Run the specs under spec/models"
|
18
|
+
Spec::Rake::SpecTask.new do |t|
|
19
|
+
t.spec_opts = ['--options', "spec/spec.opts"]
|
20
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: midas-live_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- midas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-10 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: newgem
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.3
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: midas-guilded
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.0.5
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: hoe
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.8.0
|
44
|
+
version:
|
45
|
+
description: Live validator is a Rails Guilded component that will reflect ActiveRecord validations and use them to live validate forms. Live validator uses the Live Validation (http://www.livevalidation.com) JavaScript library to accomplish this task. It also uses an ActiveRecord extension authored by Michael Schuerig.
|
46
|
+
email:
|
47
|
+
- jason@lookforwardenterprises.com
|
48
|
+
executables: []
|
49
|
+
|
50
|
+
extensions: []
|
51
|
+
|
52
|
+
extra_rdoc_files:
|
53
|
+
- History.txt
|
54
|
+
- Manifest.txt
|
55
|
+
- PostInstall.txt
|
56
|
+
- README.rdoc
|
57
|
+
files:
|
58
|
+
- History.txt
|
59
|
+
- Manifest.txt
|
60
|
+
- PostInstall.txt
|
61
|
+
- README.rdoc
|
62
|
+
- Rakefile
|
63
|
+
- lib/live_validator.rb
|
64
|
+
- lib/live_validator/active_record_extensions.rb
|
65
|
+
- lib/live_validator/view_helpers.rb
|
66
|
+
- rails_generators/live_validator_assets/live_validator_assets_generator.rb
|
67
|
+
- rails_generators/live_validator_assets/templates/guilded.live_validator.js
|
68
|
+
- rails_generators/live_validator_assets/templates/livevalidation-1.3.compressed.js
|
69
|
+
- script/console
|
70
|
+
- script/destroy
|
71
|
+
- script/generate
|
72
|
+
- spec/live_validator_spec.rb
|
73
|
+
- spec/spec.opts
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
- tasks/rspec.rake
|
76
|
+
has_rdoc: true
|
77
|
+
homepage: http://github.com/midas/live_validator/tree/master
|
78
|
+
post_install_message: PostInstall.txt
|
79
|
+
rdoc_options:
|
80
|
+
- --main
|
81
|
+
- README.rdoc
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: "0"
|
89
|
+
version:
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: "0"
|
95
|
+
version:
|
96
|
+
requirements: []
|
97
|
+
|
98
|
+
rubyforge_project: live_validator
|
99
|
+
rubygems_version: 1.2.0
|
100
|
+
signing_key:
|
101
|
+
specification_version: 2
|
102
|
+
summary: Live validator is a Rails Guilded component that will reflect ActiveRecord validations and use them to live validate forms
|
103
|
+
test_files: []
|
104
|
+
|