validate_attributes 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +49 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +16 -0
- data/Appraisals +15 -0
- data/Gemfile +6 -0
- data/LICENSE +22 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +9 -0
- data/gemfiles/rails30.gemfile +8 -0
- data/gemfiles/rails30.gemfile.lock +48 -0
- data/gemfiles/rails31.gemfile +8 -0
- data/gemfiles/rails31.gemfile.lock +50 -0
- data/gemfiles/rails32.gemfile +8 -0
- data/gemfiles/rails32.gemfile.lock +50 -0
- data/gemfiles/rails40.gemfile +8 -0
- data/gemfiles/rails40.gemfile.lock +56 -0
- data/lib/validate_attributes/version.rb +3 -0
- data/lib/validate_attributes.rb +64 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/support/models.rb +47 -0
- data/spec/validate_attributes_spec.rb +105 -0
- data/validate_attributes.gemspec +26 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2dd292f2cac2625e8a5c75fc64171e107e66b9e3
|
4
|
+
data.tar.gz: bf2ca88c970f976f2222aa07dcf2c90f36fa9d19
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4e0ac6cc3c630fdce881fca7403941ce7c7ec83d1d1a6e890b60e21b39dc2bdeb9d689dfb7cd9d087a92cde1e636cb303127819bbfdfd7008108b6e1d2ee00ca
|
7
|
+
data.tar.gz: 8e85704abd93124ed81aab6d7f50dae3c71d08b0b79726d686bcd5d2603f31fbe4c7c1a80da54ab97cb881409c0de0ea0714bd823022d1098be915f04bcd3935
|
data/.gitignore
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
*.bundle
|
11
|
+
*.so
|
12
|
+
*.o
|
13
|
+
*.a
|
14
|
+
mkmf.log
|
15
|
+
|
16
|
+
*.gem
|
17
|
+
*.rbc
|
18
|
+
/.config
|
19
|
+
/coverage/
|
20
|
+
/InstalledFiles
|
21
|
+
/pkg/
|
22
|
+
/spec/reports/
|
23
|
+
/test/tmp/
|
24
|
+
/test/version_tmp/
|
25
|
+
/tmp/
|
26
|
+
|
27
|
+
## Specific to RubyMotion:
|
28
|
+
.dat*
|
29
|
+
.repl_history
|
30
|
+
build/
|
31
|
+
|
32
|
+
## Documentation cache and generated files:
|
33
|
+
/.yardoc/
|
34
|
+
/_yardoc/
|
35
|
+
/doc/
|
36
|
+
/rdoc/
|
37
|
+
|
38
|
+
## Environment normalisation:
|
39
|
+
/.bundle/
|
40
|
+
/lib/bundler/man/
|
41
|
+
|
42
|
+
# for a library or gem, you might want to ignore these files since the code is
|
43
|
+
# intended to run in multiple environments; otherwise, check them in:
|
44
|
+
# Gemfile.lock
|
45
|
+
# .ruby-version
|
46
|
+
# .ruby-gemset
|
47
|
+
|
48
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
49
|
+
.rvmrc
|
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.5
|
data/.travis.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# .travis.yml
|
2
|
+
rvm:
|
3
|
+
- 1.8.7
|
4
|
+
- 1.9.3
|
5
|
+
- 2.1.5
|
6
|
+
gemfile:
|
7
|
+
- gemfiles/rails30.gemfile
|
8
|
+
- gemfiles/rails31.gemfile
|
9
|
+
- gemfiles/rails32.gemfile
|
10
|
+
- gemfiles/rails40.gemfile
|
11
|
+
matrix:
|
12
|
+
exclude:
|
13
|
+
- rvm: 1.8.7
|
14
|
+
gemfile: gemfiles/rails40.gemfile
|
15
|
+
|
16
|
+
script: "bundle exec rake spec"
|
data/Appraisals
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
appraise "rails30" do
|
2
|
+
gem "activemodel", "~> 3.0.0"
|
3
|
+
end
|
4
|
+
|
5
|
+
appraise "rails31" do
|
6
|
+
gem "activemodel", "~> 3.1.0"
|
7
|
+
end
|
8
|
+
|
9
|
+
appraise "rails32" do
|
10
|
+
gem "activemodel", "~> 3.2.0"
|
11
|
+
end
|
12
|
+
|
13
|
+
appraise "rails40" do
|
14
|
+
gem "activemodel", "~> 4.0.0"
|
15
|
+
end
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Gabriel Rios
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Gabriel Rios
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# ValidateAttributes
|
2
|
+
|
3
|
+
Allows for validation of only some attributes
|
4
|
+
|
5
|
+
Based on [expressica/validate_attributes](http://expressica.com/plugins/validate_attributes)
|
6
|
+
|
7
|
+
Currently only tested with ActiveModel 3.0.20
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'validate_attributes'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install validate_attributes
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
TODO: Write usage instructions here
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it ( https://github.com/[my-github-username]/validate_attributes/fork )
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../
|
3
|
+
specs:
|
4
|
+
validate_attributes (0.0.1)
|
5
|
+
activemodel (>= 3.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.0.20)
|
11
|
+
activesupport (= 3.0.20)
|
12
|
+
builder (~> 2.1.2)
|
13
|
+
i18n (~> 0.5.0)
|
14
|
+
activesupport (3.0.20)
|
15
|
+
appraisal (1.0.2)
|
16
|
+
bundler
|
17
|
+
rake
|
18
|
+
thor (>= 0.14.0)
|
19
|
+
builder (2.1.2)
|
20
|
+
diff-lcs (1.2.5)
|
21
|
+
i18n (0.5.4)
|
22
|
+
rake (10.4.0)
|
23
|
+
rspec (3.1.0)
|
24
|
+
rspec-core (~> 3.1.0)
|
25
|
+
rspec-expectations (~> 3.1.0)
|
26
|
+
rspec-mocks (~> 3.1.0)
|
27
|
+
rspec-core (3.1.7)
|
28
|
+
rspec-support (~> 3.1.0)
|
29
|
+
rspec-expectations (3.1.2)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.1.0)
|
32
|
+
rspec-mocks (3.1.3)
|
33
|
+
rspec-support (~> 3.1.0)
|
34
|
+
rspec-support (3.1.2)
|
35
|
+
sqlite3 (1.3.10)
|
36
|
+
thor (0.19.1)
|
37
|
+
|
38
|
+
PLATFORMS
|
39
|
+
ruby
|
40
|
+
|
41
|
+
DEPENDENCIES
|
42
|
+
activemodel (~> 3.0.0)
|
43
|
+
appraisal
|
44
|
+
bundler (~> 1.7)
|
45
|
+
rake (~> 10.0)
|
46
|
+
rspec (~> 3)
|
47
|
+
sqlite3
|
48
|
+
validate_attributes!
|
@@ -0,0 +1,50 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../
|
3
|
+
specs:
|
4
|
+
validate_attributes (0.0.1)
|
5
|
+
activemodel (>= 3.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.1.12)
|
11
|
+
activesupport (= 3.1.12)
|
12
|
+
builder (~> 3.0.0)
|
13
|
+
i18n (~> 0.6)
|
14
|
+
activesupport (3.1.12)
|
15
|
+
multi_json (~> 1.0)
|
16
|
+
appraisal (1.0.2)
|
17
|
+
bundler
|
18
|
+
rake
|
19
|
+
thor (>= 0.14.0)
|
20
|
+
builder (3.0.4)
|
21
|
+
diff-lcs (1.2.5)
|
22
|
+
i18n (0.6.11)
|
23
|
+
multi_json (1.10.1)
|
24
|
+
rake (10.4.0)
|
25
|
+
rspec (3.1.0)
|
26
|
+
rspec-core (~> 3.1.0)
|
27
|
+
rspec-expectations (~> 3.1.0)
|
28
|
+
rspec-mocks (~> 3.1.0)
|
29
|
+
rspec-core (3.1.7)
|
30
|
+
rspec-support (~> 3.1.0)
|
31
|
+
rspec-expectations (3.1.2)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.1.0)
|
34
|
+
rspec-mocks (3.1.3)
|
35
|
+
rspec-support (~> 3.1.0)
|
36
|
+
rspec-support (3.1.2)
|
37
|
+
sqlite3 (1.3.10)
|
38
|
+
thor (0.19.1)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
ruby
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
activemodel (~> 3.1.0)
|
45
|
+
appraisal
|
46
|
+
bundler (~> 1.7)
|
47
|
+
rake (~> 10.0)
|
48
|
+
rspec (~> 3)
|
49
|
+
sqlite3
|
50
|
+
validate_attributes!
|
@@ -0,0 +1,50 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../
|
3
|
+
specs:
|
4
|
+
validate_attributes (0.0.1)
|
5
|
+
activemodel (>= 3.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.2.21)
|
11
|
+
activesupport (= 3.2.21)
|
12
|
+
builder (~> 3.0.0)
|
13
|
+
activesupport (3.2.21)
|
14
|
+
i18n (~> 0.6, >= 0.6.4)
|
15
|
+
multi_json (~> 1.0)
|
16
|
+
appraisal (1.0.2)
|
17
|
+
bundler
|
18
|
+
rake
|
19
|
+
thor (>= 0.14.0)
|
20
|
+
builder (3.0.4)
|
21
|
+
diff-lcs (1.2.5)
|
22
|
+
i18n (0.6.11)
|
23
|
+
multi_json (1.10.1)
|
24
|
+
rake (10.4.0)
|
25
|
+
rspec (3.1.0)
|
26
|
+
rspec-core (~> 3.1.0)
|
27
|
+
rspec-expectations (~> 3.1.0)
|
28
|
+
rspec-mocks (~> 3.1.0)
|
29
|
+
rspec-core (3.1.7)
|
30
|
+
rspec-support (~> 3.1.0)
|
31
|
+
rspec-expectations (3.1.2)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.1.0)
|
34
|
+
rspec-mocks (3.1.3)
|
35
|
+
rspec-support (~> 3.1.0)
|
36
|
+
rspec-support (3.1.2)
|
37
|
+
sqlite3 (1.3.10)
|
38
|
+
thor (0.19.1)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
ruby
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
activemodel (~> 3.2.0)
|
45
|
+
appraisal
|
46
|
+
bundler (~> 1.7)
|
47
|
+
rake (~> 10.0)
|
48
|
+
rspec (~> 3)
|
49
|
+
sqlite3
|
50
|
+
validate_attributes!
|
@@ -0,0 +1,56 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ../
|
3
|
+
specs:
|
4
|
+
validate_attributes (0.0.1)
|
5
|
+
activemodel (>= 3.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (4.0.12)
|
11
|
+
activesupport (= 4.0.12)
|
12
|
+
builder (~> 3.1.0)
|
13
|
+
activesupport (4.0.12)
|
14
|
+
i18n (~> 0.6, >= 0.6.9)
|
15
|
+
minitest (~> 4.2)
|
16
|
+
multi_json (~> 1.3)
|
17
|
+
thread_safe (~> 0.1)
|
18
|
+
tzinfo (~> 0.3.37)
|
19
|
+
appraisal (1.0.2)
|
20
|
+
bundler
|
21
|
+
rake
|
22
|
+
thor (>= 0.14.0)
|
23
|
+
builder (3.1.4)
|
24
|
+
diff-lcs (1.2.5)
|
25
|
+
i18n (0.6.11)
|
26
|
+
minitest (4.7.5)
|
27
|
+
multi_json (1.10.1)
|
28
|
+
rake (10.4.0)
|
29
|
+
rspec (3.1.0)
|
30
|
+
rspec-core (~> 3.1.0)
|
31
|
+
rspec-expectations (~> 3.1.0)
|
32
|
+
rspec-mocks (~> 3.1.0)
|
33
|
+
rspec-core (3.1.7)
|
34
|
+
rspec-support (~> 3.1.0)
|
35
|
+
rspec-expectations (3.1.2)
|
36
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
37
|
+
rspec-support (~> 3.1.0)
|
38
|
+
rspec-mocks (3.1.3)
|
39
|
+
rspec-support (~> 3.1.0)
|
40
|
+
rspec-support (3.1.2)
|
41
|
+
sqlite3 (1.3.10)
|
42
|
+
thor (0.19.1)
|
43
|
+
thread_safe (0.3.4)
|
44
|
+
tzinfo (0.3.42)
|
45
|
+
|
46
|
+
PLATFORMS
|
47
|
+
ruby
|
48
|
+
|
49
|
+
DEPENDENCIES
|
50
|
+
activemodel (~> 4.0.0)
|
51
|
+
appraisal
|
52
|
+
bundler (~> 1.7)
|
53
|
+
rake (~> 10.0)
|
54
|
+
rspec (~> 3)
|
55
|
+
sqlite3
|
56
|
+
validate_attributes!
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require "validate_attributes/version"
|
3
|
+
|
4
|
+
module ValidateAttributes
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def validate_attributes(options = {})
|
8
|
+
return valid? if options.empty?
|
9
|
+
|
10
|
+
_attributes = extract_attributes(options)
|
11
|
+
|
12
|
+
# TODO: Use flat_map only with ruby 1.9
|
13
|
+
if ActiveModel::VERSION::MAJOR == 3
|
14
|
+
return validate_attributes_3(_attributes)
|
15
|
+
end
|
16
|
+
|
17
|
+
result = _attributes.map do |attr|
|
18
|
+
self.class.validators_on(attr).map do |validator|
|
19
|
+
validator.validate_each(self, attr.to_sym, send(attr))
|
20
|
+
self.errors[attr].empty?
|
21
|
+
end
|
22
|
+
end
|
23
|
+
return result.flatten.all?
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def validate_attributes_3(attrs)
|
28
|
+
attrs.map!(&:to_sym)
|
29
|
+
attrs.each do |attr|
|
30
|
+
self.class.validators_on(attr).each do |validator|
|
31
|
+
fields = validator.validate(self)
|
32
|
+
rejected = fields.reject {|f| attrs.include?(f.to_sym) }
|
33
|
+
rejected.each do |f|
|
34
|
+
errors[f].clear
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
self.errors.blank?
|
40
|
+
end
|
41
|
+
|
42
|
+
def extract_attributes(options)
|
43
|
+
only, except = options[:only], options[:except]
|
44
|
+
|
45
|
+
if only.present?
|
46
|
+
only = [only].flatten.map!(&:to_sym)
|
47
|
+
_attributes = only
|
48
|
+
end
|
49
|
+
|
50
|
+
if except.present?
|
51
|
+
except = [except].flatten.map!(&:to_sym)
|
52
|
+
if _attributes.present?
|
53
|
+
_attributes.reject!{|a| except.include?(a) }
|
54
|
+
else
|
55
|
+
_attributes = self.attribute_names
|
56
|
+
_attributes.reject!{|a| except.include?(a.to_sym) }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
return _attributes
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
ActiveModel::Validations.send(:include, ValidateAttributes)
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
RSpec.configure do |config|
|
3
|
+
config.expect_with :rspec do |expectations|
|
4
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
5
|
+
end
|
6
|
+
|
7
|
+
config.mock_with :rspec do |mocks|
|
8
|
+
mocks.verify_partial_doubles = true
|
9
|
+
end
|
10
|
+
|
11
|
+
config.filter_run :focus
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.disable_monkey_patching!
|
14
|
+
|
15
|
+
config.warnings = true
|
16
|
+
|
17
|
+
if config.files_to_run.one?
|
18
|
+
config.default_formatter = 'doc'
|
19
|
+
end
|
20
|
+
|
21
|
+
config.profile_examples = 10
|
22
|
+
|
23
|
+
config.order = :random
|
24
|
+
|
25
|
+
Kernel.srand config.seed
|
26
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
class User
|
2
|
+
include ActiveModel::Validations
|
3
|
+
|
4
|
+
attr_accessor :id, :name, :email, :address
|
5
|
+
|
6
|
+
validates_presence_of :name
|
7
|
+
validates_length_of :name, :minimum => 5
|
8
|
+
validates_presence_of :email
|
9
|
+
validates_presence_of :address
|
10
|
+
|
11
|
+
def self.build(attributes = {})
|
12
|
+
new attributes
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(options = {})
|
16
|
+
@new_record = false
|
17
|
+
options.each do |key, value|
|
18
|
+
send("#{key}=", value)
|
19
|
+
end if options
|
20
|
+
end
|
21
|
+
|
22
|
+
def attribute_names
|
23
|
+
[ "name", "email", "address" ]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Post
|
28
|
+
include ActiveModel::Validations
|
29
|
+
attr_accessor :id, :title, :content
|
30
|
+
|
31
|
+
validates_presence_of :title, :content
|
32
|
+
|
33
|
+
def self.build(attributes = {})
|
34
|
+
new attributes
|
35
|
+
end
|
36
|
+
|
37
|
+
def initialize(options = {})
|
38
|
+
@new_record = false
|
39
|
+
options.each do |key, value|
|
40
|
+
send("#{key}=", value)
|
41
|
+
end if options
|
42
|
+
end
|
43
|
+
|
44
|
+
def attribute_names
|
45
|
+
[ "id", "title", "content" ]
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'validate_attributes'
|
3
|
+
require 'support/models'
|
4
|
+
|
5
|
+
RSpec.describe ValidateAttributes do
|
6
|
+
it "adds validates_attribute method to ActiveModel::Validations" do
|
7
|
+
expect(User.new).to respond_to(:validate_attributes)
|
8
|
+
end
|
9
|
+
|
10
|
+
context "without parameters" do
|
11
|
+
let(:user) { User.new }
|
12
|
+
|
13
|
+
context "invalid" do
|
14
|
+
before { user.validate_attributes }
|
15
|
+
|
16
|
+
it "validates name" do
|
17
|
+
expect(user.errors[:name]).to include("can't be blank")
|
18
|
+
expect(user.errors[:name]).to include("is too short (minimum is 5 characters)")
|
19
|
+
end
|
20
|
+
|
21
|
+
it "validates email" do
|
22
|
+
expect(user.errors[:email]).to include("can't be blank")
|
23
|
+
end
|
24
|
+
|
25
|
+
it "validates address" do
|
26
|
+
expect(user.errors[:address]).to include("can't be blank")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "valid" do
|
31
|
+
user.name = "gabriel"
|
32
|
+
user.email = "test@example.com"
|
33
|
+
user.address = "Address Street"
|
34
|
+
expect(user.validate_attributes).to be_truthy
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "with :only parameter, validates only the array" do
|
39
|
+
context "invalid" do
|
40
|
+
let(:user) { User.new }
|
41
|
+
before { @result = user.validate_attributes(:only => [:email]) }
|
42
|
+
|
43
|
+
it "returns false" do
|
44
|
+
expect(@result).to be_falsey
|
45
|
+
end
|
46
|
+
|
47
|
+
it "doesn't validate name" do
|
48
|
+
expect(user.errors[:name]).to be_blank
|
49
|
+
end
|
50
|
+
|
51
|
+
it "doesn't validate address" do
|
52
|
+
expect(user.errors[:address]).to be_blank
|
53
|
+
end
|
54
|
+
|
55
|
+
it "validate email" do
|
56
|
+
expect(user.errors[:email]).to include("can't be blank")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it "valid" do
|
61
|
+
user = User.new :email => "email@example.org"
|
62
|
+
expect(user.validate_attributes(:only => [:email])).to be_truthy
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "with except parameter, validates all but those" do
|
67
|
+
context "invalid" do
|
68
|
+
let(:user) { User.new }
|
69
|
+
before { @result = user.validate_attributes(:except => [:email]) }
|
70
|
+
|
71
|
+
it "returns false" do
|
72
|
+
expect(@result).to be_falsey
|
73
|
+
end
|
74
|
+
|
75
|
+
it "does validate name" do
|
76
|
+
expect(user.errors[:name]).to_not be_blank
|
77
|
+
end
|
78
|
+
|
79
|
+
it "does validate address" do
|
80
|
+
expect(user.errors[:address]).to_not be_blank
|
81
|
+
end
|
82
|
+
|
83
|
+
it "doesn't validate email" do
|
84
|
+
expect(user.errors[:email]).to be_blank
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
it "valid" do
|
89
|
+
user = User.new :name => "Gabriel", :address => "Address Street"
|
90
|
+
expect(user.validate_attributes(:except => [:email])).to be_truthy
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context "same validator for multiple attributes" do
|
95
|
+
let(:post) { Post.new }
|
96
|
+
before { post.validate_attributes(:only => :title) }
|
97
|
+
|
98
|
+
it "validates only title" do
|
99
|
+
expect(post.errors[:title]).to_not be_blank
|
100
|
+
expect(post.errors[:content]).to be_blank
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'validate_attributes/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "validate_attributes"
|
8
|
+
spec.version = ValidateAttributes::VERSION
|
9
|
+
spec.authors = ["Gabriel Rios"]
|
10
|
+
spec.email = ["gabrielfalcaorios@gmail.com"]
|
11
|
+
spec.summary = %q{Allow for validation of only some attributes}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency('activemodel', '>= 3.0.0')
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3"
|
25
|
+
spec.add_development_dependency 'appraisal'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validate_attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gabriel Rios
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: appraisal
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- gabrielfalcaorios@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".ruby-version"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Appraisals
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- gemfiles/rails30.gemfile
|
101
|
+
- gemfiles/rails30.gemfile.lock
|
102
|
+
- gemfiles/rails31.gemfile
|
103
|
+
- gemfiles/rails31.gemfile.lock
|
104
|
+
- gemfiles/rails32.gemfile
|
105
|
+
- gemfiles/rails32.gemfile.lock
|
106
|
+
- gemfiles/rails40.gemfile
|
107
|
+
- gemfiles/rails40.gemfile.lock
|
108
|
+
- lib/validate_attributes.rb
|
109
|
+
- lib/validate_attributes/version.rb
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
- spec/support/models.rb
|
112
|
+
- spec/validate_attributes_spec.rb
|
113
|
+
- validate_attributes.gemspec
|
114
|
+
homepage: ''
|
115
|
+
licenses:
|
116
|
+
- MIT
|
117
|
+
metadata: {}
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 2.2.2
|
135
|
+
signing_key:
|
136
|
+
specification_version: 4
|
137
|
+
summary: Allow for validation of only some attributes
|
138
|
+
test_files:
|
139
|
+
- spec/spec_helper.rb
|
140
|
+
- spec/support/models.rb
|
141
|
+
- spec/validate_attributes_spec.rb
|