localized_each_validator 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +25 -0
- data/.rspec +1 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +51 -0
- data/LICENSE +20 -0
- data/README.textile +35 -0
- data/Rakefile +36 -0
- data/VERSION +1 -0
- data/lib/localized_each_validator.rb +56 -0
- data/localized_each_validator.gemspec +60 -0
- data/spec/localized_each_validator_spec.rb +54 -0
- data/spec/spec_helper.rb +10 -0
- metadata +106 -0
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
## MAC OS
|
2
|
+
.DS_Store
|
3
|
+
|
4
|
+
## TEXTMATE
|
5
|
+
*.tmproj
|
6
|
+
tmtags
|
7
|
+
|
8
|
+
## EMACS
|
9
|
+
*~
|
10
|
+
\#*
|
11
|
+
.\#*
|
12
|
+
|
13
|
+
## VIM
|
14
|
+
*.swp
|
15
|
+
|
16
|
+
## PROJECT::GENERAL
|
17
|
+
coverage
|
18
|
+
rdoc
|
19
|
+
pkg
|
20
|
+
.bundle
|
21
|
+
.rvmrc
|
22
|
+
|
23
|
+
## PROJECT::DOCUMENTATION
|
24
|
+
.yardoc
|
25
|
+
doc
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
-cfs
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
RedCloth (4.2.3)
|
5
|
+
activemodel (3.0.1)
|
6
|
+
activesupport (= 3.0.1)
|
7
|
+
builder (~> 2.1.2)
|
8
|
+
i18n (~> 0.4.1)
|
9
|
+
activerecord (3.0.1)
|
10
|
+
activemodel (= 3.0.1)
|
11
|
+
activesupport (= 3.0.1)
|
12
|
+
arel (~> 1.0.0)
|
13
|
+
tzinfo (~> 0.3.23)
|
14
|
+
activesupport (3.0.1)
|
15
|
+
arel (1.0.1)
|
16
|
+
activesupport (~> 3.0.0)
|
17
|
+
builder (2.1.2)
|
18
|
+
diff-lcs (1.1.2)
|
19
|
+
gemcutter (0.6.1)
|
20
|
+
git (1.2.5)
|
21
|
+
i18n (0.4.2)
|
22
|
+
jeweler (1.4.0)
|
23
|
+
gemcutter (>= 0.1.0)
|
24
|
+
git (>= 1.2.5)
|
25
|
+
rubyforge (>= 2.0.0)
|
26
|
+
json_pure (1.4.6)
|
27
|
+
rspec (2.0.1)
|
28
|
+
rspec-core (~> 2.0.1)
|
29
|
+
rspec-expectations (~> 2.0.1)
|
30
|
+
rspec-mocks (~> 2.0.1)
|
31
|
+
rspec-core (2.0.1)
|
32
|
+
rspec-expectations (2.0.1)
|
33
|
+
diff-lcs (>= 1.1.2)
|
34
|
+
rspec-mocks (2.0.1)
|
35
|
+
rspec-core (~> 2.0.1)
|
36
|
+
rspec-expectations (~> 2.0.1)
|
37
|
+
rubyforge (2.0.4)
|
38
|
+
json_pure (>= 1.1.7)
|
39
|
+
tzinfo (0.3.23)
|
40
|
+
yard (0.6.1)
|
41
|
+
|
42
|
+
PLATFORMS
|
43
|
+
ruby
|
44
|
+
|
45
|
+
DEPENDENCIES
|
46
|
+
RedCloth
|
47
|
+
activerecord
|
48
|
+
activesupport
|
49
|
+
jeweler
|
50
|
+
rspec
|
51
|
+
yard
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Tim Morgan
|
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.textile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
h1. Localized @EachValidator@
|
2
|
+
|
3
|
+
| *Author* | Tim Morgan |
|
4
|
+
| *Version* | 1.0 (Oct 31, 2010) |
|
5
|
+
| *License* | Released under the MIT license. |
|
6
|
+
|
7
|
+
h2. About
|
8
|
+
|
9
|
+
Localized @EachValidator@ is a subclass of ActiveRecord's @EachValidator@ that makes it easier to write a localized validator with Rails 3's localization. It's small and simple.
|
10
|
+
|
11
|
+
h2. Usage
|
12
|
+
|
13
|
+
Add this gem to your project's @Gemfile@, or to your own validator gem's dependencies. Then, sublass @LocalizedEachValidator@ and provide the @error_key@ and override the @valid?@ method, like so:
|
14
|
+
|
15
|
+
<pre><code>
|
16
|
+
class FourValidator < ActiveRecord::EachValidator
|
17
|
+
error_key :must_be_four
|
18
|
+
|
19
|
+
def valid?(record, field, value)
|
20
|
+
value == 4
|
21
|
+
end
|
22
|
+
</code></pre>
|
23
|
+
|
24
|
+
Now, users of your validator can create a localization YAML file like so:
|
25
|
+
|
26
|
+
<pre><code>
|
27
|
+
en:
|
28
|
+
activerecord:
|
29
|
+
errors:
|
30
|
+
messages:
|
31
|
+
must_be_four: This number must be four.
|
32
|
+
</code></pre>
|
33
|
+
|
34
|
+
|
35
|
+
See the {LocalizedEachValidator} class documentation for more information.
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rake'
|
2
|
+
begin
|
3
|
+
require 'bundler'
|
4
|
+
rescue LoadError
|
5
|
+
puts "Bundler is not installed; install with `gem install bundler`."
|
6
|
+
exit 1
|
7
|
+
end
|
8
|
+
|
9
|
+
Bundler.require :default
|
10
|
+
|
11
|
+
Jeweler::Tasks.new do |gem|
|
12
|
+
gem.name = "localized_each_validator"
|
13
|
+
gem.summary = %Q{Simple EachValidator with localization support}
|
14
|
+
gem.description = %Q{Adds an abstract EachValidator superclass that you can use to create localizable validations.}
|
15
|
+
gem.email = "git@timothymorgan.info"
|
16
|
+
gem.homepage = "http://github.com/riscfuture/localized_each_validator"
|
17
|
+
gem.authors = [ "Tim Morgan" ]
|
18
|
+
gem.add_dependency 'activerecord', '>= 3.0'
|
19
|
+
gem.add_dependency 'activesupport', '>= 3.0'
|
20
|
+
end
|
21
|
+
Jeweler::GemcutterTasks.new
|
22
|
+
|
23
|
+
require 'rspec/core/rake_task'
|
24
|
+
RSpec::Core::RakeTask.new
|
25
|
+
|
26
|
+
YARD::Rake::YardocTask.new('doc') do |doc|
|
27
|
+
doc.options << "-m" << "textile"
|
28
|
+
doc.options << "--protected"
|
29
|
+
doc.options << "-r" << "README.textile"
|
30
|
+
doc.options << "-o" << "doc"
|
31
|
+
doc.options << "--title" << "Localized EachValidator Documentation".inspect
|
32
|
+
|
33
|
+
doc.files = [ 'lib/**/*', 'README.textile' ]
|
34
|
+
end
|
35
|
+
|
36
|
+
task(default: :spec)
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'active_support/core_ext/hash/except'
|
2
|
+
|
3
|
+
# An @EachValidator@ that uses the translation table to build its error
|
4
|
+
# messages. Override the {#valid?} method to describe your validation
|
5
|
+
# conditions of your subclasses.
|
6
|
+
#
|
7
|
+
# The error message translation lookups conform to the standard hierarchy of
|
8
|
+
# internationalization keys as described by the
|
9
|
+
# @ActiveRecord::Errors#generate_message@ method. (See its documentation for
|
10
|
+
# more information.) The last portion of the translation key path is the error
|
11
|
+
# message key, and by default it is the name of the validator class (excepting
|
12
|
+
# "Validator"), underscored and demodulized. For example, an
|
13
|
+
# @EmailAddressValidator@ subclass would use the @email_address@ key within the
|
14
|
+
# normal ActiveRecord error key structure.
|
15
|
+
#
|
16
|
+
# @abstract Subclass this validator to perform your specific validations.
|
17
|
+
|
18
|
+
class LocalizedEachValidator < ActiveModel::EachValidator
|
19
|
+
extend ActiveSupport::Memoizable
|
20
|
+
|
21
|
+
# @private
|
22
|
+
def validate_each(record, attribute, value)
|
23
|
+
return if options[:allow_nil] and value.nil?
|
24
|
+
return if options[:allow_blank] and value.blank?
|
25
|
+
record.errors.add(attribute, options[:message] || self.class.error_key) unless valid?(record, attribute, value)
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
# @abstract Override this method to return true or false depending on whether
|
31
|
+
# @value@ is a valid value for @record@'s @attribute@.
|
32
|
+
# @param [ActiveRecord::Base] record The record being validated.
|
33
|
+
# @param [Symbol] attribute The attribute with the given value.
|
34
|
+
# @param value The value of the attribute to be validated.
|
35
|
+
# @return [true, false] Whether the value is valid.
|
36
|
+
|
37
|
+
def valid?(record, attribute, value)
|
38
|
+
raise NotImplementedError, "Implement this method in your subclasses"
|
39
|
+
end
|
40
|
+
|
41
|
+
# @overload error_key
|
42
|
+
# Returns the error message key this class uses.
|
43
|
+
# @return [Symbol] The error message key.
|
44
|
+
#
|
45
|
+
# @overload error_key(value)
|
46
|
+
# Sets the error message key this class uses.
|
47
|
+
# @param [Symbol] value The new error message key.
|
48
|
+
|
49
|
+
def self.error_key(value=nil)
|
50
|
+
if value then
|
51
|
+
@error_key = value
|
52
|
+
else
|
53
|
+
return @error_key || to_s.demodulize.sub(/Validator$/, '').underscore.to_sym
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,60 @@
|
|
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{localized_each_validator}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Tim Morgan"]
|
12
|
+
s.date = %q{2010-10-31}
|
13
|
+
s.description = %q{Adds an abstract EachValidator superclass that you can use to create localizable validations.}
|
14
|
+
s.email = %q{git@timothymorgan.info}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.textile"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
".rspec",
|
23
|
+
"Gemfile",
|
24
|
+
"Gemfile.lock",
|
25
|
+
"LICENSE",
|
26
|
+
"README.textile",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"lib/localized_each_validator.rb",
|
30
|
+
"localized_each_validator.gemspec",
|
31
|
+
"spec/localized_each_validator_spec.rb",
|
32
|
+
"spec/spec_helper.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/riscfuture/localized_each_validator}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.3.7}
|
38
|
+
s.summary = %q{Simple EachValidator with localization support}
|
39
|
+
s.test_files = [
|
40
|
+
"spec/localized_each_validator_spec.rb",
|
41
|
+
"spec/spec_helper.rb"
|
42
|
+
]
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
46
|
+
s.specification_version = 3
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 3.0"])
|
50
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 3.0"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<activerecord>, [">= 3.0"])
|
53
|
+
s.add_dependency(%q<activesupport>, [">= 3.0"])
|
54
|
+
end
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<activerecord>, [">= 3.0"])
|
57
|
+
s.add_dependency(%q<activesupport>, [">= 3.0"])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module SpecSupport
|
4
|
+
class TestLocalizedEachValidator < LocalizedEachValidator
|
5
|
+
def valid?(_,_, v) v == 'foo' end
|
6
|
+
end
|
7
|
+
class FakeModel
|
8
|
+
extend ActiveModel::Translation
|
9
|
+
def errors() @errors ||= ActiveModel::Errors.new(self) end
|
10
|
+
def self.lookup_ancestors() [ self ] end
|
11
|
+
def read_attribute_for_validation(_) "mock" end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe LocalizedEachValidator do
|
16
|
+
before :each do
|
17
|
+
@model = SpecSupport::FakeModel.new
|
18
|
+
end
|
19
|
+
|
20
|
+
describe ".error_key_prefix" do
|
21
|
+
it "should be the downcased name of the validator by default" do
|
22
|
+
SpecSupport::TestLocalizedEachValidator.error_key.should eql(:test_localized_each)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should be able to be set" do
|
26
|
+
SpecSupport::TestLocalizedEachValidator.error_key(:foo)
|
27
|
+
SpecSupport::TestLocalizedEachValidator.error_key.should eql(:foo)
|
28
|
+
SpecSupport::TestLocalizedEachValidator.instance_variable_set :@error_key, nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe ".validate_each" do
|
33
|
+
it "should do nothing if given nil and :allow_nil is set" do
|
34
|
+
SpecSupport::TestLocalizedEachValidator.new(attributes: :field, allow_nil: true).validate_each(@model, :field, nil)
|
35
|
+
@model.errors.should be_empty
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should do nothing if given a blank value and :allow_blank is set" do
|
39
|
+
SpecSupport::TestLocalizedEachValidator.new(attributes: :field, allow_blank: true).validate_each(@model, :field, "")
|
40
|
+
@model.errors.should be_empty
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should validate according to the #valid? method" do
|
44
|
+
SpecSupport::TestLocalizedEachValidator.new(attributes: :field).validate_each(@model, :field, "foo")
|
45
|
+
@model.errors.should be_empty
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should add an error if the validation fails" do
|
49
|
+
SpecSupport::TestLocalizedEachValidator.new(attributes: :field).validate_each(@model, :field, "bar")
|
50
|
+
@model.errors[:field].should_not be_empty
|
51
|
+
@model.errors[:field].first.should include('test_localized_each')
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: localized_each_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Tim Morgan
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-10-31 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activerecord
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 3
|
29
|
+
- 0
|
30
|
+
version: "3.0"
|
31
|
+
type: :runtime
|
32
|
+
prerelease: false
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: activesupport
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 3
|
43
|
+
- 0
|
44
|
+
version: "3.0"
|
45
|
+
type: :runtime
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *id002
|
48
|
+
description: Adds an abstract EachValidator superclass that you can use to create localizable validations.
|
49
|
+
email: git@timothymorgan.info
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files:
|
55
|
+
- LICENSE
|
56
|
+
- README.textile
|
57
|
+
files:
|
58
|
+
- .document
|
59
|
+
- .gitignore
|
60
|
+
- .rspec
|
61
|
+
- Gemfile
|
62
|
+
- Gemfile.lock
|
63
|
+
- LICENSE
|
64
|
+
- README.textile
|
65
|
+
- Rakefile
|
66
|
+
- VERSION
|
67
|
+
- lib/localized_each_validator.rb
|
68
|
+
- localized_each_validator.gemspec
|
69
|
+
- spec/localized_each_validator_spec.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
has_rdoc: true
|
72
|
+
homepage: http://github.com/riscfuture/localized_each_validator
|
73
|
+
licenses: []
|
74
|
+
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options:
|
77
|
+
- --charset=UTF-8
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
hash: 4590308378211320386
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
version: "0"
|
97
|
+
requirements: []
|
98
|
+
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 1.3.7
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Simple EachValidator with localization support
|
104
|
+
test_files:
|
105
|
+
- spec/localized_each_validator_spec.rb
|
106
|
+
- spec/spec_helper.rb
|