g_live_validator 1.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/History.txt +32 -0
- data/LICENSE +20 -0
- data/README.rdoc +90 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/g_live_validator.gemspec +74 -0
- data/lib/g_live_validator.rb +26 -0
- data/lib/g_live_validator/active_record_extensions.rb +72 -0
- data/lib/g_live_validator/validation_definition.rb +21 -0
- data/lib/g_live_validator/view_helpers.rb +222 -0
- data/live_validator.gemspec +41 -0
- data/rails_generators/live_validator_assets/live_validator_assets_generator.rb +17 -0
- data/rails_generators/live_validator_assets/templates/default.css +23 -0
- data/rails_generators/live_validator_assets/templates/guilded.live_validator.js +90 -0
- data/rails_generators/live_validator_assets/templates/guilded.live_validator.min.js +4 -0
- data/rails_generators/live_validator_assets/templates/livevalidation-1.3.js +884 -0
- data/rails_generators/live_validator_assets/templates/livevalidation-1.3.min.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 +110 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.idea/*
|
data/History.txt
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
== 1.0.7 2010-01-04
|
2
|
+
|
3
|
+
* Stopped automatically eliminating *_id fields from validations.
|
4
|
+
|
5
|
+
|
6
|
+
== 1.0.6 2009-06-18
|
7
|
+
|
8
|
+
* Added code so that client side JavaScript does not add validations with if or except options.
|
9
|
+
|
10
|
+
|
11
|
+
== 1.0.5 2009-06-03
|
12
|
+
|
13
|
+
* Added the g_live_dynamic_validations view helper that works with dynamic validations gem.
|
14
|
+
|
15
|
+
|
16
|
+
== 1.0.4 2009-04-03
|
17
|
+
|
18
|
+
* Added support for InacvtiveRecord (http://github.com/midas/inactive_record/tree/master) validations.
|
19
|
+
|
20
|
+
|
21
|
+
== 1.0.3
|
22
|
+
|
23
|
+
* Updated JavaScript to keep a collection LiveValidation objects indexed by field name accessible in the g namespace.
|
24
|
+
|
25
|
+
|
26
|
+
== 1.0.1
|
27
|
+
|
28
|
+
* Added a guilded.live_validator.min.js file.
|
29
|
+
|
30
|
+
== 0.0.1 2009-03-09
|
31
|
+
|
32
|
+
* Initial release.
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Jason Harrelson (midas)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,90 @@
|
|
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 (http://github.com/midas/guilded/tree/master) component that will reflect ActiveRecord validations
|
9
|
+
and use them to live validate forms. Live validator uses the Live Validation (http://www.livevalidation.com) JavaScript library to
|
10
|
+
accomplish this task. It also uses an ActiveRecord extension authored by Michael Schuerig to mre easily reflect on the ActiveRecord
|
11
|
+
valdiations.
|
12
|
+
|
13
|
+
|
14
|
+
== FEATURES:
|
15
|
+
|
16
|
+
* Validate Rails XHTML forms without submitting form to server.
|
17
|
+
* Declare validations once in ActiveRecord model's. If JavaScript is enabled, the validation will happen live. Otherwise,
|
18
|
+
the validation will happen normally, with a submission to the server.
|
19
|
+
* The error message defined in the validation macro will be used in live validation
|
20
|
+
* The following ActiveRecord validations are currently implemented and working: validates_presence_of (:message),
|
21
|
+
validates_numericality_of (:less_than, :less_than_or_equal_to, :equal_to, :greater_than, :greater_then_or_equal_to,
|
22
|
+
:only_integer, :notANumberMessage, :notAnIntegerMessage, :wrongNumberMessage, :tooLowMessage, :tooHighMessage),
|
23
|
+
validates_length_of / validates_size_of (:maximum, :minimum, :is, :within, :in, :too_long, :too_short, :wrong_length),
|
24
|
+
validates_confirmation_of (Only works for 2 fields, no more nor less.), validates_acceptance_of (:message),
|
25
|
+
validates_inclusion_of (:message), validates_exclusion_of (:message)
|
26
|
+
|
27
|
+
==PROBLEMS:
|
28
|
+
|
29
|
+
|
30
|
+
== INSTALL:
|
31
|
+
|
32
|
+
sudo gem install midas-g_live_validator
|
33
|
+
|
34
|
+
Add gem requirement to your environment.rb file:
|
35
|
+
|
36
|
+
config.gem 'midas-g_live_validator', :version => '1.0.4', :lib => 'g_live_validator', :source => 'http://gems.github.com'
|
37
|
+
|
38
|
+
Generate:
|
39
|
+
|
40
|
+
script/generate live_validator_assets
|
41
|
+
|
42
|
+
|
43
|
+
== USE:
|
44
|
+
|
45
|
+
Make a call to the view_helper within a form:
|
46
|
+
|
47
|
+
<%= g_live_validator f %> # where f is the form variable passed into the form_for block
|
48
|
+
|
49
|
+
To use with a library like midas-dynamic_Validations, that does not use ActiveRecord validations, but some other method of
|
50
|
+
defining validations:
|
51
|
+
|
52
|
+
<%= g_live_dynamic_validator f, @validation_definitions %>
|
53
|
+
|
54
|
+
|
55
|
+
== OPTIONS:
|
56
|
+
|
57
|
+
:except - List of fields not to validate. Foreign key fields (that end in _id) are automatically excluded. Can be a string,
|
58
|
+
symbol or an array of string or sybmols.
|
59
|
+
|
60
|
+
|
61
|
+
== REQUIREMENTS:
|
62
|
+
|
63
|
+
* Guilded >= 0.1.3 (http://github.com/midas/guilded/tree/master)
|
64
|
+
* Rails >= 2.0
|
65
|
+
|
66
|
+
|
67
|
+
== LICENSE:
|
68
|
+
|
69
|
+
(The MIT License)
|
70
|
+
|
71
|
+
Copyright (c) 2009 midas (excluding included material copyrighted by others)
|
72
|
+
|
73
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
74
|
+
a copy of this software and associated documentation files (the
|
75
|
+
'Software'), to deal in the Software without restriction, including
|
76
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
77
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
78
|
+
permit persons to whom the Software is furnished to do so, subject to
|
79
|
+
the following conditions:
|
80
|
+
|
81
|
+
The above copyright notice and this permission notice shall be
|
82
|
+
included in all copies or substantial portions of the Software.
|
83
|
+
|
84
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
85
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
86
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
87
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
88
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
89
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
90
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "g_live_validator"
|
8
|
+
gem.summary = %Q{A Guilded (http://github.com/midas/guilded/tree/master) component that will reflect ActiveRecord validations and use them to live validate forms.}
|
9
|
+
gem.email = "jason@lookforwardenterprises.com"
|
10
|
+
gem.homepage = "http://github.com/midas/g_live_validator"
|
11
|
+
gem.authors = ["C. Jason Harrelson (midas)"]
|
12
|
+
gem.add_dependency "rails", ">= 2.2.0"
|
13
|
+
gem.add_dependency "guilded", ">= 1.0.3"
|
14
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "g_live_validator #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.7
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{g_live_validator}
|
8
|
+
s.version = "1.0.7"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["C. Jason Harrelson (midas)"]
|
12
|
+
s.date = %q{2010-01-04}
|
13
|
+
s.email = %q{jason@lookforwardenterprises.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"LICENSE",
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"History.txt",
|
21
|
+
"LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"g_live_validator.gemspec",
|
26
|
+
"lib/g_live_validator.rb",
|
27
|
+
"lib/g_live_validator/active_record_extensions.rb",
|
28
|
+
"lib/g_live_validator/validation_definition.rb",
|
29
|
+
"lib/g_live_validator/view_helpers.rb",
|
30
|
+
"live_validator.gemspec",
|
31
|
+
"rails_generators/live_validator_assets/live_validator_assets_generator.rb",
|
32
|
+
"rails_generators/live_validator_assets/templates/default.css",
|
33
|
+
"rails_generators/live_validator_assets/templates/guilded.live_validator.js",
|
34
|
+
"rails_generators/live_validator_assets/templates/guilded.live_validator.min.js",
|
35
|
+
"rails_generators/live_validator_assets/templates/livevalidation-1.3.js",
|
36
|
+
"rails_generators/live_validator_assets/templates/livevalidation-1.3.min.js",
|
37
|
+
"script/console",
|
38
|
+
"script/destroy",
|
39
|
+
"script/generate",
|
40
|
+
"spec/live_validator_spec.rb",
|
41
|
+
"spec/spec.opts",
|
42
|
+
"spec/spec_helper.rb",
|
43
|
+
"tasks/rspec.rake"
|
44
|
+
]
|
45
|
+
s.homepage = %q{http://github.com/midas/g_live_validator}
|
46
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
47
|
+
s.require_paths = ["lib"]
|
48
|
+
s.rubygems_version = %q{1.3.5}
|
49
|
+
s.summary = %q{A Guilded (http://github.com/midas/guilded/tree/master) component that will reflect ActiveRecord validations and use them to live validate forms.}
|
50
|
+
s.test_files = [
|
51
|
+
"spec/live_validator_spec.rb",
|
52
|
+
"spec/spec_helper.rb"
|
53
|
+
]
|
54
|
+
|
55
|
+
if s.respond_to? :specification_version then
|
56
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
57
|
+
s.specification_version = 3
|
58
|
+
|
59
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
60
|
+
s.add_runtime_dependency(%q<rails>, [">= 2.2.0"])
|
61
|
+
s.add_runtime_dependency(%q<guilded>, [">= 1.0.3"])
|
62
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
63
|
+
else
|
64
|
+
s.add_dependency(%q<rails>, [">= 2.2.0"])
|
65
|
+
s.add_dependency(%q<guilded>, [">= 1.0.3"])
|
66
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
67
|
+
end
|
68
|
+
else
|
69
|
+
s.add_dependency(%q<rails>, [">= 2.2.0"])
|
70
|
+
s.add_dependency(%q<guilded>, [">= 1.0.3"])
|
71
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'g_live_validator/view_helpers'
|
5
|
+
require 'g_live_validator/validation_definition'
|
6
|
+
require 'g_live_validator/active_record_extensions'
|
7
|
+
|
8
|
+
module GLiveValidator
|
9
|
+
VERSION = '1.0.7'
|
10
|
+
end
|
11
|
+
|
12
|
+
if defined?( ActiveRecord::Base )
|
13
|
+
ActiveRecord::Base.class_eval do
|
14
|
+
include GLiveValidator::ActiveRecordExtensions::ValidationReflection
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
if defined?( InactiveRecord::Base )
|
19
|
+
InactiveRecord::Base.class_eval do
|
20
|
+
include GLiveValidator::ActiveRecordExtensions::ValidationReflection
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
if defined?( ActionView::Base )
|
25
|
+
ActionView::Base.send( :include, GLiveValidator::ViewHelpers ) unless ActionView::Base.include?( GLiveValidator::ViewHelpers )
|
26
|
+
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 GLiveValidator # :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,21 @@
|
|
1
|
+
module GLiveValidator
|
2
|
+
|
3
|
+
class ValidationDefinition
|
4
|
+
attr_accessor :active_record, :macro, :name, :options
|
5
|
+
|
6
|
+
def initialize( validation )
|
7
|
+
if validation.is_a?(ActiveRecord::Reflection::MacroReflection )
|
8
|
+
self.active_record = validation.active_record
|
9
|
+
self.macro = validation.macro
|
10
|
+
self.name = validation.name
|
11
|
+
self.options = validation.options
|
12
|
+
elsif validation.is_a?( ValidationRule )
|
13
|
+
self.active_record = validation.entity_type.constantize
|
14
|
+
self.macro = "validates_#{validation.validation}_of"
|
15
|
+
self.name = validation.attribute
|
16
|
+
self.options = YAML.load( validation.description ) unless validation.description.nil?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,222 @@
|
|
1
|
+
module GLiveValidator
|
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() in the client side JavaScript environment.
|
32
|
+
#
|
33
|
+
# If you need custom handling of valid or invalid fields, you can implement g.liveValidatorInvalidField()
|
34
|
+
# or g.liveValidatorValidField() in the client side JavaScript environment.
|
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
|
+
# TODO: Add human names to options
|
51
|
+
#options.merge! :human_names => form.object
|
52
|
+
|
53
|
+
validations = g_map_validations( klass.reflect_on_all_validations )
|
54
|
+
validations = apply_options( form, validations, ar_obj_name, options )
|
55
|
+
|
56
|
+
options.merge! :validations => validations
|
57
|
+
Guilded::Guilder.instance.add( :live_validator, options, [ 'livevalidation-1.3.min.js' ] )
|
58
|
+
return ""
|
59
|
+
end
|
60
|
+
|
61
|
+
# Guilded component. The live dynamic validator accepts an array of ValidationDefinition objects and
|
62
|
+
# sets up the live validations accordingly. This makes the live dynamic validator usable with the
|
63
|
+
# midas-dynamic_validations gem and possibly other validations that can be standardized through the
|
64
|
+
# ValidationDefinition object.
|
65
|
+
#
|
66
|
+
# The same validations that are supported by the g_live_validator are also supported.
|
67
|
+
#
|
68
|
+
# If you need to do custom initialization you can implement g.beforeLiveValidatorInit() or
|
69
|
+
# g.afterLiveValidatorInit() in the client side JavaScript environment.
|
70
|
+
#
|
71
|
+
# If you need custom handling of valid or invalid fields, you can implement g.liveValidatorInvalidField()
|
72
|
+
# or g.liveValidatorValidField() in the client side JavaScript environment.
|
73
|
+
#
|
74
|
+
# *parameters*
|
75
|
+
# form:: The form object from the form_for helper.
|
76
|
+
#
|
77
|
+
# *options*
|
78
|
+
# id:: (required)
|
79
|
+
# except:: List of fields to not include. A string, symbol or array of strings or symbols.
|
80
|
+
#
|
81
|
+
def g_live_dynamic_validator( form, validation_rules, *args )
|
82
|
+
options = args.extract_options!
|
83
|
+
klass = form.object.class
|
84
|
+
class_name = klass.to_s.downcase
|
85
|
+
options.merge! :id => "live-validator-#{class_name}"
|
86
|
+
ar_obj_name = form.object.class.to_s.underscore
|
87
|
+
|
88
|
+
validation_defs = validation_rules.map { |validation_rule| ValidationDefinition.new( validation_rule ) }
|
89
|
+
|
90
|
+
validations = g_map_validations( validation_defs )
|
91
|
+
validations = apply_options( form, validations, ar_obj_name, options )
|
92
|
+
|
93
|
+
options.merge! :validations => validations
|
94
|
+
Guilded::Guilder.instance.add( :live_validator, options, [ 'livevalidation-1.3.min.js' ] )
|
95
|
+
return ""
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
def g_map_validations( validation_defs )
|
101
|
+
validations = {}
|
102
|
+
confirmation_of = []
|
103
|
+
|
104
|
+
validation_defs.each do |validation_def|
|
105
|
+
klass = validation_def.active_record.to_s.underscore
|
106
|
+
temp = { :name => validation_def.macro }
|
107
|
+
options = {}
|
108
|
+
|
109
|
+
unless validation_def.options.nil?
|
110
|
+
|
111
|
+
validation_def.options.each do |key, value|
|
112
|
+
if key == :greater_than
|
113
|
+
options.merge!( :minimum => value + 1 )
|
114
|
+
elsif key == :greater_than_or_equal_to
|
115
|
+
options.merge!( :minimum => value )
|
116
|
+
elsif key == :equal_to
|
117
|
+
options.merge!( :is => value )
|
118
|
+
elsif key == :less_than
|
119
|
+
options.merge!( :maximum => value - 1 )
|
120
|
+
elsif key == :less_than_or_equal_to
|
121
|
+
options.merge!( :maximum => value )
|
122
|
+
elsif ( key == :within || key == :in ) && validation_def.macro == :validates_length_of
|
123
|
+
options.merge!( :minimum => value.begin )
|
124
|
+
options.merge!( :maximum => value.end )
|
125
|
+
elsif ( validation_def.macro == :validates_inclusion_of || validation_def.macro == :validates_exclusion_of ) && key == :in
|
126
|
+
if value.is_a?( Array )
|
127
|
+
options.merge!( :within => value )
|
128
|
+
else
|
129
|
+
options.merge!( :within => value.to_a )
|
130
|
+
end
|
131
|
+
elsif key == :message
|
132
|
+
options.merge!( :failureMessage => value )
|
133
|
+
elsif key == :allow_nil
|
134
|
+
options.merge!( :allowNull => value )
|
135
|
+
elsif key == :wrong_length
|
136
|
+
options.merge!( :wrongLengthMessage => value )
|
137
|
+
elsif key == :too_long
|
138
|
+
options.merge!( :tooLongMessage => value )
|
139
|
+
elsif key == :too_short
|
140
|
+
options.merge!( :tooShortMessage => value )
|
141
|
+
else
|
142
|
+
options.merge!( key.to_s.camelize( :lower ) => value )
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
temp.merge!( :args => options )
|
147
|
+
|
148
|
+
end
|
149
|
+
|
150
|
+
# Handle validatesconfirmation_of
|
151
|
+
if validation_def.macro.to_sym == :validates_confirmation_of
|
152
|
+
|
153
|
+
confirmation_of.push( validation_def.name )
|
154
|
+
|
155
|
+
if confirmation_of.size ==2
|
156
|
+
key = "#{klass}_#{confirmation_of[1]}"
|
157
|
+
|
158
|
+
temp[:args] = {} if temp[:options].nil?
|
159
|
+
|
160
|
+
temp[:args].merge! :match => "#{klass}_#{confirmation_of[0]}"
|
161
|
+
|
162
|
+
if validations.has_key?( key )
|
163
|
+
validations[key].push( temp )
|
164
|
+
else
|
165
|
+
validations[key] = [ temp ]
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
else # Handle others
|
170
|
+
|
171
|
+
key = "#{klass}_#{validation_def.name}"
|
172
|
+
|
173
|
+
if validations.has_key?( key )
|
174
|
+
validations[key].push( temp )
|
175
|
+
else
|
176
|
+
validations[key] = [ temp ]
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
end
|
182
|
+
|
183
|
+
return validations
|
184
|
+
end
|
185
|
+
|
186
|
+
def apply_options( form, validations, ar_obj_name, options )
|
187
|
+
# Remove any foreign keys as they will not be present on the form
|
188
|
+
#validations.reject! { |field, validation| field.include?( "_id" ) }
|
189
|
+
|
190
|
+
# Remove any excepts, if necessary
|
191
|
+
if options[:except]
|
192
|
+
if options[:except].is_a?( Array )
|
193
|
+
excepts = options[:except]
|
194
|
+
elsif options[:except].is_a?( String ) || options[:except].is_a?( Symbol )
|
195
|
+
excepts = Array.new << options[:except]
|
196
|
+
else
|
197
|
+
throw "'Except' option must be a string symbol or arry of strings or symbols"
|
198
|
+
end
|
199
|
+
|
200
|
+
# Add the AR object name to the field name, as Rails does this on forms
|
201
|
+
excepts.map! { |except| "#{ar_obj_name}_#{except.to_s}" }
|
202
|
+
|
203
|
+
excepts.each do |except|
|
204
|
+
validations.reject! { |field, validation| field == except }
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
# Handle nested form namings, if necessary
|
209
|
+
if form.object.class.to_s.underscore != form.object_name
|
210
|
+
#field_precursor = form.object_name.gsub( /\[/, '_' ).gsub( /\]/, '_' )
|
211
|
+
field_precursor = form.object_name[0...form.object_name.index( "[" )] + '_'
|
212
|
+
nested_validations = Hash.new
|
213
|
+
validations.each do |key, value|
|
214
|
+
nested_validations[ "#{field_precursor}#{key}".to_sym ] = value
|
215
|
+
end
|
216
|
+
validations = nested_validations
|
217
|
+
end
|
218
|
+
|
219
|
+
validations
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|