common_validators 1.0.3 → 1.1.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.
- checksums.yaml +4 -4
- data/.travis.yml +1 -2
- data/app/validators/money_format_validator.rb +14 -2
- data/config/locales/en.yml +1 -0
- data/lib/common_validators/version.rb +1 -1
- data/test/models/money_format_validator_test.rb +4 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d988741f639f92a4b78583e01e4be5c63ad55bd1
|
4
|
+
data.tar.gz: bd01cb9d66ad674994afa62b96465033fff88608
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ba5d948547f2052aa1b1a7ef5895c1fc993a20be7bcd4234288f924c2b326806ee80e01e597aa8158762297c5977af46bac9f472d955c652f310eb286549578
|
7
|
+
data.tar.gz: 527d1f8d6d86eccdfc1f7385e42cea26a98b1a3be8bf5a5b6951483f5b3dfdb3eb2ae0f9cc169d8989945cdc0a1484e20bea2f3a22885eb5e10f04ffa2d2b2f4
|
data/.travis.yml
CHANGED
@@ -4,13 +4,16 @@ require 'bigdecimal'
|
|
4
4
|
#
|
5
5
|
# NOTE: Currently limited to USD/CAD values.
|
6
6
|
#
|
7
|
-
#
|
7
|
+
# Options:
|
8
|
+
#
|
9
|
+
# * `exclude_cents = true|false` determines if amount can have cents (default false)
|
10
|
+
# * `allow_negative = true|false` determines if amount can be negative (default false)
|
8
11
|
#
|
9
12
|
# A blank value is considered valid (use presence validator to check for that)
|
10
13
|
#
|
11
14
|
# Examples
|
12
15
|
# validates :amount, money_format: true # optional
|
13
|
-
# validates :amount, money_format: { exclude_cents: true }
|
16
|
+
# validates :amount, money_format: { exclude_cents: true, allow_negative: true }
|
14
17
|
# validates :amount, money_format: true, presence: true # required
|
15
18
|
class MoneyFormatValidator < ActiveModel::EachValidator
|
16
19
|
MONEY_REGEX = /\A[-+]?\d+(\.\d{1,2})?\z/
|
@@ -40,9 +43,18 @@ class MoneyFormatValidator < ActiveModel::EachValidator
|
|
40
43
|
if options[:exclude_cents] && value_has_cents?(value)
|
41
44
|
record.errors.add(attr_name, :money_format_has_cents)
|
42
45
|
end
|
46
|
+
|
47
|
+
# Check if value is negative but shouldn't
|
48
|
+
if value_is_negative?(value)
|
49
|
+
record.errors.add(attr_name, :money_format_is_negative) unless options[:allow_negative]
|
50
|
+
end
|
43
51
|
end
|
44
52
|
|
45
53
|
def value_has_cents?(value)
|
46
54
|
BigDecimal.new(value.to_i) != BigDecimal.new(value)
|
47
55
|
end
|
56
|
+
|
57
|
+
def value_is_negative?(value)
|
58
|
+
value.to_i < 0
|
59
|
+
end
|
48
60
|
end
|
data/config/locales/en.yml
CHANGED
@@ -5,6 +5,7 @@ en:
|
|
5
5
|
email_format: "is invalid"
|
6
6
|
money_format: "is invalid"
|
7
7
|
money_format_has_cents: "must not include cents"
|
8
|
+
money_format_is_negative: "must not be negative"
|
8
9
|
phone_number_format: "is invalid"
|
9
10
|
slug_format: "is invalid, only lowercase letters and numbers allowed"
|
10
11
|
url_format: "is invalid"
|
@@ -3,10 +3,11 @@ require File.expand_path("../../../app/validators/money_format_validator", __FI
|
|
3
3
|
|
4
4
|
class TestModel
|
5
5
|
include ActiveModel::Validations
|
6
|
-
attr_accessor :amount, :whole_amount
|
6
|
+
attr_accessor :amount, :whole_amount, :negative_amount
|
7
7
|
|
8
8
|
validates :amount, money_format: true
|
9
9
|
validates :whole_amount, money_format: { exclude_cents: true }
|
10
|
+
validates :negative_amount, money_format: { allow_negative: true }
|
10
11
|
end
|
11
12
|
|
12
13
|
class MoneyFormatValidatorTest < ActiveSupport::TestCase
|
@@ -29,6 +30,7 @@ class MoneyFormatValidatorTest < ActiveSupport::TestCase
|
|
29
30
|
assert_invalid_amount "1,000.12"
|
30
31
|
assert_invalid_amount "1000000.999"
|
31
32
|
assert_invalid_amount 100.999
|
33
|
+
assert_invalid_amount -1
|
32
34
|
assert_invalid_amount BigDecimal.new("100.999")
|
33
35
|
|
34
36
|
assert_invalid_amount "100.99", :whole_amount
|
@@ -42,7 +44,7 @@ class MoneyFormatValidatorTest < ActiveSupport::TestCase
|
|
42
44
|
assert_valid_amount "1.00"
|
43
45
|
assert_valid_amount "1.99"
|
44
46
|
assert_valid_amount "+1.99"
|
45
|
-
assert_valid_amount "-1.99"
|
47
|
+
assert_valid_amount "-1.99", :negative_amount
|
46
48
|
assert_valid_amount "100000000.99"
|
47
49
|
assert_valid_amount 100
|
48
50
|
assert_valid_amount 100.99
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: common_validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerry Shaw
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -200,7 +200,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
200
200
|
version: '0'
|
201
201
|
requirements: []
|
202
202
|
rubyforge_project:
|
203
|
-
rubygems_version: 2.
|
203
|
+
rubygems_version: 2.1.11
|
204
204
|
signing_key:
|
205
205
|
specification_version: 4
|
206
206
|
summary: Common validators for Rails applications
|