validates_formatting_of 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -1
- data/README.markdown +25 -3
- data/lib/validates_formatting_of/validation_addition.rb +0 -3
- data/lib/validates_formatting_of/version.rb +1 -1
- data/spec/spec_helper.rb +2 -3
- data/spec/validates_formatting_of/validation_addition_spec.rb +15 -10
- data/spec/validates_formatting_of/validation_spec.rb +8 -13
- metadata +3 -2
data/.travis.yml
CHANGED
data/README.markdown
CHANGED
@@ -55,14 +55,36 @@ You can still add the following options when using `validates_formatting_of`:
|
|
55
55
|
* `:unless`
|
56
56
|
* You can also specify the `:message` option to use a custom validation message.
|
57
57
|
|
58
|
-
#
|
58
|
+
# Share your Validations
|
59
59
|
|
60
|
-
|
60
|
+
Say, for example, you have identical plain-old regex validations for different columns or even on different models entirely. You can easily alleviate this problem by simply adding the validation(s) in an initializer file.
|
61
|
+
|
62
|
+
While very unrealistic, these examples should serve their purpose in demonstrating this ability.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
# config/initializers/validates_foramatting_of.rb
|
66
|
+
ValidatesFormattingOf::Method.add :loweralpha, /[a-z]/, "must be lowercase and no spaces"
|
67
|
+
ValidatesFormattingOf::Method.add :upperalpha, /[A-Z]/, "must be uppercase and no spaces"
|
68
|
+
ValidatesFormattingOf::Method.add :weak_password, /[a-zA-Z0-9]{8,}/, "must contain only letters and numbers and be at least 8 characters long".
|
69
|
+
```
|
70
|
+
|
71
|
+
Keep in mind, since this gem is only ActiveModel-dependent, you should be able to do this in whatever non-Rails application classes you have that include `ActiveModel::Validations` in some way (and that extend the `ValidatesFormattingOf::ModelAdditons` module).
|
72
|
+
|
73
|
+
# Built-in Validations
|
74
|
+
|
75
|
+
`validates_formatting_of` has the following build-in validations:
|
61
76
|
|
62
77
|
### Email
|
63
78
|
|
64
79
|
```ruby
|
65
|
-
|
80
|
+
# non ActiveRecord::Base example. This is the least you need to get validations running.
|
81
|
+
class User
|
82
|
+
|
83
|
+
include ActiveModel::Validations
|
84
|
+
extend ValidatesFormattingOf::ModelAdditions
|
85
|
+
|
86
|
+
attr_accessor :email
|
87
|
+
|
66
88
|
validates_formatting_of :email, :using => :email
|
67
89
|
end
|
68
90
|
```
|
@@ -19,9 +19,6 @@ module ValidatesFormattingOf
|
|
19
19
|
def find(attribute, opts = {})
|
20
20
|
method = opts.fetch(:using, attribute)
|
21
21
|
raise MissingValidation.new(method) if missing? method
|
22
|
-
if method.to_sym == :ip_address
|
23
|
-
raise ArgumentError, "[REMOVED] The :ip_address validation for `validates_formatting_of` has been removed since version 0.7.0. Please update your model validations to use :ip_address_v4."
|
24
|
-
end
|
25
22
|
@validations[method.to_sym]
|
26
23
|
end
|
27
24
|
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'simplecov'
|
2
2
|
|
3
|
-
|
3
|
+
if ENV["RUN_SIMPLECOV"] == 'true'
|
4
4
|
SimpleCov.start do
|
5
5
|
load_adapter 'test_frameworks'
|
6
6
|
end
|
@@ -10,9 +10,8 @@ require 'validates_formatting_of'
|
|
10
10
|
require 'active_model'
|
11
11
|
|
12
12
|
class TestActiveRecord
|
13
|
-
include ActiveModel::Validations
|
14
|
-
include ActiveModel::Conversion
|
15
13
|
|
14
|
+
include ActiveModel::Validations
|
16
15
|
extend ValidatesFormattingOf::ModelAdditions
|
17
16
|
|
18
17
|
def initialize(attributes = {})
|
@@ -4,28 +4,33 @@ module ValidatesFormattingOf
|
|
4
4
|
|
5
5
|
class TestAdding
|
6
6
|
extend ValidationAddition
|
7
|
+
def self.clear!
|
8
|
+
@validations = nil
|
9
|
+
end
|
7
10
|
end
|
8
11
|
|
9
12
|
describe ValidationAddition do
|
10
13
|
before do
|
14
|
+
TestAdding.clear!
|
11
15
|
TestAdding.add :email, /email/i
|
12
16
|
end
|
13
|
-
after do
|
14
|
-
TestAdding.instance_variable_set("@validations", nil)
|
15
|
-
end
|
16
17
|
it "should be able to add new validations" do
|
17
18
|
TestAdding.add :another, /another/i
|
18
19
|
TestAdding.validations.count.should == 2
|
19
20
|
TestAdding.validations[:email].should be_instance_of Validation
|
20
21
|
TestAdding.validations[:another].should be_instance_of Validation
|
21
22
|
end
|
22
|
-
|
23
|
-
validation
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
validation
|
23
|
+
describe "find" do
|
24
|
+
context "implicit validation method" do
|
25
|
+
subject { TestAdding.find(:email) }
|
26
|
+
its(:name) { should == :email }
|
27
|
+
its(:regex) { should == /email/i }
|
28
|
+
end
|
29
|
+
context "explicit validation method" do
|
30
|
+
subject { TestAdding.find(:non_existent_validation, :using => :email) }
|
31
|
+
its(:name) { should == :email }
|
32
|
+
its(:regex) { should == /email/i }
|
33
|
+
end
|
29
34
|
end
|
30
35
|
it "should raise an error if the method does not exist" do
|
31
36
|
expect { TestAdding.find(:fake) }.to raise_error MissingValidation
|
@@ -5,19 +5,14 @@ module ValidatesFormattingOf
|
|
5
5
|
describe Validation do
|
6
6
|
context "valid validation creation" do
|
7
7
|
let(:validation) { Validation.new(:name, /something/i, "is an invalid value") }
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
it "should be inspected properly" do
|
17
|
-
validation.inspect.should =~ /Validation/
|
18
|
-
validation.inspect.should =~ /\/something\/i/
|
19
|
-
validation.inspect.should =~ /\:name/
|
20
|
-
end
|
8
|
+
subject { validation }
|
9
|
+
its(:name) { should == :name }
|
10
|
+
its(:regex) { should == %r{something}i }
|
11
|
+
its(:message) { should == 'is an invalid value' }
|
12
|
+
its(:to_s) { should == "<Validation::name>" }
|
13
|
+
its(:inspect) { should =~ /Validation/ }
|
14
|
+
its(:inspect) { should =~ /\/something\/i/ }
|
15
|
+
its(:inspect) { should =~ /\:name/ }
|
21
16
|
end
|
22
17
|
context "invalid validation creation" do
|
23
18
|
it "should raise an error if the specified regex is not a Regexp objct" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_formatting_of
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
@@ -132,3 +132,4 @@ test_files:
|
|
132
132
|
- spec/validates_formatting_of/model_additions_spec.rb
|
133
133
|
- spec/validates_formatting_of/validation_addition_spec.rb
|
134
134
|
- spec/validates_formatting_of/validation_spec.rb
|
135
|
+
has_rdoc:
|