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 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
- This is a simple validator which validates an association belongs to the same owner.
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
- car = warehouse.cars.create # Works fine
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
- car.warehouse = warehouse_2
50
- car.save! # Validation failed: Warehouse does not belong to user
78
+ Car.create :warehouses => [warehouse] # Works fine
79
+ Car.create :warehouses => [warehouse_2] # Warehouses do not belong to user
51
80
  ```
@@ -1,4 +1,5 @@
1
1
  en:
2
2
  errors:
3
3
  messages:
4
- belongs_to: "does not belong to %{value}"
4
+ belongs_to: "does not belong to %{value}"
5
+ belongs_to_habtm: "do not belong to %{value}"
@@ -1,3 +1,3 @@
1
1
  module ValidatesBelongsTo
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -16,16 +16,33 @@ module ValidatesBelongsTo
16
16
  class BelongsToValidator < ActiveModel::EachValidator
17
17
 
18
18
  def validate_each(record, attribute, owner)
19
- record_value = get_id_by_association(record, options[:with])
20
- owner_value = get_id_by_association(owner, options[:with])
19
+ association = record.class.reflect_on_association(attribute)
20
+ association_type = association.macro
21
+ method = "validate_by_#{association_type}"
21
22
 
22
- unless record_value == owner_value || owner_value.nil?
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
 
@@ -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
@@ -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
- subject { Car.new({ :user => user }) }
13
+ context 'with belongs_to' do
14
14
 
15
- it { should allow_value(warehouse).for(:warehouse) }
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
- describe 'when association owner is set to nil' do
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
- it { should allow_value(Warehouse.create).for(:warehouse) }
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
- describe 'when record owner is set to nil' do
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
- subject { Car.new }
53
+ it { should_not allow_value([warehouse]).for(:warehouses) }
27
54
 
28
- it { should_not allow_value(warehouse).for(:warehouse) }
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.3
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-01-04 00:00:00.000000000 Z
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: 934803748451263386
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: 934803748451263386
121
+ hash: -3583830976604260693
122
122
  requirements: []
123
123
  rubyforge_project: validates_belongs_to
124
124
  rubygems_version: 1.8.24