easy_validatable 1.0.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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/easy_validatable.gemspec +21 -0
- data/lib/active_model/locale/en.yml +8 -0
- data/lib/active_model/validations/association.rb +65 -0
- data/lib/active_model/validators.rb +5 -0
- data/lib/easy_validatable/version.rb +3 -0
- data/lib/easy_validatable.rb +2 -0
- data/spec/cases/patient.rb +5 -0
- data/spec/cases/person.rb +2 -0
- data/spec/cases/professional.rb +9 -0
- data/spec/cases/student.rb +9 -0
- data/spec/cases/task.rb +5 -0
- data/spec/cases.rb +4 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/validations/association_spec.rb +78 -0
- metadata +126 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jonathan C. Calixto
|
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,39 @@
|
|
1
|
+
# EasyValidatable
|
2
|
+
|
3
|
+
custom validators to improve the lives of developers
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'easy_validatable'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install easy_validatable
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
* **association** - validate presence of attribute and verify if exist your relationship
|
22
|
+
```ruby
|
23
|
+
class Patient < Activerecord::Base
|
24
|
+
belongs_to :person
|
25
|
+
|
26
|
+
validates :person_id
|
27
|
+
end
|
28
|
+
|
29
|
+
## TODO
|
30
|
+
* __ make __ a tests
|
31
|
+
* implements matchers
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/easy_validatable/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jonathan C. Calixto"]
|
6
|
+
gem.email = ["jonathanccalixto@gmail.com"]
|
7
|
+
gem.description = %q{custom validators to improve the lives of developers}
|
8
|
+
gem.summary = %q{custom validators}
|
9
|
+
gem.homepage = "https://github.com/jonathanccalixto/easy_validatable"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "easy_validatable"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = EasyValidatable::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'activemodel', ">= 3.0.0"
|
19
|
+
gem.add_development_dependency 'rake', '>= 0.8.7'
|
20
|
+
gem.add_development_dependency 'rspec', '>= 2.2.0'
|
21
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
en:
|
2
|
+
errors:
|
3
|
+
# The values :model, :attribute and :value are always available for interpolation
|
4
|
+
# The value :count is available when applicable. Can be used for pluralization.
|
5
|
+
messages:
|
6
|
+
not_relationship: "is not a relationship"
|
7
|
+
invalid: "is invalid"
|
8
|
+
blank: "can't be blank"
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
# == Active Model Association Validator
|
3
|
+
module Validations
|
4
|
+
class AssociationValidator < EachValidator
|
5
|
+
def validate_each(record, attribute, value)
|
6
|
+
association_name = options.fetch :association, "#{attribute}".humanize.downcase
|
7
|
+
association = record.send :"#{association_name}"
|
8
|
+
|
9
|
+
unless record.respond_to? "#{association_name}".to_sym
|
10
|
+
raise ArgumentError, "The #{association_name} association was not found!"
|
11
|
+
end
|
12
|
+
|
13
|
+
if value.blank?
|
14
|
+
record_error(record, attribute, error_message(:blank), value)
|
15
|
+
elsif association.nil?
|
16
|
+
record_error(record, attribute, error_message(:invalid), value)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
private
|
20
|
+
def error_message(message)
|
21
|
+
options.fetch :"#{message}_message", message
|
22
|
+
end
|
23
|
+
|
24
|
+
def record_error(record, attribute, message, value)
|
25
|
+
record.errors.add(attribute, message, options.merge(:value => value))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module HelperMethods
|
30
|
+
# Validates that the specified attributes and associations are not blank(as
|
31
|
+
# defined by Object#blank?) . Happens by default on save. Example:
|
32
|
+
#
|
33
|
+
# class Patient < ActiveRecord::Base
|
34
|
+
# belongs_to :person
|
35
|
+
# validates_association_of :person_id
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# The person_id attribute and the person association must be the object
|
39
|
+
# and can not be blank unless presence is false.
|
40
|
+
#
|
41
|
+
# Configuration options:
|
42
|
+
# * <tt>:presence</tt> - The verification of presence of the attribute (default is: true).
|
43
|
+
# * <tt>:association</tt> - The association name (default is: nil).
|
44
|
+
# * <tt>:blank_message</tt> - A custom error message (default is: "can't be blank").
|
45
|
+
# * <tt>:invalid_message</tt> - A custom error message (default is: "is invalid").
|
46
|
+
# * <tt>:not_relationship_message</tt> - A custom error message (default is: "is not a relationship").
|
47
|
+
# * <tt>:on</tt> - Specifies when this validation is active. Runs in all
|
48
|
+
# validation contexts by default (+nil+), other options are <tt>:create</tt>
|
49
|
+
# and <tt>:update</tt>.
|
50
|
+
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if
|
51
|
+
# the validation should occur (e.g. <tt>:if => :allow_validation</tt>, or
|
52
|
+
# <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The method, proc
|
53
|
+
# or string should return or evaluate to a true or false value.
|
54
|
+
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine
|
55
|
+
# if the validation should not occur (e.g. <tt>:unless => :skip_validation</tt>,
|
56
|
+
# or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The method,
|
57
|
+
# proc or string should return or evaluate to a true or false value.
|
58
|
+
# * <tt>:strict</tt> - Specifies whether validation should be strict.
|
59
|
+
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
|
60
|
+
def validates_association_of(*attr_names)
|
61
|
+
validates_with AssociationValidator, _merge_attributes(attr_names)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/spec/cases/task.rb
ADDED
data/spec/cases.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Model" do
|
4
|
+
let(:patient) { Patient.new }
|
5
|
+
let(:person) { Person.new }
|
6
|
+
let(:professional) { Professional.new }
|
7
|
+
let(:student) { Student.new }
|
8
|
+
let(:task) { Task.new }
|
9
|
+
|
10
|
+
context "without parameters" do
|
11
|
+
it "triggers an exception when the association of the field_id is not found" do
|
12
|
+
expect { patient.valid? }.to raise_error("The person association was not found!")
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should require association_id to be set" do
|
16
|
+
patient.stub(:person)
|
17
|
+
|
18
|
+
patient.valid?
|
19
|
+
patient.errors[:person_id].should eq [I18n.t('errors.messages.blank')]
|
20
|
+
|
21
|
+
patient.person_id = 1
|
22
|
+
patient.valid?
|
23
|
+
patient.errors[:person_id].should_not eq [I18n.t('errors.messages.blank')]
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should be invalid if the association is not found and association_id was informed" do
|
27
|
+
patient.person_id = 1
|
28
|
+
|
29
|
+
patient.stub!(:person)
|
30
|
+
patient.valid?
|
31
|
+
patient.errors[:person_id].should eq [I18n.t('errors.messages.invalid')]
|
32
|
+
|
33
|
+
patient.stub(:person).and_return(person)
|
34
|
+
patient.valid?
|
35
|
+
patient.errors[:person_id].should_not eq [I18n.t('errors.messages.invalid')]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "with the allow_blank parameter" do
|
40
|
+
it "should not require association_id to be set" do
|
41
|
+
professional.valid?
|
42
|
+
professional.errors[:person_id].should_not eq [I18n.t('errors.messages.blank')]
|
43
|
+
|
44
|
+
professional.person_id = ""
|
45
|
+
professional.valid?
|
46
|
+
professional.errors[:person_id].should_not eq [I18n.t('errors.messages.blank')]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "with the allow_nil parameter" do
|
51
|
+
it "should not require association_id to be set" do
|
52
|
+
student.valid?
|
53
|
+
student.errors[:person_id].should_not eq [I18n.t('errors.messages.blank')]
|
54
|
+
|
55
|
+
student.person_id = ""
|
56
|
+
student.valid?
|
57
|
+
student.errors[:person_id].should eq [I18n.t('errors.messages.blank')]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "with the association parameter" do
|
62
|
+
it "triggers an exception when the association of the field_id is not found" do
|
63
|
+
expect { task.valid? }.to raise_error("The responsable association was not found!")
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should not require association_id to be set" do
|
67
|
+
task.person_id = 1
|
68
|
+
|
69
|
+
task.stub!(:responsable)
|
70
|
+
task.valid?
|
71
|
+
task.errors[:person_id].should eq [I18n.t('errors.messages.invalid')]
|
72
|
+
|
73
|
+
task.stub(:responsable).and_return(person)
|
74
|
+
task.valid?
|
75
|
+
task.errors[:person_id].should_not eq [I18n.t('errors.messages.invalid')]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_validatable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jonathan C. Calixto
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.8.7
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.8.7
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.2.0
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.2.0
|
62
|
+
description: custom validators to improve the lives of developers
|
63
|
+
email:
|
64
|
+
- jonathanccalixto@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- easy_validatable.gemspec
|
75
|
+
- lib/active_model/locale/en.yml
|
76
|
+
- lib/active_model/validations/association.rb
|
77
|
+
- lib/active_model/validators.rb
|
78
|
+
- lib/easy_validatable.rb
|
79
|
+
- lib/easy_validatable/version.rb
|
80
|
+
- spec/cases.rb
|
81
|
+
- spec/cases/patient.rb
|
82
|
+
- spec/cases/person.rb
|
83
|
+
- spec/cases/professional.rb
|
84
|
+
- spec/cases/student.rb
|
85
|
+
- spec/cases/task.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
- spec/validations/association_spec.rb
|
88
|
+
homepage: https://github.com/jonathanccalixto/easy_validatable
|
89
|
+
licenses: []
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
hash: 1178045979402514614
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
hash: 1178045979402514614
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 1.8.24
|
115
|
+
signing_key:
|
116
|
+
specification_version: 3
|
117
|
+
summary: custom validators
|
118
|
+
test_files:
|
119
|
+
- spec/cases.rb
|
120
|
+
- spec/cases/patient.rb
|
121
|
+
- spec/cases/person.rb
|
122
|
+
- spec/cases/professional.rb
|
123
|
+
- spec/cases/student.rb
|
124
|
+
- spec/cases/task.rb
|
125
|
+
- spec/spec_helper.rb
|
126
|
+
- spec/validations/association_spec.rb
|