association_validator 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,14 +7,15 @@ validates :user_id, :presence => true, :association => true</code></pre>
7
7
  You can also set the message using the :message option:
8
8
  <pre><code>validates :user_id, :association => {:message => 'was unretrievable'}</code></pre>
9
9
 
10
- For polymorphic classes, pass your class_name option as a proc:
10
+ For polymorphic classes, pass your class_name option as a proc or use the new polymorphic_class option
11
11
  <pre><code>class Item < ActiveRecord::Base
12
- validates :owner_id, :presence => true, :association => {:class_name => Proc.new {|r| r.owner_type}}
13
- end
14
- # == Schema Information
15
- # Table name: items
16
- # owner_id :integer
17
- # owner_type :string</code></pre>
12
+ # The new way to do polymorphic resource validation
13
+ validates :owner_id, :presence => true, :association => {:polymorphic_class => 'owner_type'}
14
+ # it calls record.send(:polymorphic_class)
15
+
16
+ # If for some odd reasson you need to use a proc:
17
+ validates :user_id, :presence => true, :association => {:class_name => Proc.new {|r| r.user_type || "User" }}
18
+ end</code></pre>
18
19
 
19
20
  h1. How to install
20
21
 
data/README.txt CHANGED
@@ -6,14 +6,15 @@ Validate that the id field is a saved record. It will ignore blank fields, so us
6
6
  You can also set the message using the :message option:
7
7
  validates :user_id, :association => {:message => 'was unretrievable'}
8
8
 
9
- For polymorphic classes, pass your class_name option as a proc:
9
+ For polymorphic classes, pass your class_name option as a proc or use the new polymorphic_class option
10
10
  class Item < ActiveRecord::Base
11
- validates :owner_id, :presence => true, :association => {:class_name => Proc.new {|r| r.owner_type}}
11
+ # The new way to do polymorphic resource validation
12
+ validates :owner_id, :presence => true, :association => {:polymorphic_class => 'owner_type'}
13
+ # it calls record.send(:polymorphic_class)
14
+
15
+ # If for some odd reasson you need to use a proc:
16
+ validates :user_id, :presence => true, :association => {:class_name => Proc.new {|r| r.user_type || "User" }}
12
17
  end
13
- # == Schema Information
14
- # Table name: items
15
- # owner_id :integer
16
- # owner_type :string
17
18
 
18
19
  === How to install
19
20
  Add the following line to your Gemfile and install.
data/Rakefile CHANGED
@@ -2,9 +2,9 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('association_validator', '0.6.0') do |p|
5
+ Echoe.new('association_validator', '0.6.1') do |p|
6
6
  p.description = "Rails 3 assocation validation for id fields"
7
- p.url = "http://github.com/TheEmpty"
7
+ p.url = "http://rubygems.org/gems/association_validator"
8
8
  p.author = "Mohammad El-Abid"
9
9
  p.email = "mohammad {dot} elabid {at} gmail"
10
10
  p.ignore_pattern = []
@@ -2,16 +2,16 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{association_validator}
5
- s.version = "0.6.0"
5
+ s.version = "0.6.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Mohammad El-Abid"]
9
- s.date = %q{2011-03-23}
9
+ s.date = %q{2011-03-24}
10
10
  s.description = %q{Rails 3 assocation validation for id fields}
11
11
  s.email = %q{mohammad {dot} elabid {at} gmail}
12
12
  s.extra_rdoc_files = ["README.textile", "README.txt", "lib/association_validator.rb"]
13
13
  s.files = ["Manifest", "README.textile", "README.txt", "Rakefile", "association_validator.gemspec", "lib/association_validator.rb"]
14
- s.homepage = %q{http://github.com/TheEmpty}
14
+ s.homepage = %q{http://rubygems.org/gems/association_validator}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Association_validator", "--main", "README.textile"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{association_validator}
@@ -1,16 +1,25 @@
1
1
  class AssociationValidator < ActiveModel::EachValidator
2
- def validate_each(record, attribute, value)
3
- return if record.send(attribute).blank? # return if value.blank? # use :presence => true if you want to confirm that something is there
2
+ def validate_each(record, attribute, record_id)
3
+ return if record.send(attribute).blank? # return if there is nothing to validate
4
+ # use :presence => true if you want to confirm that something is there
4
5
 
5
- if options[:class_name].blank?
6
+ if not options[:polymorphic_class].blank?
7
+ attribute_string = record.send(options[:polymorphic_class])
8
+ return if attribute_string.blank?
9
+
10
+ elsif options[:class_name].blank? # if class_name is not specified, assume that it's the attirbute without the last 3 characters which should be _id
6
11
  attribute_string = attribute.to_s
7
12
  attribute_string[-3,3] = ''
13
+
8
14
  elsif options[:class_name].is_a?(String)
9
15
  attribute_string = options[:class_name]
10
- elsif options[:class_name].is_a?(Proc)
11
- attribute_string = options[:class_name].yield(record)
12
- else
13
- raise "Association Validator does not support a(n) #{options[:class_name].class.to_s.humanize} as a class_name option"
16
+
17
+ elsif options[:class_name].is_a?(Proc)
18
+ attribute_string = options[:class_name].yield(record)
19
+ return if attribute_string.blank?
20
+
21
+ else
22
+ raise "Association Validator does not support a(n) #{options[:class_name].class.to_s.humanize} as a class_name option"
14
23
  end
15
24
 
16
25
  c = attribute_string.camelize.constantize # create a refrence to the class
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: association_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-03-23 00:00:00.000000000 -04:00
12
+ date: 2011-03-24 00:00:00.000000000 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activemodel
17
- requirement: &30028020 !ruby/object:Gem::Requirement
17
+ requirement: &25441140 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: 3.0.0
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *30028020
25
+ version_requirements: *25441140
26
26
  description: Rails 3 assocation validation for id fields
27
27
  email: mohammad {dot} elabid {at} gmail
28
28
  executables: []
@@ -39,7 +39,7 @@ files:
39
39
  - association_validator.gemspec
40
40
  - lib/association_validator.rb
41
41
  has_rdoc: true
42
- homepage: http://github.com/TheEmpty
42
+ homepage: http://rubygems.org/gems/association_validator
43
43
  licenses: []
44
44
  post_install_message:
45
45
  rdoc_options: