validates_existence 0.5.5 → 0.6.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/lib/rails2.rb +5 -5
- data/lib/rails3.rb +5 -6
- data/test/{models → rails2/models}/name.rb +0 -0
- data/test/{models → rails2/models}/user.rb +1 -1
- data/test/{models → rails2/models}/user_with_allow_nil.rb +0 -0
- data/test/{models → rails2/models}/user_with_both.rb +0 -0
- data/test/rails2/models/user_with_fk.rb +9 -0
- data/test/{models → rails2/models}/user_with_has_many.rb +0 -0
- data/test/{models → rails2/models}/user_with_poly.rb +0 -0
- data/test/{models → rails2/models}/user_with_poly_allow_nil.rb +0 -0
- data/test/{test_helper.rb → rails2/test_helper.rb} +6 -3
- data/test/{validates_existence_test.rb → rails2/validates_existence_test.rb} +25 -2
- data/test/rails3/models/name.rb +3 -0
- data/test/rails3/models/user.rb +7 -0
- data/test/rails3/models/user_with_allow_nil.rb +9 -0
- data/test/rails3/models/user_with_both.rb +7 -0
- data/test/rails3/models/user_with_fk.rb +9 -0
- data/test/rails3/models/user_with_has_many.rb +7 -0
- data/test/rails3/models/user_with_poly.rb +9 -0
- data/test/rails3/models/user_with_poly_allow_nil.rb +9 -0
- data/test/rails3/test_helper.rb +27 -0
- data/test/rails3/validates_existence_test.rb +96 -0
- metadata +46 -24
data/lib/rails2.rb
CHANGED
@@ -5,7 +5,7 @@ module Perfectline
|
|
5
5
|
def validates_existence_of(*attribute_names)
|
6
6
|
options = attribute_names.extract_options!.symbolize_keys
|
7
7
|
options[:message] ||= :existence
|
8
|
-
options[:both] = true unless options.
|
8
|
+
options[:both] = true unless options.key?(:both)
|
9
9
|
|
10
10
|
validates_each(attribute_names, options) do |record, attribute, value|
|
11
11
|
normalized = attribute.to_s.sub(/_id$/, "").to_sym
|
@@ -30,11 +30,11 @@ module Perfectline
|
|
30
30
|
|
31
31
|
# add the error on both :relation and :relation_id
|
32
32
|
if options[:both]
|
33
|
-
|
34
|
-
errors.push(normalized) unless errors.include? attribute
|
33
|
+
errors.push(attribute.to_s.ends_with?("_id") ? normalized : association.primary_key_name)
|
35
34
|
end
|
36
|
-
|
37
|
-
|
35
|
+
|
36
|
+
errors.each do |error|
|
37
|
+
record.errors.add(error, options[:message], :default => "does not exist")
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
data/lib/rails3.rb
CHANGED
@@ -12,7 +12,7 @@ module Perfectline
|
|
12
12
|
def initialize(options)
|
13
13
|
# set the default message if its unspecified
|
14
14
|
options[:message] ||= :existence
|
15
|
-
options[:both] = true unless options.
|
15
|
+
options[:both] = true unless options.key?(:both)
|
16
16
|
super(options)
|
17
17
|
end
|
18
18
|
|
@@ -35,15 +35,14 @@ module Perfectline
|
|
35
35
|
end
|
36
36
|
|
37
37
|
if value.nil? or target_class.nil? or !target_class.exists?(value)
|
38
|
-
|
38
|
+
errors = [attribute]
|
39
39
|
|
40
40
|
# add the error on both :relation and :relation_id
|
41
41
|
if options[:both]
|
42
|
-
|
43
|
-
errors.push(normalized) unless errors.include? attribute
|
42
|
+
errors.push(attribute.to_s.ends_with?("_id") ? normalized : association.primary_key_name)
|
44
43
|
end
|
45
|
-
errors.each do |
|
46
|
-
record.errors.add(
|
44
|
+
errors.each do |error|
|
45
|
+
record.errors.add(error, options[:message], :message => "does not exist")
|
47
46
|
end
|
48
47
|
end
|
49
48
|
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
|
3
|
-
gem '
|
3
|
+
gem 'rails', '2.3.8'
|
4
4
|
|
5
5
|
require 'sqlite3'
|
6
6
|
require 'test/unit'
|
@@ -13,7 +13,9 @@ ActiveRecord::Base.establish_connection(
|
|
13
13
|
"database" => ":memory:"
|
14
14
|
)
|
15
15
|
|
16
|
-
require File.join(File.dirname(__FILE__), '..', '
|
16
|
+
require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'rails2.rb')
|
17
|
+
|
18
|
+
ActiveRecord::Base.send(:extend, Perfectline::ValidatesExistence::Rails2)
|
17
19
|
|
18
20
|
autoload :Name, File.join(File.dirname(__FILE__), 'models', 'name.rb')
|
19
21
|
autoload :User, File.join(File.dirname(__FILE__), 'models', 'user.rb')
|
@@ -21,4 +23,5 @@ autoload :UserWithAllowNil, File.join(File.dirname(__FILE__), 'models', 'use
|
|
21
23
|
autoload :UserWithPoly, File.join(File.dirname(__FILE__), 'models', 'user_with_poly.rb')
|
22
24
|
autoload :UserWithPolyAllowNil, File.join(File.dirname(__FILE__), 'models', 'user_with_poly_allow_nil.rb')
|
23
25
|
autoload :UserWithHasMany, File.join(File.dirname(__FILE__), 'models', 'user_with_has_many.rb')
|
24
|
-
autoload :UserWithBoth, File.join(File.dirname(__FILE__), 'models', 'user_with_both.rb')
|
26
|
+
autoload :UserWithBoth, File.join(File.dirname(__FILE__), 'models', 'user_with_both.rb')
|
27
|
+
autoload :UserWithFk, File.join(File.dirname(__FILE__), 'models', 'user_with_fk.rb')
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper.rb'
|
2
2
|
|
3
3
|
class TestValidatesExistence < Test::Unit::TestCase
|
4
4
|
|
@@ -14,6 +14,10 @@ class TestValidatesExistence < Test::Unit::TestCase
|
|
14
14
|
t.references :relation, :polymorphic => true
|
15
15
|
end
|
16
16
|
|
17
|
+
create_table :users2, :force => true do |t|
|
18
|
+
t.column :custom_id, :integer
|
19
|
+
end
|
20
|
+
|
17
21
|
end
|
18
22
|
end
|
19
23
|
|
@@ -26,7 +30,7 @@ class TestValidatesExistence < Test::Unit::TestCase
|
|
26
30
|
user = User.new
|
27
31
|
assert_equal user.save, false
|
28
32
|
assert_not_nil user.errors.on(:name)
|
29
|
-
assert_not_nil user.errors.on(:
|
33
|
+
assert_not_nil user.errors.on(:name_id)
|
30
34
|
end
|
31
35
|
|
32
36
|
def test_save_with_relation
|
@@ -64,10 +68,29 @@ class TestValidatesExistence < Test::Unit::TestCase
|
|
64
68
|
def test_errors_on_one_field
|
65
69
|
user = UserWithBoth.new
|
66
70
|
user.save
|
71
|
+
|
67
72
|
assert_not_nil user.errors.on(:name)
|
68
73
|
assert_nil user.errors.on(:name_id)
|
69
74
|
end
|
70
75
|
|
76
|
+
def test_save_with_new_record
|
77
|
+
name = Name.create(:name => "foobar")
|
78
|
+
user = User.create(:name => name)
|
79
|
+
|
80
|
+
user.name = Name.new
|
81
|
+
user.save
|
82
|
+
|
83
|
+
assert_not_nil user.errors.on(:name)
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_save_with_custom_fk
|
87
|
+
user = UserWithFk.new
|
88
|
+
user.save
|
89
|
+
|
90
|
+
assert_not_nil user.errors.on(:name)
|
91
|
+
assert_not_nil user.errors.on(:custom_id)
|
92
|
+
end
|
93
|
+
|
71
94
|
end
|
72
95
|
|
73
96
|
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
gem 'rails', '3.0.5'
|
4
|
+
|
5
|
+
require 'sqlite3'
|
6
|
+
require 'test/unit'
|
7
|
+
require 'active_record'
|
8
|
+
require 'active_record/base'
|
9
|
+
|
10
|
+
ActiveRecord::Migration.verbose = false
|
11
|
+
ActiveRecord::Base.establish_connection(
|
12
|
+
"adapter" => "sqlite3",
|
13
|
+
"database" => ":memory:"
|
14
|
+
)
|
15
|
+
|
16
|
+
require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'rails3.rb')
|
17
|
+
|
18
|
+
ActiveRecord::Base.send(:include, Perfectline::ValidatesExistence::Rails3)
|
19
|
+
|
20
|
+
autoload :Name, File.join(File.dirname(__FILE__), 'models', 'name.rb')
|
21
|
+
autoload :User, File.join(File.dirname(__FILE__), 'models', 'user.rb')
|
22
|
+
autoload :UserWithAllowNil, File.join(File.dirname(__FILE__), 'models', 'user_with_allow_nil.rb')
|
23
|
+
autoload :UserWithPoly, File.join(File.dirname(__FILE__), 'models', 'user_with_poly.rb')
|
24
|
+
autoload :UserWithPolyAllowNil, File.join(File.dirname(__FILE__), 'models', 'user_with_poly_allow_nil.rb')
|
25
|
+
autoload :UserWithHasMany, File.join(File.dirname(__FILE__), 'models', 'user_with_has_many.rb')
|
26
|
+
autoload :UserWithBoth, File.join(File.dirname(__FILE__), 'models', 'user_with_both.rb')
|
27
|
+
autoload :UserWithFk, File.join(File.dirname(__FILE__), 'models', 'user_with_fk.rb')
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'test_helper.rb'
|
2
|
+
|
3
|
+
class TestValidatesExistence < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
ActiveRecord::Schema.define(:version => 1) do
|
7
|
+
|
8
|
+
create_table :names, :force => true do |t|
|
9
|
+
t.column :name, :string
|
10
|
+
end
|
11
|
+
|
12
|
+
create_table :users, :force => true do |t|
|
13
|
+
t.references :name
|
14
|
+
t.references :relation, :polymorphic => true
|
15
|
+
end
|
16
|
+
|
17
|
+
create_table :users2, :force => true do |t|
|
18
|
+
t.column :custom_id, :integer
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def teardown
|
25
|
+
ActiveRecord::Base.connection.drop_table(:users)
|
26
|
+
ActiveRecord::Base.connection.drop_table(:names)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_save_with_no_relation
|
30
|
+
user = User.new
|
31
|
+
assert_equal user.save, false
|
32
|
+
assert_not_nil user.errors[:name]
|
33
|
+
assert_not_nil user.errors[:name_id]
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_save_with_relation
|
37
|
+
name = Name.create(:name => "foo")
|
38
|
+
assert_equal User.new(:name => name).save, true
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_save_with_bogus_id
|
42
|
+
assert_equal User.new(:name_id => 100).save, false
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_allow_nil
|
46
|
+
assert_equal UserWithAllowNil.new.save, true
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_poly_relation
|
50
|
+
assert_equal UserWithPoly.new.save, false
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_poly_relation_with_name
|
54
|
+
name = Name.create(:name => "bar")
|
55
|
+
assert_equal UserWithPoly.new(:relation => name).save, true
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_poly_relation_with_allow_nil
|
59
|
+
assert_equal UserWithPolyAllowNil.new.save, true
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_argument_error
|
63
|
+
assert_raise ArgumentError do
|
64
|
+
UserWithHasMany.new.save
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_errors_on_one_field
|
69
|
+
user = UserWithBoth.new
|
70
|
+
user.save
|
71
|
+
|
72
|
+
assert_not_nil user.errors[:name]
|
73
|
+
assert_empty user.errors[:name_id]
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_save_with_new_record
|
77
|
+
name = Name.create(:name => "foobar")
|
78
|
+
user = User.create(:name => name)
|
79
|
+
|
80
|
+
user.name = Name.new
|
81
|
+
user.save
|
82
|
+
|
83
|
+
assert_not_nil user.errors[:name]
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_save_with_custom_fk
|
87
|
+
user = UserWithFk.new
|
88
|
+
user.save
|
89
|
+
|
90
|
+
assert_not_nil user.errors[:name]
|
91
|
+
assert_not_nil user.errors[:custom_id]
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 6
|
8
|
+
- 0
|
9
|
+
version: 0.6.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Tanel Suurhans
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-03-02 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -36,22 +36,33 @@ files:
|
|
36
36
|
- lib/rails3.rb
|
37
37
|
- lib/validates_existence.rb
|
38
38
|
- rails/init.rb
|
39
|
-
- test/models/name.rb
|
40
|
-
- test/models/user.rb
|
41
|
-
- test/models/user_with_allow_nil.rb
|
42
|
-
- test/models/user_with_both.rb
|
43
|
-
- test/models/
|
44
|
-
- test/models/
|
45
|
-
- test/models/
|
46
|
-
- test/
|
47
|
-
- test/
|
39
|
+
- test/rails2/models/name.rb
|
40
|
+
- test/rails2/models/user.rb
|
41
|
+
- test/rails2/models/user_with_allow_nil.rb
|
42
|
+
- test/rails2/models/user_with_both.rb
|
43
|
+
- test/rails2/models/user_with_fk.rb
|
44
|
+
- test/rails2/models/user_with_has_many.rb
|
45
|
+
- test/rails2/models/user_with_poly.rb
|
46
|
+
- test/rails2/models/user_with_poly_allow_nil.rb
|
47
|
+
- test/rails2/test_helper.rb
|
48
|
+
- test/rails2/validates_existence_test.rb
|
49
|
+
- test/rails3/models/name.rb
|
50
|
+
- test/rails3/models/user.rb
|
51
|
+
- test/rails3/models/user_with_allow_nil.rb
|
52
|
+
- test/rails3/models/user_with_both.rb
|
53
|
+
- test/rails3/models/user_with_fk.rb
|
54
|
+
- test/rails3/models/user_with_has_many.rb
|
55
|
+
- test/rails3/models/user_with_poly.rb
|
56
|
+
- test/rails3/models/user_with_poly_allow_nil.rb
|
57
|
+
- test/rails3/test_helper.rb
|
58
|
+
- test/rails3/validates_existence_test.rb
|
48
59
|
has_rdoc: true
|
49
60
|
homepage: http://github.com/perfectline/validates_existence/tree/master
|
50
61
|
licenses: []
|
51
62
|
|
52
63
|
post_install_message:
|
53
|
-
rdoc_options:
|
54
|
-
|
64
|
+
rdoc_options: []
|
65
|
+
|
55
66
|
require_paths:
|
56
67
|
- lib
|
57
68
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -78,12 +89,23 @@ signing_key:
|
|
78
89
|
specification_version: 3
|
79
90
|
summary: Validates Rails model belongs_to association existence.
|
80
91
|
test_files:
|
81
|
-
- test/models/name.rb
|
82
|
-
- test/models/user.rb
|
83
|
-
- test/models/user_with_allow_nil.rb
|
84
|
-
- test/models/user_with_both.rb
|
85
|
-
- test/models/
|
86
|
-
- test/models/
|
87
|
-
- test/models/
|
88
|
-
- test/
|
89
|
-
- test/
|
92
|
+
- test/rails2/models/name.rb
|
93
|
+
- test/rails2/models/user.rb
|
94
|
+
- test/rails2/models/user_with_allow_nil.rb
|
95
|
+
- test/rails2/models/user_with_both.rb
|
96
|
+
- test/rails2/models/user_with_fk.rb
|
97
|
+
- test/rails2/models/user_with_has_many.rb
|
98
|
+
- test/rails2/models/user_with_poly.rb
|
99
|
+
- test/rails2/models/user_with_poly_allow_nil.rb
|
100
|
+
- test/rails2/test_helper.rb
|
101
|
+
- test/rails2/validates_existence_test.rb
|
102
|
+
- test/rails3/models/name.rb
|
103
|
+
- test/rails3/models/user.rb
|
104
|
+
- test/rails3/models/user_with_allow_nil.rb
|
105
|
+
- test/rails3/models/user_with_both.rb
|
106
|
+
- test/rails3/models/user_with_fk.rb
|
107
|
+
- test/rails3/models/user_with_has_many.rb
|
108
|
+
- test/rails3/models/user_with_poly.rb
|
109
|
+
- test/rails3/models/user_with_poly_allow_nil.rb
|
110
|
+
- test/rails3/test_helper.rb
|
111
|
+
- test/rails3/validates_existence_test.rb
|