validates_existence 0.7.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.markdown +2 -2
  2. data/lib/rails2.rb +8 -1
  3. data/lib/rails3.rb +6 -1
  4. metadata +3 -3
@@ -2,7 +2,7 @@
2
2
 
3
3
  This plugin library adds ActiveRecord models a way to check if a `:belongs_to` association actually exists upon saving.
4
4
  This is achieved via adding a `validates_existence_of` validator to the base validations module.
5
- It also supports `:allow_nil => true/false` and `:polymorphic => true` associations.
5
+ It also supports `:allow_nil => true/false`, `:allow_new => true/false`, and `:polymorphic => true` associations.
6
6
 
7
7
  Version 0.4.0 also adds Rails 3 support (the appropriate version is used automatically).
8
8
  Version 0.5.0 introduces a new option - `:both` and changes the default behaviour of error attaching.
@@ -94,4 +94,4 @@ This plugin is inspired by ideas from **Josh Susser**
94
94
  **Tarmo Lehtpuu** - tarmo.lehtpuu_at_perfectline_d0t_ee
95
95
 
96
96
  ## License
97
- Copyright 2010 by PerfectLine LLC (<http://www.perfectline.co.uk>) and is released under the MIT license.
97
+ Copyright 2010 by PerfectLine LLC (<http://www.perfectline.co.uk>) and is released under the MIT license.
@@ -6,6 +6,7 @@ module Perfectline
6
6
  options = attribute_names.extract_options!.symbolize_keys
7
7
  options[:message] ||= :existence
8
8
  options[:both] = true unless options.key?(:both)
9
+ options[:allow_new] = false unless options.key?(:allow_new)
9
10
 
10
11
  validates_each(attribute_names, options) do |record, attribute, value|
11
12
  normalized = attribute.to_s.sub(/_id$/, "").to_sym
@@ -25,7 +26,13 @@ module Perfectline
25
26
  target_class = association.klass
26
27
  end
27
28
 
28
- if target_class.nil? or !target_class.exists?(value)
29
+ if options[:allow_new]
30
+ exists = value.new_record? or target_class.exists?(value)
31
+ else
32
+ exists = target_class.exists?(value)
33
+ end
34
+
35
+ if target_class.nil? or !exists
29
36
  errors = [attribute]
30
37
 
31
38
  # add the error on both :relation and :relation_id
@@ -13,6 +13,7 @@ module Perfectline
13
13
  # set the default message if its unspecified
14
14
  options[:message] ||= :existence
15
15
  options[:both] = true unless options.key?(:both)
16
+ options[:allow_new] = false unless options.key?(:allow_new)
16
17
  super(options)
17
18
  end
18
19
 
@@ -34,7 +35,7 @@ module Perfectline
34
35
  target_class = association.klass
35
36
  end
36
37
 
37
- if value.nil? || target_class.nil? || !target_class.exists?(value)
38
+ if value.nil? || target_class.nil? || !exists?(target_class, value)
38
39
  errors = [attribute]
39
40
 
40
41
  # add the error on both :relation and :relation_id
@@ -55,6 +56,10 @@ module Perfectline
55
56
  end
56
57
  end
57
58
  end
59
+
60
+ def exists?(target_class, value)
61
+ (options[:allow_new] && value.new_record?) || target_class.exists?(value)
62
+ end
58
63
  end
59
64
 
60
65
  module ClassMethods
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_existence
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-10-26 00:00:00.000000000Z
13
+ date: 2012-11-08 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: A library for validating model association existence.
16
16
  email:
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
47
  version: '0'
48
48
  requirements: []
49
49
  rubyforge_project:
50
- rubygems_version: 1.8.6
50
+ rubygems_version: 1.8.24
51
51
  signing_key:
52
52
  specification_version: 3
53
53
  summary: Validates Rails model belongs_to association existence.