validates_existence 0.5.5 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
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.has_key?(:both)
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
- normalized = attribute.to_s.ends_with?("_id") ? normalized : "#{attribute}_id"
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
- errors.each do | error |
37
- record.errors.add(normalized, options[:message], :default => "does not exist")
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.has_key?(:both)
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
- errors = [attribute]
38
+ errors = [attribute]
39
39
 
40
40
  # add the error on both :relation and :relation_id
41
41
  if options[:both]
42
- normalized = attribute.to_s.ends_with?("_id") ? normalized : "#{attribute}_id"
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 | error |
46
- record.errors.add(normalized, options[:message], :default => "does not exist")
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
@@ -2,6 +2,6 @@ class User < ActiveRecord::Base
2
2
 
3
3
  belongs_to :name
4
4
 
5
- validates_existence_of :name, :both => false
5
+ validates_existence_of :name
6
6
 
7
7
  end
File without changes
@@ -0,0 +1,9 @@
1
+ class UserWithFk < ActiveRecord::Base
2
+
3
+ self.table_name = "users2"
4
+
5
+ belongs_to :name, :foreign_key => :custom_id
6
+
7
+ validates_existence_of :name
8
+
9
+ end
File without changes
@@ -1,6 +1,6 @@
1
1
  require 'rubygems'
2
2
 
3
- gem 'activerecord', '~> 2'
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__), '..', 'rails', 'init.rb')
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 File.join(File.dirname(__FILE__), 'test_helper.rb')
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(:name)
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,3 @@
1
+ class Name < ActiveRecord::Base
2
+
3
+ end
@@ -0,0 +1,7 @@
1
+ class User < ActiveRecord::Base
2
+
3
+ belongs_to :name
4
+
5
+ validates :name, :existence => true
6
+
7
+ end
@@ -0,0 +1,9 @@
1
+ class UserWithAllowNil < ActiveRecord::Base
2
+
3
+ self.table_name = "users"
4
+
5
+ belongs_to :name
6
+
7
+ validates :name, :existence => true, :allow_nil => true
8
+
9
+ end
@@ -0,0 +1,7 @@
1
+ class UserWithBoth < ActiveRecord::Base
2
+ self.table_name = "users"
3
+
4
+ belongs_to :name
5
+
6
+ validates :name, :existence => {:both => false}
7
+ end
@@ -0,0 +1,9 @@
1
+ class UserWithFk < ActiveRecord::Base
2
+
3
+ self.table_name = "users2"
4
+
5
+ belongs_to :name, :foreign_key => :custom_id
6
+
7
+ validates :name, :existence => true
8
+
9
+ end
@@ -0,0 +1,7 @@
1
+ class UserWithHasMany < ActiveRecord::Base
2
+ self.table_name = "users"
3
+
4
+ has_many :names
5
+
6
+ validates :names, :existence => true
7
+ end
@@ -0,0 +1,9 @@
1
+ class UserWithPoly < ActiveRecord::Base
2
+
3
+ self.table_name = "users"
4
+
5
+ belongs_to :relation, :polymorphic => true
6
+
7
+ validates :relation, :existence => true
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ class UserWithPolyAllowNil < ActiveRecord::Base
2
+
3
+ self.table_name = "users"
4
+
5
+ belongs_to :relation, :polymorphic => true
6
+
7
+ validates :relation, :existence => true, :allow_nil => true
8
+
9
+ end
@@ -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
- - 5
8
- - 5
9
- version: 0.5.5
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: 2010-12-10 00:00:00 +02:00
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/user_with_has_many.rb
44
- - test/models/user_with_poly.rb
45
- - test/models/user_with_poly_allow_nil.rb
46
- - test/test_helper.rb
47
- - test/validates_existence_test.rb
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
- - --charset=UTF-8
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/user_with_has_many.rb
86
- - test/models/user_with_poly.rb
87
- - test/models/user_with_poly_allow_nil.rb
88
- - test/test_helper.rb
89
- - test/validates_existence_test.rb
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