active_model_validators_ex 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -0
- data/active_model_validators_ex.gemspec +1 -0
- data/config/locales/en.yml +8 -0
- data/config/locales/pt.yml +8 -0
- data/lib/active_model_validators_ex/array_format_validator.rb +3 -5
- data/lib/active_model_validators_ex/array_inclusion_validator.rb +4 -7
- data/lib/active_model_validators_ex/array_validator_base.rb +2 -2
- data/lib/active_model_validators_ex/time_format_validator.rb +12 -2
- data/lib/active_model_validators_ex/version.rb +2 -2
- data/lib/active_model_validators_ex.rb +1 -1
- data/spec/active_model_validators_ex/array_format_validator_spec.rb +1 -1
- data/spec/active_model_validators_ex/array_inclusion_validator_spec.rb +1 -1
- data/spec/active_model_validators_ex/array_validator_base_spec.rb +2 -2
- data/spec/active_model_validators_ex/time_format_validator_spec.rb +1 -1
- data/spec/mock_objects/mock_record.rb +5 -4
- data/spec/spec_helper.rb +4 -1
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a05737b152952959d082cacc7e93ddf422da9a9f
|
4
|
+
data.tar.gz: ffb49f4614ca57efbd248df84c55dae753280efc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e66f36b5998c3b43cf40487fd130c67d8cd98e79295d8335b42ecb728559e9701f8a226dda075b07817ea2c1d09ab4f5a19d0c1a0c138db0f00f74e0e9fc711d
|
7
|
+
data.tar.gz: 6dd4a3d772f9d09438f10305f019bb619b75f6a6e45d8fd6221c2e7bc3933ccf2fabe6597470a14dbd85225741afce36e8395b1dd13126c067b4e7987b22cf6a
|
data/README.md
CHANGED
@@ -22,6 +22,13 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
$ gem install active_model_validators_ex
|
24
24
|
|
25
|
+
## Localization
|
26
|
+
|
27
|
+
Translations are supported by [I18n](https://github.com/svenfuchs/i18n) and follows
|
28
|
+
the established convention. Default translations are available in the folder
|
29
|
+
[config/locales](https://github.com/junhanamaki/active_model_validators_ex/tree/master/config/locales)
|
30
|
+
of this project, so just copy them to your project and edit as needed.
|
31
|
+
|
25
32
|
## Usage
|
26
33
|
|
27
34
|
After requiring, the following classes will be available for ActiveModel
|
@@ -60,6 +67,8 @@ validations:
|
|
60
67
|
# returns true
|
61
68
|
ExampleModel.new(array: []).valid?
|
62
69
|
|
70
|
+
Used translation keys: array, empty, array_inclusion
|
71
|
+
|
63
72
|
* ArrayFormatValidator
|
64
73
|
|
65
74
|
class ExampleModel
|
@@ -87,6 +96,8 @@ validations:
|
|
87
96
|
# return false
|
88
97
|
ExampleModel.new(array: ['not', 'matching']).valid?
|
89
98
|
|
99
|
+
Used translation keys: array, empty, array_format
|
100
|
+
|
90
101
|
* TimeFormatValidator
|
91
102
|
|
92
103
|
Validates if value is of type Time or a string parsable to Time, example:
|
@@ -111,6 +122,8 @@ validations:
|
|
111
122
|
# returns false
|
112
123
|
ExampleModel.new(time: Time.new(2013)).valid?
|
113
124
|
|
125
|
+
Used translation keys: time_greater_than, time_invalid
|
126
|
+
|
114
127
|
## Contributing
|
115
128
|
|
116
129
|
1. Fork it ( https://github.com/junhanamaki/active_model_validators_ex/fork )
|
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
|
27
27
|
spec.add_development_dependency 'jazz_hands', '~> 0.5'
|
28
28
|
spec.add_development_dependency 'activemodel', '~> 4.1'
|
29
|
+
spec.add_development_dependency 'mongoid', '~> 4.0'
|
29
30
|
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
pt:
|
2
|
+
errors:
|
3
|
+
messages:
|
4
|
+
array: tem de ser uma colecção
|
5
|
+
array_format: os valores estão num formato inválido
|
6
|
+
array_inclusion: tem valores não permitidos
|
7
|
+
time_greater_than: 'tem de ser posterior a %{previous_time}'
|
8
|
+
time_invalid: valor não corresponde a um formato de tempo válido
|
@@ -2,8 +2,8 @@ require 'active_model_validators_ex/array_validator_base'
|
|
2
2
|
|
3
3
|
class ArrayFormatValidator < ArrayValidatorBase
|
4
4
|
def initialize(options)
|
5
|
-
unless options.key?(:with)
|
6
|
-
raise '
|
5
|
+
unless options.key?(:with) and options[:with].is_a?(Regexp)
|
6
|
+
raise 'options must contain a key :with, where the value is a Regexp'
|
7
7
|
end
|
8
8
|
|
9
9
|
super(options)
|
@@ -11,9 +11,7 @@ class ArrayFormatValidator < ArrayValidatorBase
|
|
11
11
|
|
12
12
|
def custom_validations(record, attribute, value)
|
13
13
|
unless value.all? { |val| !val.match(options[:with]).nil? }
|
14
|
-
record.errors
|
15
|
-
"attribute #{attribute} must be an Array with values that matches " \
|
16
|
-
"#{options[:with]}"
|
14
|
+
record.errors.add(attribute, :array_format, options)
|
17
15
|
end
|
18
16
|
end
|
19
17
|
end
|
@@ -2,10 +2,9 @@ require 'active_model_validators_ex/array_validator_base'
|
|
2
2
|
|
3
3
|
class ArrayInclusionValidator < ArrayValidatorBase
|
4
4
|
def initialize(options)
|
5
|
-
unless options.
|
6
|
-
|
7
|
-
|
8
|
-
raise 'key in must be present, and value must be either a Range or Array'
|
5
|
+
unless options[:in].is_a?(Array) or options[:in].is_a?(Range)
|
6
|
+
raise ArgumentError,
|
7
|
+
'value for in must be either a Range or Array'
|
9
8
|
end
|
10
9
|
|
11
10
|
super(options)
|
@@ -13,9 +12,7 @@ class ArrayInclusionValidator < ArrayValidatorBase
|
|
13
12
|
|
14
13
|
def custom_validations(record, attribute, value)
|
15
14
|
unless value.all? { |val| options[:in].include?(val) }
|
16
|
-
record.errors
|
17
|
-
"attribute #{attribute} has be an array composed of values " \
|
18
|
-
"#{options[:in]}"
|
15
|
+
record.errors.add(attribute, :array_inclusion, options)
|
19
16
|
end
|
20
17
|
end
|
21
18
|
end
|
@@ -10,12 +10,12 @@ class ArrayValidatorBase < ActiveModel::EachValidator
|
|
10
10
|
return if options[:allow_nil] && value.nil?
|
11
11
|
|
12
12
|
unless value.is_a? Array
|
13
|
-
record.errors
|
13
|
+
record.errors.add(attribute, :array, options)
|
14
14
|
return
|
15
15
|
end
|
16
16
|
|
17
17
|
if !options[:allow_empty] and value.empty?
|
18
|
-
record.errors
|
18
|
+
record.errors.add(attribute, :empty, options)
|
19
19
|
return
|
20
20
|
end
|
21
21
|
|
@@ -1,5 +1,12 @@
|
|
1
1
|
class TimeFormatValidator < ActiveModel::EachValidator
|
2
2
|
def initialize(options)
|
3
|
+
unless options[:after].is_a?(NilClass) or
|
4
|
+
options[:after].is_a?(Proc) or
|
5
|
+
options[:after].is_a?(Time)
|
6
|
+
raise ArgumentError,
|
7
|
+
'value after must be either NilClass, Proc or Time'
|
8
|
+
end
|
9
|
+
|
3
10
|
options[:allow_nil] ||= false
|
4
11
|
|
5
12
|
super(options)
|
@@ -13,10 +20,13 @@ class TimeFormatValidator < ActiveModel::EachValidator
|
|
13
20
|
previous_time = calculate_previous_time(record)
|
14
21
|
|
15
22
|
if !previous_time.nil? and parsed_time < previous_time
|
16
|
-
|
23
|
+
options[:value] = value
|
24
|
+
options[:previous_time] = previous_time
|
25
|
+
|
26
|
+
record.errors.add(attribute, :time_greater_than, options)
|
17
27
|
end
|
18
28
|
rescue StandardError => e
|
19
|
-
record.errors
|
29
|
+
record.errors.add(attribute, :time_invalid, options)
|
20
30
|
end
|
21
31
|
|
22
32
|
def calculate_previous_time(record)
|
@@ -1,3 +1,3 @@
|
|
1
1
|
module ActiveModelValidatorsEx
|
2
|
-
VERSION = "0.3.
|
3
|
-
end
|
2
|
+
VERSION = "0.3.2"
|
3
|
+
end
|
@@ -24,7 +24,7 @@ describe ArrayFormatValidator do
|
|
24
24
|
end
|
25
25
|
|
26
26
|
describe '#validate_each' do
|
27
|
-
let(:record) { MockRecord.new
|
27
|
+
let(:record) { MockRecord.new }
|
28
28
|
let(:attribute) { :array }
|
29
29
|
let(:validator) { ArrayFormatValidator.new(options) }
|
30
30
|
before { validator.validate_each(record, attribute, value) }
|
@@ -14,7 +14,7 @@ describe ArrayInclusionValidator do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
describe '#validate_each' do
|
17
|
-
let(:record) { MockRecord.new
|
17
|
+
let(:record) { MockRecord.new }
|
18
18
|
let(:attribute) { :array }
|
19
19
|
let(:validator) { ArrayInclusionValidator.new(options) }
|
20
20
|
before { validator.validate_each(record, attribute, value) }
|
@@ -12,8 +12,8 @@ describe ArrayValidatorBase do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
describe '.validate_each' do
|
15
|
-
let(:record) { MockRecord.new
|
16
|
-
let(:attribute) { :
|
15
|
+
let(:record) { MockRecord.new }
|
16
|
+
let(:attribute) { :array }
|
17
17
|
let(:validator) { ArrayValidatorBase.new(options) }
|
18
18
|
before do
|
19
19
|
validator.validate_each(record, attribute, value) rescue nil
|
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe TimeFormatValidator do
|
4
4
|
describe '#validate_each' do
|
5
|
-
let(:record) { MockRecord.new
|
5
|
+
let(:record) { MockRecord.new }
|
6
6
|
let(:attribute) { :time }
|
7
7
|
let(:validator) { TimeFormatValidator.new(options) }
|
8
8
|
before { validator.validate_each(record, attribute, value) }
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_model_validators_ex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- junhanamaki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '4.1'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: mongoid
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '4.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '4.0'
|
97
111
|
description: Add more validators for your ActiveModel models.
|
98
112
|
email:
|
99
113
|
- jun.hanamaki@gmail.com
|
@@ -111,6 +125,8 @@ files:
|
|
111
125
|
- README.md
|
112
126
|
- Rakefile
|
113
127
|
- active_model_validators_ex.gemspec
|
128
|
+
- config/locales/en.yml
|
129
|
+
- config/locales/pt.yml
|
114
130
|
- lib/active_model_validators_ex.rb
|
115
131
|
- lib/active_model_validators_ex/array_format_validator.rb
|
116
132
|
- lib/active_model_validators_ex/array_inclusion_validator.rb
|