common_validators 1.0.0 → 1.0.1
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/README.md +32 -10
- data/app/validators/money_format_validator.rb +14 -3
- data/common_validators.gemspec +3 -1
- data/lib/common_validators/version.rb +1 -1
- data/test/money_format_validator_test.rb +8 -2
- metadata +18 -2
data/README.md
CHANGED
@@ -7,13 +7,35 @@ Collection of common validators for Rails applications.
|
|
7
7
|
|
8
8
|
[RubyDoc Documentation](http://rubydoc.info/github/gshaw/common_validators/master/frames)
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
validates :
|
15
|
-
validates :
|
16
|
-
validates :
|
17
|
-
validates :
|
18
|
-
validates :
|
19
|
-
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
In your ActiveRecord models include one more validations on your model's fields.
|
13
|
+
|
14
|
+
validates :email, email_format: true, presence: true
|
15
|
+
validates :date, date_format: true
|
16
|
+
validates :amount, money_format: { exclude_cents: true }
|
17
|
+
validates :phone_number, phone_number_format: true
|
18
|
+
validates :slug, slug_format: true
|
19
|
+
validates :url, url_format: true
|
20
|
+
|
21
|
+
## Installation
|
22
|
+
|
23
|
+
Add this line to your application's Gemfile:
|
24
|
+
|
25
|
+
gem 'venture_base'
|
26
|
+
|
27
|
+
And then execute:
|
28
|
+
|
29
|
+
$ bundle
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
$ gem install venture_base
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create new Pull Request
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
|
1
3
|
# Validate fields to look like currency values
|
2
4
|
#
|
3
5
|
# NOTE: Currently limited to USD/CAD values.
|
@@ -10,9 +12,6 @@
|
|
10
12
|
# validates :amount, money_format: true # optional
|
11
13
|
# validates :amount, money_format: { exclude_cents: true }
|
12
14
|
# validates :amount, money_format: true, presence: true # required
|
13
|
-
|
14
|
-
require 'bigdecimal'
|
15
|
-
|
16
15
|
class MoneyFormatValidator < ActiveModel::EachValidator
|
17
16
|
MONEY_REGEX = /\A[-+]?\d+(\.\d{1,2})?\z/
|
18
17
|
|
@@ -24,8 +23,20 @@ class MoneyFormatValidator < ActiveModel::EachValidator
|
|
24
23
|
end
|
25
24
|
return if value.blank?
|
26
25
|
|
26
|
+
# Need to first convert BigDecimal to floating point representation of string
|
27
|
+
if value.kind_of?(BigDecimal)
|
28
|
+
value = value.to_s("F")
|
29
|
+
end
|
30
|
+
|
31
|
+
# Finally convert any other types to string and clean off whitespace
|
32
|
+
value = value.to_s.strip
|
33
|
+
|
34
|
+
# Regex seems a bit odd to use here (vs converting to BigDeciaml) but we need
|
35
|
+
# to check for values that BigDecimal can't represent (e.g., "badvalue") so
|
36
|
+
# for now this works.
|
27
37
|
record.errors.add(attr_name, :money_format, options) unless value =~ MONEY_REGEX
|
28
38
|
|
39
|
+
# Check if value has cents but shouldn't
|
29
40
|
if options[:exclude_cents] && value_has_cents?(value)
|
30
41
|
record.errors.add(attr_name, :money_format_whole_number)
|
31
42
|
end
|
data/common_validators.gemspec
CHANGED
@@ -18,7 +18,9 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
20
|
spec.required_ruby_version = ">= 1.9"
|
21
|
-
|
21
|
+
|
22
|
+
spec.add_dependency "rails", "~> 3.2"
|
23
|
+
spec.add_dependency "bigdecimal"
|
22
24
|
|
23
25
|
spec.add_development_dependency "bundler", "~> 1.3"
|
24
26
|
spec.add_development_dependency "rake"
|
@@ -27,6 +27,9 @@ class MoneyFormatValidatorTest < ActiveSupport::TestCase
|
|
27
27
|
assert_invalid_amount "10 20"
|
28
28
|
assert_invalid_amount "10.123"
|
29
29
|
assert_invalid_amount "1,000.12"
|
30
|
+
assert_invalid_amount "1000000.999"
|
31
|
+
assert_invalid_amount 100.999
|
32
|
+
assert_invalid_amount BigDecimal.new("100.999")
|
30
33
|
|
31
34
|
assert_invalid_amount "100.99", :whole_amount
|
32
35
|
assert_invalid_amount "100.1", :whole_amount
|
@@ -34,13 +37,16 @@ class MoneyFormatValidatorTest < ActiveSupport::TestCase
|
|
34
37
|
|
35
38
|
test "valid amount" do
|
36
39
|
assert_valid_amount ""
|
40
|
+
assert_valid_amount "0"
|
37
41
|
assert_valid_amount "1"
|
38
42
|
assert_valid_amount "1.00"
|
39
43
|
assert_valid_amount "1.99"
|
40
44
|
assert_valid_amount "+1.99"
|
41
45
|
assert_valid_amount "-1.99"
|
42
|
-
assert_valid_amount "
|
43
|
-
assert_valid_amount
|
46
|
+
assert_valid_amount "100000000.99"
|
47
|
+
assert_valid_amount 100
|
48
|
+
assert_valid_amount 100.99
|
49
|
+
assert_valid_amount BigDecimal.new("100.99")
|
44
50
|
|
45
51
|
assert_valid_amount "100.00", :whole_amount
|
46
52
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -27,6 +27,22 @@ dependencies:
|
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '3.2'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bigdecimal
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
47
|
name: bundler
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -145,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
145
161
|
version: '0'
|
146
162
|
segments:
|
147
163
|
- 0
|
148
|
-
hash: -
|
164
|
+
hash: -922536056553976169
|
149
165
|
requirements: []
|
150
166
|
rubyforge_project:
|
151
167
|
rubygems_version: 1.8.23
|