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 +4 -0
- data/Gemfile +6 -0
- data/README.md +34 -0
- data/Rakefile +5 -0
- data/lib/validates_belongs_to/locale/en.yml +5 -0
- data/lib/validates_belongs_to/version.rb +3 -0
- data/lib/validates_belongs_to.rb +44 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/support/models.rb +15 -0
- data/spec/support/schema.rb +18 -0
- data/spec/validates_belong_to_spec.rb +32 -0
- data/spec/validates_belongs_to.sqlite3 +0 -0
- data/validates_belongs_to.gemspec +30 -0
- metadata +107 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# validates_belongs_to [![Build Status](https://secure.travis-ci.org/Arjeno/validates_belongs_to.png?branch=master)](http://travis-ci.org/Arjeno/validates_belongs_to)
|
2
|
+
|
3
|
+
This is a simple validator which validates an association belongs to the same owner.
|
4
|
+
|
5
|
+
## Example code
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
class User < ActiveRecord::Base
|
9
|
+
has_many :warehouses
|
10
|
+
has_many :cars
|
11
|
+
end
|
12
|
+
|
13
|
+
class Warehouse < ActiveRecord::Base
|
14
|
+
has_many :cars
|
15
|
+
belongs_to :user
|
16
|
+
end
|
17
|
+
|
18
|
+
class Car < ActiveRecord::Base
|
19
|
+
belongs_to :user
|
20
|
+
belongs_to :warehouse
|
21
|
+
validates :warehouse, :belongs_to => :user
|
22
|
+
end
|
23
|
+
|
24
|
+
user = User.create
|
25
|
+
warehouse = user.warehouses.create
|
26
|
+
|
27
|
+
user_2 = User.create
|
28
|
+
warehouse_2 = user_2.warehouses.create
|
29
|
+
|
30
|
+
car = warehouse.cars.create # Works fine
|
31
|
+
|
32
|
+
car.warehouse = warehouse_2
|
33
|
+
car.save! # Validation failed: Warehouse does not belong to user
|
34
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'validates_belongs_to/version'
|
3
|
+
|
4
|
+
module ValidatesBelongsTo
|
5
|
+
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def validates_belongs_to(assocation, owner)
|
11
|
+
validates assocation, :belongs_to => owner
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
class BelongsToValidator < ActiveModel::EachValidator
|
17
|
+
|
18
|
+
def validate_each(record, attribute, owner)
|
19
|
+
record_value = get_id_by_association(record)
|
20
|
+
owner_value = get_id_by_association(owner)
|
21
|
+
|
22
|
+
unless record_value == owner_value
|
23
|
+
record.errors.add(attribute, :belongs_to, options.merge(:value => options[:with]))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
|
29
|
+
def get_id_by_association(record)
|
30
|
+
association = record.class.reflect_on_association(options[:with])
|
31
|
+
|
32
|
+
if association.try(:macro) == :belongs_to
|
33
|
+
record.send(association.foreign_key)
|
34
|
+
else
|
35
|
+
association_resource = record.send(options[:with])
|
36
|
+
association_resource.try(association_resource.class.primary_key)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
ActiveRecord::Base.send(:include, ValidatesBelongsTo)
|
44
|
+
I18n.load_path << File.dirname(__FILE__) + '/validates_belongs_to/locale/en.yml'
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
require 'validates_belongs_to'
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
|
4
|
+
:database => File.dirname(__FILE__) + "/validates_belongs_to.sqlite3")
|
5
|
+
|
6
|
+
load File.dirname(__FILE__) + '/support/schema.rb'
|
7
|
+
load File.dirname(__FILE__) + '/support/models.rb'
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class User < ActiveRecord::Base
|
2
|
+
has_many :warehouses
|
3
|
+
has_many :cars
|
4
|
+
end
|
5
|
+
|
6
|
+
class Warehouse < ActiveRecord::Base
|
7
|
+
has_many :cars
|
8
|
+
belongs_to :user
|
9
|
+
end
|
10
|
+
|
11
|
+
class Car < ActiveRecord::Base
|
12
|
+
belongs_to :user
|
13
|
+
belongs_to :warehouse
|
14
|
+
validates :warehouse, :belongs_to => :user
|
15
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
ActiveRecord::Schema.define do
|
2
|
+
self.verbose = false
|
3
|
+
|
4
|
+
create_table :users, :force => true do |t|
|
5
|
+
t.timestamps
|
6
|
+
end
|
7
|
+
|
8
|
+
create_table :warehouses, :force => true do |t|
|
9
|
+
t.belongs_to :user
|
10
|
+
t.timestamps
|
11
|
+
end
|
12
|
+
|
13
|
+
create_table :cars, :force => true do |t|
|
14
|
+
t.belongs_to :user
|
15
|
+
t.belongs_to :warehouse
|
16
|
+
t.timestamps
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValidatesBelongsTo do
|
4
|
+
|
5
|
+
context "valid" do
|
6
|
+
|
7
|
+
let(:user) { User.create }
|
8
|
+
let(:warehouse) { Warehouse.create({ :user => user }) }
|
9
|
+
|
10
|
+
subject { Car.create({ :user => user, :warehouse => warehouse })}
|
11
|
+
|
12
|
+
it { should be_valid }
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
context "invalid" do
|
17
|
+
|
18
|
+
let(:user) { User.create }
|
19
|
+
let(:user_2) { User.create }
|
20
|
+
let(:warehouse) { Warehouse.create({ :user => user }) }
|
21
|
+
|
22
|
+
subject { Car.create({ :user => user_2, :warehouse => warehouse })}
|
23
|
+
|
24
|
+
it { should_not be_valid }
|
25
|
+
|
26
|
+
it "should contain validation error" do
|
27
|
+
subject.errors[:warehouse].should include 'does not belong to user'
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
Binary file
|
@@ -0,0 +1,30 @@
|
|
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 = ValidatesBelongsTo::VERSION
|
8
|
+
s.authors = ["Arjen Oosterkamp"]
|
9
|
+
s.email = ["mail@arjen.me"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Validates wether an association belongs to the same owner}
|
12
|
+
s.description = %q{Validates wether an association belongs to the same owner}
|
13
|
+
|
14
|
+
s.rubyforge_project = "validates_belongs_to"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# s.add_development_dependency "rspec"
|
22
|
+
# s.add_development_dependency "activemodel", ">= 3.0.0"
|
23
|
+
|
24
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
25
|
+
s.add_development_dependency "rspec", "~> 2.3"
|
26
|
+
|
27
|
+
s.add_development_dependency "sqlite3"
|
28
|
+
|
29
|
+
s.add_dependency "activerecord", "~> 3.0"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_belongs_to
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Arjen Oosterkamp
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-15 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: &70281984882240 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70281984882240
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70281984881560 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '2.3'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70281984881560
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: sqlite3
|
38
|
+
requirement: &70281984881100 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70281984881100
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: activerecord
|
49
|
+
requirement: &70281984879600 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70281984879600
|
58
|
+
description: Validates wether an association belongs to the same owner
|
59
|
+
email:
|
60
|
+
- mail@arjen.me
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- Gemfile
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/validates_belongs_to.rb
|
70
|
+
- lib/validates_belongs_to/locale/en.yml
|
71
|
+
- lib/validates_belongs_to/version.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
- spec/support/models.rb
|
74
|
+
- spec/support/schema.rb
|
75
|
+
- spec/validates_belong_to_spec.rb
|
76
|
+
- spec/validates_belongs_to.sqlite3
|
77
|
+
- validates_belongs_to.gemspec
|
78
|
+
homepage: ''
|
79
|
+
licenses: []
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project: validates_belongs_to
|
98
|
+
rubygems_version: 1.8.10
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: Validates wether an association belongs to the same owner
|
102
|
+
test_files:
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
- spec/support/models.rb
|
105
|
+
- spec/support/schema.rb
|
106
|
+
- spec/validates_belong_to_spec.rb
|
107
|
+
- spec/validates_belongs_to.sqlite3
|