validates-belongs-to 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in validates-belongs-to.gemspec
4
+ gemspec
5
+
6
+ gem 'rake'
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ Rails 3 validation to ensure that two ActiveModel associations are related to each other.
2
+
3
+ class JobApplication
4
+ belongs_to :resume
5
+ belongs_to :user
6
+ validates :resume, :belongs_to => :user
7
+ end
8
+
9
+ Installation:
10
+
11
+ gem 'validates-belongs-to'
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new
@@ -0,0 +1,25 @@
1
+ require 'active_model'
2
+
3
+ # Validation to ensure that two ActiveModel associations are related to each other.
4
+ #
5
+ # class JobApplication
6
+ # belongs_to :resume
7
+ # belongs_to :user
8
+ # validates :resume, :belongs_to => :user
9
+ # end
10
+ class BelongsToValidator < ActiveModel::EachValidator
11
+ def check_validity!
12
+ unless options[:with]
13
+ raise ArgumentError, "An association must be specified."
14
+ end
15
+ end
16
+ def validate_each(record, attribute, value)
17
+ if value
18
+ if common_owner = record.send(options[:with])
19
+ unless common_owner == value.send(options[:with])
20
+ record.errors.add(attribute, :belongs_to, options.merge(:value => value))
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ module Validates
2
+ module Belongs
3
+ module To
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,54 @@
1
+ require 'test/unit'
2
+ require 'validates-belongs-to'
3
+
4
+ class ValidatesBelongsToTest < Test::Unit::TestCase
5
+ module HelperClassMethods
6
+ def belongs_to(*args)
7
+ attr_accessor(*args)
8
+ end
9
+ end
10
+ class TheModel
11
+ include ActiveModel::Validations
12
+ extend HelperClassMethods
13
+ validates :thing1, :belongs_to => :thing2
14
+ belongs_to :thing1, :thing2
15
+ end
16
+ class Thing1
17
+ extend HelperClassMethods
18
+ belongs_to :thing2
19
+ end
20
+
21
+ def setup
22
+ @obj = TheModel.new
23
+ end
24
+ attr_reader :obj
25
+
26
+ def test_everything_nil_is_ok
27
+ assert obj.valid?, 'valid with no associated objects'
28
+ end
29
+
30
+ def test_thing2_nil_is_ok
31
+ obj.thing1 = Thing1.new
32
+ obj.thing1.thing2 = :thing2
33
+ assert obj.valid?, 'valid with just a thing1'
34
+ end
35
+
36
+ def test_thing1_nil_is_ok
37
+ obj.thing2 = :thing2
38
+ assert obj.valid?, 'valid with just a thing2'
39
+ end
40
+
41
+ def test_valid
42
+ obj.thing1 = Thing1.new
43
+ obj.thing1.thing2 = :thing2
44
+ obj.thing2 = :thing2
45
+ assert obj.valid?, 'valid with matching related objects'
46
+ end
47
+
48
+ def test_invalid
49
+ obj.thing1 = Thing1.new
50
+ obj.thing1.thing2 = :thing2
51
+ obj.thing2 = :other_thing
52
+ assert !obj.valid?, 'not valid with non-matching related objects'
53
+ end
54
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "validates-belongs-to/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "validates-belongs-to"
7
+ s.version = Validates::Belongs::To::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Matt Burke']
10
+ s.email = ['spraints@gmail.com']
11
+ s.homepage = 'http://github.com/spraints/validates-belongs-to'
12
+ s.summary = %q{ActiveModel validation that two associated models are related to each other.}
13
+ s.description = %q{ActiveModel validation that two associated models are related to each other.}
14
+
15
+ s.rubyforge_project = "validates-belongs-to"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'activemodel', '>= 3.0.0'
23
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates-belongs-to
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Matt Burke
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-16 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 3
31
+ - 0
32
+ - 0
33
+ version: 3.0.0
34
+ version_requirements: *id001
35
+ name: activemodel
36
+ prerelease: false
37
+ description: ActiveModel validation that two associated models are related to each other.
38
+ email:
39
+ - spraints@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - README.md
50
+ - Rakefile
51
+ - lib/validates-belongs-to.rb
52
+ - lib/validates-belongs-to/version.rb
53
+ - test/validates-belongs-to_test.rb
54
+ - validates-belongs-to.gemspec
55
+ has_rdoc: true
56
+ homepage: http://github.com/spraints/validates-belongs-to
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project: validates-belongs-to
85
+ rubygems_version: 1.6.2
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: ActiveModel validation that two associated models are related to each other.
89
+ test_files:
90
+ - test/validates-belongs-to_test.rb