validates_belongs_to 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +34 -5
- data/lib/validates_belongs_to/locale/en.yml +2 -1
- data/lib/validates_belongs_to/version.rb +1 -1
- data/lib/validates_belongs_to.rb +22 -5
- data/spec/support/models.rb +7 -0
- data/spec/support/schema.rb +11 -0
- data/spec/validates_belong_to_spec.rb +35 -8
- metadata +4 -4
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
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
2
|
|
3
|
-
|
3
|
+
Validate your associations to ensure they belong to the same owner. Also works for `has_and_belongs_to_many` associations.
|
4
4
|
|
5
5
|
## Usage
|
6
6
|
|
@@ -19,7 +19,7 @@ class Car < ActiveRecord::Base
|
|
19
19
|
end
|
20
20
|
```
|
21
21
|
|
22
|
-
## Example code
|
22
|
+
## Example code: belongs_to
|
23
23
|
|
24
24
|
```ruby
|
25
25
|
class User < ActiveRecord::Base
|
@@ -44,8 +44,37 @@ warehouse = user.warehouses.create
|
|
44
44
|
user_2 = User.create
|
45
45
|
warehouse_2 = user_2.warehouses.create
|
46
46
|
|
47
|
-
|
47
|
+
Car.create :warehouse => warehouse # Works fine
|
48
|
+
Car.create :warehouse => warehouse_2 # Validation failed: Warehouse does not belong to user
|
49
|
+
```
|
50
|
+
|
51
|
+
## Example code: has_and_belongs_to_many
|
52
|
+
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
class User < ActiveRecord::Base
|
56
|
+
has_many :warehouses
|
57
|
+
has_many :cars
|
58
|
+
end
|
59
|
+
|
60
|
+
class Warehouse < ActiveRecord::Base
|
61
|
+
has_many :cars
|
62
|
+
belongs_to :user
|
63
|
+
has_and_belongs_to_many :cars
|
64
|
+
end
|
65
|
+
|
66
|
+
class Car < ActiveRecord::Base
|
67
|
+
belongs_to :user
|
68
|
+
has_and_belongs_to_many :warehouses
|
69
|
+
validates :warehouses, :belongs_to => :user
|
70
|
+
end
|
71
|
+
|
72
|
+
user = User.create
|
73
|
+
warehouse = user.warehouses.create
|
74
|
+
|
75
|
+
user_2 = User.create
|
76
|
+
warehouse_2 = user_2.warehouses.create
|
48
77
|
|
49
|
-
|
50
|
-
|
78
|
+
Car.create :warehouses => [warehouse] # Works fine
|
79
|
+
Car.create :warehouses => [warehouse_2] # Warehouses do not belong to user
|
51
80
|
```
|
data/lib/validates_belongs_to.rb
CHANGED
@@ -16,16 +16,33 @@ module ValidatesBelongsTo
|
|
16
16
|
class BelongsToValidator < ActiveModel::EachValidator
|
17
17
|
|
18
18
|
def validate_each(record, attribute, owner)
|
19
|
-
|
20
|
-
|
19
|
+
association = record.class.reflect_on_association(attribute)
|
20
|
+
association_type = association.macro
|
21
|
+
method = "validate_by_#{association_type}"
|
21
22
|
|
22
|
-
|
23
|
-
record.errors.add(attribute, :belongs_to, options.merge(:value => options[:with]))
|
24
|
-
end
|
23
|
+
send(method, record, attribute, owner) if respond_to?(method)
|
25
24
|
end
|
26
25
|
|
27
26
|
protected
|
28
27
|
|
28
|
+
def validate_by_belongs_to(record, attribute, owner)
|
29
|
+
record_value = get_id_by_association(record, options[:with])
|
30
|
+
owner_value = get_id_by_association(owner, options[:with])
|
31
|
+
|
32
|
+
unless record_value == owner_value || owner_value.nil?
|
33
|
+
record.errors.add(attribute, :belongs_to, options.merge(:value => options[:with]))
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def validate_by_has_and_belongs_to_many(record, attribute, owner)
|
38
|
+
record_value = get_id_by_association(record, options[:with])
|
39
|
+
owner_values = owner.map { |o| get_id_by_association(o, options[:with]) }.compact.uniq
|
40
|
+
|
41
|
+
unless owner_values == [record_value] || owner_values.empty?
|
42
|
+
record.errors.add(attribute, :belongs_to_habtm, options.merge(:value => options[:with]))
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
29
46
|
def get_id_by_association(record, association_name)
|
30
47
|
return nil if record.nil?
|
31
48
|
|
data/spec/support/models.rb
CHANGED
@@ -5,6 +5,7 @@ end
|
|
5
5
|
|
6
6
|
class Warehouse < ActiveRecord::Base
|
7
7
|
has_many :cars
|
8
|
+
has_and_belongs_to_many :shared_cars
|
8
9
|
belongs_to :user
|
9
10
|
end
|
10
11
|
|
@@ -12,4 +13,10 @@ class Car < ActiveRecord::Base
|
|
12
13
|
belongs_to :user
|
13
14
|
belongs_to :warehouse
|
14
15
|
validates :warehouse, :belongs_to => :user
|
16
|
+
end
|
17
|
+
|
18
|
+
class SharedCar < ActiveRecord::Base
|
19
|
+
belongs_to :user
|
20
|
+
has_and_belongs_to_many :warehouses
|
21
|
+
validates :warehouses, :belongs_to => :user
|
15
22
|
end
|
data/spec/support/schema.rb
CHANGED
@@ -15,4 +15,15 @@ ActiveRecord::Schema.define do
|
|
15
15
|
t.belongs_to :warehouse
|
16
16
|
t.timestamps
|
17
17
|
end
|
18
|
+
|
19
|
+
create_table :shared_cars, :force => true do |t|
|
20
|
+
t.belongs_to :user
|
21
|
+
t.timestamps
|
22
|
+
end
|
23
|
+
|
24
|
+
create_table :warehouses_shared_cars, :id => false, :force => true do |t|
|
25
|
+
t.belongs_to :shared_car
|
26
|
+
t.belongs_to :warehouse
|
27
|
+
t.timestamps
|
28
|
+
end
|
18
29
|
end
|
@@ -10,22 +10,49 @@ describe ValidatesBelongsTo do
|
|
10
10
|
let(:warehouse) { Warehouse.create({ :user => user }) }
|
11
11
|
let(:warehouse_2) { Warehouse.create({ :user => user_2 }) }
|
12
12
|
|
13
|
-
|
13
|
+
context 'with belongs_to' do
|
14
14
|
|
15
|
-
|
16
|
-
it { should_not allow_value(warehouse_2).for(:warehouse).with_message('does not belong to user') }
|
15
|
+
subject { Car.new({ :user => user }) }
|
17
16
|
|
18
|
-
|
17
|
+
it { should allow_value(warehouse).for(:warehouse) }
|
18
|
+
it { should_not allow_value(warehouse_2).for(:warehouse).with_message('does not belong to user') }
|
19
19
|
|
20
|
-
|
20
|
+
describe 'when association owner is set to nil' do
|
21
|
+
|
22
|
+
it { should allow_value(Warehouse.create).for(:warehouse) }
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'when record owner is set to nil' do
|
27
|
+
|
28
|
+
subject { Car.new }
|
29
|
+
|
30
|
+
it { should_not allow_value(warehouse).for(:warehouse) }
|
31
|
+
|
32
|
+
end
|
21
33
|
|
22
34
|
end
|
23
35
|
|
24
|
-
|
36
|
+
context 'with has_and_belongs_to_many' do
|
37
|
+
|
38
|
+
subject { SharedCar.new({ :user => user }) }
|
39
|
+
|
40
|
+
it { should allow_value([warehouse]).for(:warehouses) }
|
41
|
+
it { should_not allow_value([warehouse_2]).for(:warehouses).with_message('do not belong to user') }
|
42
|
+
|
43
|
+
describe 'when association owner is set to nil' do
|
44
|
+
|
45
|
+
it { should allow_value([Warehouse.create]).for(:warehouses) }
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
describe 'when record owner is set to nil' do
|
50
|
+
|
51
|
+
subject { SharedCar.new }
|
25
52
|
|
26
|
-
|
53
|
+
it { should_not allow_value([warehouse]).for(:warehouses) }
|
27
54
|
|
28
|
-
|
55
|
+
end
|
29
56
|
|
30
57
|
end
|
31
58
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_belongs_to
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-02-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -109,7 +109,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
109
109
|
version: '0'
|
110
110
|
segments:
|
111
111
|
- 0
|
112
|
-
hash:
|
112
|
+
hash: -3583830976604260693
|
113
113
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
114
|
none: false
|
115
115
|
requirements:
|
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
118
|
version: '0'
|
119
119
|
segments:
|
120
120
|
- 0
|
121
|
-
hash:
|
121
|
+
hash: -3583830976604260693
|
122
122
|
requirements: []
|
123
123
|
rubyforge_project: validates_belongs_to
|
124
124
|
rubygems_version: 1.8.24
|