validates_existence 0.5.3 → 0.5.4

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 ADDED
@@ -0,0 +1,41 @@
1
+ module Perfectline
2
+ module ValidatesExistence
3
+
4
+ module Rails2
5
+ def validates_existence_of(*attribute_names)
6
+ options = attribute_names.extract_options!.symbolize_keys
7
+ options[:message] ||= :existence
8
+ options[:both] = true unless options.has_key?(:both)
9
+
10
+ validates_each(attribute_names, options) do |record, attribute, value|
11
+ normalized = attribute.to_s.sub(/_id$/, "").to_sym
12
+ association = record.class.reflect_on_association(normalized)
13
+
14
+ if association.nil? or !association.belongs_to?
15
+ raise ArgumentError, "Cannot validate existence on #{normalized}, not a :belongs_to association"
16
+ end
17
+
18
+ target_class = nil
19
+
20
+ # dealing with polymorphic belongs_to
21
+ if association.options.has_key?(:foreign_type)
22
+ foreign_type = record.send(association.options.fetch(:foreign_type))
23
+ target_class = foreign_type.constantize unless foreign_type.nil?
24
+ else
25
+ target_class = association.klass
26
+ end
27
+
28
+ if target_class.nil? or !target_class.exists?(value)
29
+ record.errors.add(attribute, options[:message], :default => "does not exist")
30
+
31
+ # add the error on both :relation and :relation_id
32
+ if options[:both]
33
+ normalized = attribute.to_s.ends_with?("_id") ? normalized : "#{attribute}_id"
34
+ record.errors.add(normalized, options[:message], :default => "does not exist")
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
data/lib/rails3.rb ADDED
@@ -0,0 +1,57 @@
1
+ require 'active_support/concern'
2
+ require 'active_model'
3
+
4
+ module Perfectline
5
+ module ValidatesExistence
6
+
7
+ module Rails3
8
+ extend ActiveSupport::Concern
9
+
10
+ class ExistenceValidator < ActiveModel::EachValidator
11
+
12
+ def initialize(options)
13
+ # set the default message if its unspecified
14
+ options[:message] ||= :existence
15
+ options[:both] = true unless options.has_key?(:both)
16
+ super(options)
17
+ end
18
+
19
+ def validate_each(record, attribute, value)
20
+ normalized = attribute.to_s.sub(/_id$/, "").to_sym
21
+ association = record.class.reflect_on_association(normalized)
22
+
23
+ if association.nil? or !association.belongs_to?
24
+ raise ArgumentError, "Cannot validate existence on #{normalized}, not a :belongs_to association"
25
+ end
26
+
27
+ target_class = nil
28
+
29
+ # dealing with polymorphic belongs_to
30
+ if association.options.has_key?(:foreign_type)
31
+ foreign_type = record.send(association.options.fetch(:foreign_type))
32
+ target_class = foreign_type.constantize unless foreign_type.nil?
33
+ else
34
+ target_class = association.klass
35
+ end
36
+
37
+ if value.nil? or target_class.nil? or !target_class.exists?(value)
38
+ record.errors.add(attribute, options[:message], :message => "does not exist")
39
+
40
+ # add the error on both :relation and :relation_id
41
+ if options[:both]
42
+ normalized = attribute.to_s.ends_with?("_id") ? normalized : "#{attribute}_id"
43
+ record.errors.add(normalized, options[:message], :message => "does not exist")
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ module ClassMethods
50
+ def validates_existence_of(*attr_names)
51
+ validates_with ExistenceValidator, _merge_attributes(attr_names)
52
+ end
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -1,97 +1,7 @@
1
- module Perfectline
2
- module ValidatesExistence
3
-
4
- module Rails2
5
- def validates_existence_of(*attribute_names)
6
- options = attribute_names.extract_options!.symbolize_keys
7
- options[:message] ||= :existence
8
- options[:both] = true unless options.has_key?(:both)
9
-
10
- validates_each(attribute_names, options) do |record, attribute, value|
11
- normalized = attribute.to_s.sub(/_id$/, "").to_sym
12
- association = record.class.reflect_on_association(normalized)
13
-
14
- if association.nil? or !association.belongs_to?
15
- raise ArgumentError, "Cannot validate existence on #{normalized}, not a :belongs_to association"
16
- end
17
-
18
- target_class = nil
19
-
20
- # dealing with polymorphic belongs_to
21
- if association.options.has_key?(:foreign_type)
22
- foreign_type = record.send(association.options.fetch(:foreign_type))
23
- target_class = foreign_type.constantize unless foreign_type.nil?
24
- else
25
- target_class = association.klass
26
- end
27
-
28
- if target_class.nil? or !target_class.exists?(value)
29
- record.errors.add(attribute, options[:message], :default => "does not exist")
30
-
31
- # add the error on both :relation and :relation_id
32
- if options[:both]
33
- normalized = attribute.to_s.ends_with?("_id") ? normalized : "#{attribute}_id"
34
- record.errors.add(normalized, options[:message], :default => "does not exist")
35
- end
36
- end
37
- end
38
- end
39
- end
40
-
41
- module Rails3
42
- extend ActiveSupport::Concern
43
-
44
- class ExistenceValidator < ActiveModel::EachValidator
45
-
46
- def initialize(options)
47
- # set the default message if its unspecified
48
- options[:message] ||= :existence
49
- options[:both] ||= true
50
- super(options)
51
- end
52
-
53
- def validate_each(record, attribute, value)
54
- normalized = attribute.to_s.sub(/_id$/, "").to_sym
55
- association = record.class.reflect_on_association(normalized)
56
-
57
- if association.nil? or !association.belongs_to?
58
- raise ArgumentError, "Cannot validate existence on #{normalized}, not a :belongs_to association"
59
- end
60
-
61
- target_class = nil
62
-
63
- # dealing with polymorphic belongs_to
64
- if association.options.has_key?(:foreign_type)
65
- foreign_type = record.send(association.options.fetch(:foreign_type))
66
- target_class = foreign_type.constantize unless foreign_type.nil?
67
- else
68
- target_class = association.klass
69
- end
70
-
71
- if value.nil? or target_class.nil? or !target_class.exists?(value)
72
- record.errors.add(attribute, options[:message], :default => "does not exist")
73
-
74
- # add the error on both :relation and :relation_id
75
- if options[:both]
76
- normalized = attribute.to_s.ends_with?("_id") ? normalized : "#{attribute}_id"
77
- record.errors.add(normalized, options[:message], :default => "does not exist")
78
- end
79
- end
80
- end
81
- end
82
-
83
- module ClassMethods
84
- def validates_existence_of(*attr_names)
85
- validates_with ExistenceValidator, _merge_attributes(attr_names)
86
- end
87
- end
88
-
89
- end
90
- end
91
- end
92
-
93
1
  if Rails::VERSION::MAJOR >= 3
2
+ require 'rails3'
94
3
  ActiveRecord::Base.send(:include, Perfectline::ValidatesExistence::Rails3)
95
4
  else
5
+ require 'rails2'
96
6
  ActiveRecord::Base.send(:extend, Perfectline::ValidatesExistence::Rails2)
97
7
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 5
8
- - 3
9
- version: 0.5.3
8
+ - 4
9
+ version: 0.5.4
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-09-05 00:00:00 +03:00
18
+ date: 2010-10-02 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -32,17 +32,19 @@ extra_rdoc_files:
32
32
  files:
33
33
  - README.markdown
34
34
  - install.rb
35
+ - lib/rails2.rb
36
+ - lib/rails3.rb
35
37
  - lib/validates_existence.rb
36
38
  - rails/init.rb
37
- - test/models/user_with_poly.rb
38
39
  - test/models/name.rb
40
+ - test/models/user.rb
39
41
  - test/models/user_with_allow_nil.rb
42
+ - test/models/user_with_both.rb
40
43
  - test/models/user_with_has_many.rb
44
+ - test/models/user_with_poly.rb
41
45
  - test/models/user_with_poly_allow_nil.rb
42
- - test/models/user_with_both.rb
43
- - test/models/user.rb
44
- - test/validates_existence_test.rb
45
46
  - test/test_helper.rb
47
+ - test/validates_existence_test.rb
46
48
  has_rdoc: true
47
49
  homepage: http://github.com/perfectline/validates_existence/tree/master
48
50
  licenses: []
@@ -76,12 +78,12 @@ signing_key:
76
78
  specification_version: 3
77
79
  summary: Validates Rails model belongs_to association existence.
78
80
  test_files:
79
- - test/models/user_with_poly.rb
80
81
  - test/models/name.rb
82
+ - test/models/user.rb
81
83
  - test/models/user_with_allow_nil.rb
84
+ - test/models/user_with_both.rb
82
85
  - test/models/user_with_has_many.rb
86
+ - test/models/user_with_poly.rb
83
87
  - test/models/user_with_poly_allow_nil.rb
84
- - test/models/user_with_both.rb
85
- - test/models/user.rb
86
- - test/validates_existence_test.rb
87
88
  - test/test_helper.rb
89
+ - test/validates_existence_test.rb