validators 0.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.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in validators.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ validators (0.1.0)
5
+ activerecord (>= 3.0.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activemodel (3.0.1)
11
+ activesupport (= 3.0.1)
12
+ builder (~> 2.1.2)
13
+ i18n (~> 0.4.1)
14
+ activerecord (3.0.1)
15
+ activemodel (= 3.0.1)
16
+ activesupport (= 3.0.1)
17
+ arel (~> 1.0.0)
18
+ tzinfo (~> 0.3.23)
19
+ activesupport (3.0.1)
20
+ archive-tar-minitar (0.5.2)
21
+ arel (1.0.1)
22
+ activesupport (~> 3.0.0)
23
+ builder (2.1.2)
24
+ columnize (0.3.2)
25
+ diff-lcs (1.1.2)
26
+ i18n (0.4.2)
27
+ linecache19 (0.5.11)
28
+ ruby_core_source (>= 0.1.4)
29
+ rspec (2.1.0)
30
+ rspec-core (~> 2.1.0)
31
+ rspec-expectations (~> 2.1.0)
32
+ rspec-mocks (~> 2.1.0)
33
+ rspec-core (2.1.0)
34
+ rspec-expectations (2.1.0)
35
+ diff-lcs (~> 1.1.2)
36
+ rspec-mocks (2.1.0)
37
+ ruby-debug-base19 (0.11.24)
38
+ columnize (>= 0.3.1)
39
+ linecache19 (>= 0.5.11)
40
+ ruby_core_source (>= 0.1.4)
41
+ ruby-debug19 (0.11.6)
42
+ columnize (>= 0.3.1)
43
+ linecache19 (>= 0.5.11)
44
+ ruby-debug-base19 (>= 0.11.19)
45
+ ruby_core_source (0.1.4)
46
+ archive-tar-minitar (>= 0.5.2)
47
+ sqlite3-ruby (1.3.2)
48
+ tzinfo (0.3.23)
49
+
50
+ PLATFORMS
51
+ ruby
52
+
53
+ DEPENDENCIES
54
+ activerecord (>= 3.0.0)
55
+ rspec (>= 2.0.0)
56
+ ruby-debug19
57
+ sqlite3-ruby
58
+ validators!
data/README.rdoc ADDED
@@ -0,0 +1,73 @@
1
+ = Validators
2
+
3
+ Add some nice Rails 3 ActiveRecord validators.
4
+
5
+ == Installation
6
+
7
+ gem install validators
8
+
9
+ Then add it to your Gemfile:
10
+
11
+ gem "validators"
12
+
13
+ == Usage
14
+
15
+ === validates_email_format_of
16
+
17
+ class User < ActiveRecord::Base
18
+ # old fashion
19
+ validates_email_format_of :email
20
+
21
+ # alternative way
22
+ validates :email, :email => true
23
+ end
24
+
25
+ === validates_ownership_of
26
+
27
+ class Task < ActiveRecord::Base
28
+ belongs_to :user
29
+ belongs_to :category
30
+
31
+ validates_ownership_of :category, :with => :user
32
+ end
33
+
34
+ user = User.find(1)
35
+ another_user = User.find(2)
36
+
37
+ user_category = user.categories.first
38
+ another_user_category = another_user.categories.first
39
+
40
+ task = user.tasks.create(:category => user_category)
41
+ task.valid?
42
+ #=> true
43
+
44
+ task = user.tasks.create(:category => another_user_category)
45
+ task.valid?
46
+ #=> false
47
+
48
+ == Maintainer
49
+
50
+ * Nando Vieira - http://simplesideias.com.br
51
+
52
+ == License
53
+
54
+ (The MIT License)
55
+
56
+ Permission is hereby granted, free of charge, to any person obtaining
57
+ a copy of this software and associated documentation files (the
58
+ 'Software'), to deal in the Software without restriction, including
59
+ without limitation the rights to use, copy, modify, merge, publish,
60
+ distribute, sublicense, and/or sell copies of the Software, and to
61
+ permit persons to whom the Software is furnished to do so, subject to
62
+ the following conditions:
63
+
64
+ The above copyright notice and this permission notice shall be
65
+ included in all copies or substantial portions of the Software.
66
+
67
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
68
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
69
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
70
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
71
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
72
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
73
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
data/lib/validators.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "active_record"
2
+ require "validators/validates_email_format_of"
3
+ require "validators/validates_ownership_of"
4
+
5
+ module Validators
6
+ EMAIL_FORMAT = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
7
+ end
@@ -0,0 +1,26 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class EmailValidator < EachValidator
4
+ def validate_each(record, attribute, value)
5
+ if value.to_s !~ Validators::EMAIL_FORMAT
6
+ record.errors.add(
7
+ attribute, :invalid_email,
8
+ :message => options[:message], :value => value
9
+ )
10
+ end
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ # Validates weather or not the specified e-mail address is valid.
16
+ #
17
+ # class User < ActiveRecord::Base
18
+ # validates_email_format_of :email
19
+ # end
20
+ #
21
+ def validates_email_format_of(*attr_names)
22
+ validates_with EmailValidator, _merge_attributes(attr_names)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,51 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class OwnershipValidator < EachValidator
4
+ def validate_each(record, attribute, value)
5
+ owner = record.send(options[:with])
6
+ actual_owner = value ? value.send(options[:with]) : nil
7
+
8
+ if value && owner != actual_owner
9
+ record.errors.add(
10
+ attribute, :invalid_owner,
11
+ :message => options[:message]
12
+ )
13
+ end
14
+ end
15
+
16
+ def check_validity!
17
+ raise ArgumentError, ":with is required" unless options.key?(:with)
18
+ raise ArgumentError, ":with option must be a string or a symbol" unless ["String", "Symbol"].include?(options[:with].class.name)
19
+ end
20
+ end
21
+
22
+ module ClassMethods
23
+ # Validates whether the owner of the specified attribute is the same from the current object.
24
+ #
25
+ # class Task < ActiveRecord::Base
26
+ # belongs_to :user
27
+ # belongs_to :category
28
+ #
29
+ # validates_ownership_of :category, :with => :user
30
+ # end
31
+ #
32
+ # user = User.find(1)
33
+ # another_user = User.find(2)
34
+ #
35
+ # user_category = user.categories.first
36
+ # another_user_category = another_user.categories.first
37
+ #
38
+ # task = user.tasks.create(:category => user_category)
39
+ # task.valid?
40
+ # #=> true
41
+ #
42
+ # task = user.tasks.create(:category => another_user_category)
43
+ # task.valid?
44
+ # #=> false
45
+ #
46
+ def validates_ownership_of(*attr_names)
47
+ validates_with OwnershipValidator, _merge_attributes(attr_names)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,8 @@
1
+ module Validators
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+ end
8
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,13 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :users do |t|
3
+ t.string :email, :corporate_email
4
+ end
5
+
6
+ create_table :categories do |t|
7
+ t.references :user
8
+ end
9
+
10
+ create_table :tasks do |t|
11
+ t.references :user, :category
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ require "validators"
2
+
3
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
4
+
5
+ ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
6
+ load "schema.rb"
7
+
8
+ I18n.load_path << File.dirname(__FILE__) + "/support/translations.yml"
9
+
10
+ RSpec.configure do |config|
11
+ config.before do
12
+ I18n.locale = :en
13
+
14
+ ActiveRecord::Base.descendants.each do |model|
15
+ model.delete_all
16
+ Object.class_eval { remove_const model.name if const_defined?(model.name) }
17
+ end
18
+
19
+ load File.dirname(__FILE__) + "/support/models.rb"
20
+ end
21
+ end
@@ -0,0 +1,55 @@
1
+ INVALID_EMAILS = [
2
+ 'invalid@example-com',
3
+ # period can not start local part
4
+ '.invalid@example.com',
5
+ # period can not end local part
6
+ 'invalid.@example.com',
7
+ # period can not appear twice consecutively in local part
8
+ 'invali..d@example.com',
9
+ # should not allow underscores in domain names
10
+ 'invalid@ex_mple.com',
11
+ 'invalid@example.com.',
12
+ 'invalid@example.com_',
13
+ 'invalid@example.com-',
14
+ 'invalid-example.com',
15
+ 'invalid@example.b#r.com',
16
+ 'invalid@example.c',
17
+ 'invali d@example.com',
18
+ 'invalidexample.com',
19
+ 'invalid@example.',
20
+ # from http://tools.ietf.org/html/rfc3696, page 5
21
+ # corrected in http://www.rfc-editor.org/errata_search.php?rfc=3696
22
+ 'Fred\ Bloggs_@example.com',
23
+ 'Abc\@def+@example.com',
24
+ 'Joe.\\Blow@example.com'
25
+ ]
26
+
27
+ VALID_EMAILS = [
28
+ 'valid@example.com',
29
+ 'Valid@test.example.com',
30
+ 'valid+valid123@test.example.com',
31
+ 'valid_valid123@test.example.com',
32
+ 'valid-valid+123@test.example.co.uk',
33
+ 'valid-valid+1.23@test.example.com.au',
34
+ 'valid@example.co.uk',
35
+ 'v@example.com',
36
+ 'valid@example.ca',
37
+ 'valid_@example.com',
38
+ 'valid123.456@example.org',
39
+ 'valid123.456@example.travel',
40
+ 'valid123.456@example.museum',
41
+ 'valid@example.mobi',
42
+ 'valid@example.info',
43
+ 'valid-@example.com',
44
+ # from RFC 3696, page 6
45
+ 'customer/department=shipping@example.com',
46
+ '$A12345@example.com',
47
+ '!def!xyz%abc@example.com',
48
+ '_somename@example.com',
49
+ # apostrophes
50
+ "test'test@example.com",
51
+ # '"Abc\@def"@example.com',
52
+ # from http://www.rfc-editor.org/errata_search.php?rfc=3696
53
+ # '"Fred\ Bloggs"@example.com',
54
+ '"Joe.\\Blow"@example.com'
55
+ ]
@@ -0,0 +1,22 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :tasks
3
+ has_many :categories
4
+ end
5
+
6
+ class Category < ActiveRecord::Base
7
+ belongs_to :user
8
+ has_many :tasks
9
+ end
10
+
11
+ class Task < ActiveRecord::Base
12
+ belongs_to :user
13
+ belongs_to :category
14
+ end
15
+
16
+ class Buyer < ActiveRecord::Base
17
+ set_table_name :users
18
+ end
19
+
20
+ class Person < ActiveRecord::Base
21
+ set_table_name :users
22
+ end
@@ -0,0 +1,15 @@
1
+ en:
2
+ activerecord:
3
+ errors:
4
+ messages:
5
+ record_invalid: "Errors: %{errors}"
6
+ invalid_email: "is not a valid address"
7
+ invalid_owner: "is not associated with your user"
8
+
9
+ pt-BR:
10
+ activerecord:
11
+ errors:
12
+ messages:
13
+ record_invalid: "Erros: %{errors}"
14
+ invalid_email: "não parece ser um e-mail válido"
15
+ invalid_owner: "não está associado ao seu usuário"
@@ -0,0 +1,71 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe ".validates_email_format_of" do
5
+ before do
6
+ User.validates_email_format_of :email, :corporate_email, :allow_blank => false
7
+ Buyer.validates_email_format_of :email, :message => "is not a valid e-mail"
8
+ Person.validates :email, :email => true
9
+ end
10
+
11
+ VALID_EMAILS.each do |email|
12
+ it "should accept #{email.inspect} as a valid email" do
13
+ user = User.new(:email => email, :corporate_email => email)
14
+ user.should be_valid
15
+
16
+ user = Person.new(:email => email)
17
+ user.should be_valid
18
+ end
19
+ end
20
+
21
+ INVALID_EMAILS.each do |email|
22
+ it "should reject #{email.inspect} as a valid email" do
23
+ user = User.new(:email => "invalid", :corporate_email => "invalid")
24
+ user.should_not be_valid
25
+
26
+ user = Person.new(:email => "invalid")
27
+ user.should_not be_valid
28
+ end
29
+ end
30
+
31
+ it "should use default error message" do
32
+ user = User.new(:email => "invalid")
33
+ user.should_not be_valid
34
+ errors_for(user, :email).should == ["is not a valid address"]
35
+ end
36
+
37
+ it "should reject nil value" do
38
+ user = User.new(:email => nil)
39
+ user.should_not be_valid
40
+ errors_for(user, :email).should_not be_empty
41
+ end
42
+
43
+ it "should reject empty value" do
44
+ user = User.new(:email => "")
45
+ user.should_not be_valid
46
+ errors_for(user, :email).should_not be_empty
47
+ end
48
+
49
+ it "should validate multiple attributes" do
50
+ user = User.new(:corporate_email => "invalid")
51
+ user.should_not be_valid
52
+ errors_for(user, :corporate_email).should == ["is not a valid address"]
53
+ end
54
+
55
+ it "should use custom error message as :message options" do
56
+ buyer = Buyer.new(:email => "invalid")
57
+ buyer.should_not be_valid
58
+ errors_for(buyer, :email).should == ["is not a valid e-mail"]
59
+ end
60
+
61
+ it "should use I18n string as error message [pt-BR]" do
62
+ I18n.locale = :'pt-BR'
63
+ user = User.new(:email => "invalid")
64
+ user.should_not be_valid
65
+ errors_for(user, :email).should == ["não parece ser um e-mail válido"]
66
+ end
67
+
68
+ def errors_for(record, attr_name)
69
+ record.errors[attr_name]
70
+ end
71
+ end
@@ -0,0 +1,69 @@
1
+ require "spec_helper"
2
+
3
+ describe ".validates_ownership_of" do
4
+ let!(:user) { User.create! }
5
+ let!(:another_user) { User.create! }
6
+ let!(:category) { Category.create!(:user => user) }
7
+ let!(:another_category) { Category.create!(:user => another_user) }
8
+ subject { Task.new(:user => user, :category => category) }
9
+
10
+ before do
11
+ Task.validates_ownership_of :category, :with => :user
12
+ end
13
+
14
+ it "should be valid when record is owned by the correct user" do
15
+ subject.should be_valid
16
+ end
17
+
18
+ it "should not be valid when record is owned by a different user" do
19
+ subject.category = another_category
20
+ subject.should_not be_valid
21
+ end
22
+
23
+ it "should raise error without :with option" do
24
+ expect {
25
+ Task.validates_ownership_of :category
26
+ }.to raise_error(ArgumentError)
27
+ end
28
+
29
+ it "should raise error when :with options is not a valid type" do
30
+ expect {
31
+ Task.validates_ownership_of :category, :with => user
32
+ }.to raise_error(ArgumentError)
33
+ end
34
+
35
+ it "should not be valid when owner is not present" do
36
+ expect {
37
+ subject.user = nil
38
+ subject.should_not be_valid
39
+ }.to_not raise_error
40
+ end
41
+
42
+ it "should not be valid when attribute owner is not present" do
43
+ expect {
44
+ subject.category.user = nil
45
+ subject.should_not be_valid
46
+ }.to_not raise_error
47
+ end
48
+
49
+ it "should be valid when both owners are nil" do
50
+ expect {
51
+ subject.category.user = nil
52
+ subject.user = nil
53
+ subject.should be_valid
54
+ }.to_not raise_error
55
+ end
56
+
57
+ it "should be valid when attribute is nil" do
58
+ expect {
59
+ subject.category = nil
60
+ subject.should be_valid
61
+ }.to_not raise_error
62
+ end
63
+
64
+ it "should set error message" do
65
+ subject.user = nil
66
+ subject.should_not be_valid
67
+ subject.errors[:category].should == ["is not associated with your user"]
68
+ end
69
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "validators/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "validators"
7
+ s.version = Validators::Version::STRING
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Nando Vieira"]
10
+ s.email = ["fnando.vieira@gmail.com"]
11
+ s.homepage = "http://rubygems.org/gems/validators"
12
+ s.summary = "Add some nice Rails 3 ActiveRecord validators."
13
+ s.description = "Add some nice Rails 3 ActiveRecord validators."
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency "activerecord", ">= 3.0.0"
21
+ s.add_development_dependency "rspec", ">= 2.0.0"
22
+ s.add_development_dependency "sqlite3-ruby"
23
+ s.add_development_dependency "ruby-debug19"
24
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validators
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Nando Vieira
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-10 00:00:00 -02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 0
31
+ - 0
32
+ version: 3.0.0
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 2
45
+ - 0
46
+ - 0
47
+ version: 2.0.0
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: sqlite3-ruby
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: ruby-debug19
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ type: :development
75
+ version_requirements: *id004
76
+ description: Add some nice Rails 3 ActiveRecord validators.
77
+ email:
78
+ - fnando.vieira@gmail.com
79
+ executables: []
80
+
81
+ extensions: []
82
+
83
+ extra_rdoc_files: []
84
+
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - Gemfile.lock
89
+ - README.rdoc
90
+ - Rakefile
91
+ - lib/validators.rb
92
+ - lib/validators/validates_email_format_of.rb
93
+ - lib/validators/validates_ownership_of.rb
94
+ - lib/validators/version.rb
95
+ - spec/schema.rb
96
+ - spec/spec_helper.rb
97
+ - spec/support/emails.rb
98
+ - spec/support/models.rb
99
+ - spec/support/translations.yml
100
+ - spec/validators/validates_email_format_of_spec.rb
101
+ - spec/validators/validates_ownership_of_spec.rb
102
+ - validators.gemspec
103
+ has_rdoc: true
104
+ homepage: http://rubygems.org/gems/validators
105
+ licenses: []
106
+
107
+ post_install_message:
108
+ rdoc_options: []
109
+
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ requirements: []
129
+
130
+ rubyforge_project:
131
+ rubygems_version: 1.3.7
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: Add some nice Rails 3 ActiveRecord validators.
135
+ test_files:
136
+ - spec/schema.rb
137
+ - spec/spec_helper.rb
138
+ - spec/support/emails.rb
139
+ - spec/support/models.rb
140
+ - spec/support/translations.yml
141
+ - spec/validators/validates_email_format_of_spec.rb
142
+ - spec/validators/validates_ownership_of_spec.rb