deferred_associations 0.5.7 → 0.5.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG +4 -0
- data/Readme.markdown +3 -3
- data/VERSION +1 -1
- data/deferred_associations.gemspec +41 -44
- data/lib/array_to_association_wrapper.rb +68 -67
- data/lib/has_and_belongs_to_many_with_deferred_save.rb +34 -53
- data/lib/has_many_with_deferred_save.rb +107 -113
- data/spec/db/schema.rb +20 -22
- data/spec/habtm_ar4_spec.rb +85 -0
- data/spec/has_and_belongs_to_many_with_deferred_save_spec.rb +125 -128
- data/spec/has_many_with_deferred_save_spec.rb +103 -104
- data/spec/models/chair.rb +5 -3
- data/spec/models/door.rb +2 -0
- data/spec/models/person.rb +5 -3
- data/spec/models/room.rb +8 -9
- data/spec/models/table.rb +6 -4
- data/spec/spec_helper.rb +14 -12
- metadata +6 -5
- data/Rakefile +0 -27
data/spec/db/schema.rb
CHANGED
@@ -2,39 +2,37 @@
|
|
2
2
|
# migrations feature of ActiveRecord to incrementally modify your database, and
|
3
3
|
# then regenerate this schema definition.
|
4
4
|
|
5
|
-
ActiveRecord::Schema.define(:
|
6
|
-
|
7
|
-
|
8
|
-
t.column "name", :string
|
5
|
+
ActiveRecord::Schema.define(version: 1) do
|
6
|
+
create_table 'people', force: true do |t|
|
7
|
+
t.column 'name', :string
|
9
8
|
end
|
10
9
|
|
11
|
-
create_table
|
12
|
-
t.column
|
13
|
-
t.column
|
10
|
+
create_table 'people_rooms', id: false, force: true do |t|
|
11
|
+
t.column 'person_id', :integer
|
12
|
+
t.column 'room_id', :integer
|
14
13
|
end
|
15
14
|
|
16
|
-
create_table
|
17
|
-
t.column
|
18
|
-
t.column
|
15
|
+
create_table 'rooms', force: true do |t|
|
16
|
+
t.column 'name', :string
|
17
|
+
t.column 'maximum_occupancy', :integer
|
19
18
|
end
|
20
19
|
|
21
|
-
create_table
|
22
|
-
t.column
|
23
|
-
t.column
|
20
|
+
create_table 'doors_rooms', id: false, force: true do |t|
|
21
|
+
t.column 'door_id', :integer
|
22
|
+
t.column 'room_id', :integer
|
24
23
|
end
|
25
24
|
|
26
|
-
create_table
|
27
|
-
t.column
|
25
|
+
create_table 'doors', force: true do |t|
|
26
|
+
t.column 'name', :string
|
28
27
|
end
|
29
28
|
|
30
|
-
create_table
|
31
|
-
t.column
|
32
|
-
t.column
|
29
|
+
create_table 'tables', force: true do |t|
|
30
|
+
t.column 'name', :string
|
31
|
+
t.column 'room_id', :integer
|
33
32
|
end
|
34
33
|
|
35
|
-
create_table
|
36
|
-
t.column
|
37
|
-
t.column
|
34
|
+
create_table 'chairs', force: true do |t|
|
35
|
+
t.column 'name', :string
|
36
|
+
t.column 'table_id', :integer
|
38
37
|
end
|
39
|
-
|
40
38
|
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'has_and_belongs_to_many_with_deferred_save'
|
3
|
+
|
4
|
+
if ar4?
|
5
|
+
describe 'ActiveRecord4 specials' do
|
6
|
+
before :all do
|
7
|
+
#ActiveRecord::Base.logger = Logger.new(STDOUT) # uncomment for debugging statements
|
8
|
+
@people = []
|
9
|
+
@people << Person.create(name: 'Filbert')
|
10
|
+
@people << Person.create(name: 'Miguel')
|
11
|
+
@people << Person.create(name: 'Rainer')
|
12
|
+
@room1 = Room.create! maximum_occupancy: 2, people: @people[1..2]
|
13
|
+
@room2 = Room.create! maximum_occupancy: 2, people: @people[0..1]
|
14
|
+
@room3 = Room.create! maximum_occupancy: 2
|
15
|
+
end
|
16
|
+
|
17
|
+
after :all do
|
18
|
+
Person.delete_all
|
19
|
+
Room.delete_all
|
20
|
+
Person.connection.execute('DELETE FROM people_rooms')
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'queries' do #
|
24
|
+
it 'should not preload, if option is not specified' do
|
25
|
+
rooms = Room.where(id: [@room1.id, @room2.id, @room3.id])
|
26
|
+
rooms = rooms.to_a # execute original query
|
27
|
+
room1 = rooms.first
|
28
|
+
room2 = rooms.second
|
29
|
+
room3 = rooms.third
|
30
|
+
|
31
|
+
# association is not autoloaded, but will be loaded on access
|
32
|
+
expect(room1.people_without_deferred_save.loaded?).to be false
|
33
|
+
expect(room2.people_without_deferred_save.loaded?).to be false
|
34
|
+
expect(room3.people_without_deferred_save.loaded?).to be false
|
35
|
+
|
36
|
+
# accessing the deferred assotiation will load it anyway
|
37
|
+
expect(ActiveRecord::Base).to receive(:connection).at_least(:once).and_call_original
|
38
|
+
expect(room1.people.loaded?).to be true
|
39
|
+
|
40
|
+
expect(room1.people.map(&:name)).to match_array(%w(Miguel Rainer))
|
41
|
+
expect(room2.people.map(&:name)).to match_array(%w(Filbert Miguel))
|
42
|
+
expect(room3.people.map(&:name)).to match_array(%w())
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should preload, if option is specified' do
|
46
|
+
rooms = Room.where(id: [@room1.id, @room2.id, @room3.id]).preload(:people)
|
47
|
+
rooms = rooms.to_a # execute original query, together with preloading the association
|
48
|
+
room1 = rooms.first
|
49
|
+
room2 = rooms.second
|
50
|
+
room3 = rooms.third
|
51
|
+
|
52
|
+
# association is autoloaded
|
53
|
+
expect(room1.people_without_deferred_save.loaded?).to be true
|
54
|
+
expect(room2.people_without_deferred_save.loaded?).to be true
|
55
|
+
expect(room3.people_without_deferred_save.loaded?).to be true
|
56
|
+
|
57
|
+
expect(ActiveRecord::Base).not_to receive(:connection)
|
58
|
+
# deferred association is also already loaded
|
59
|
+
expect(room1.people.loaded?).to be true
|
60
|
+
|
61
|
+
expect(room1.people.map(&:name)).to match_array(%w(Miguel Rainer))
|
62
|
+
expect(room2.people.map(&:name)).to match_array(%w(Filbert Miguel))
|
63
|
+
expect(room3.people.map(&:name)).to match_array(%w())
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should preload with non-deferred association' do
|
67
|
+
rooms = Room.where(id: [@room1.id, @room2.id, @room3.id]).preload(:people2)
|
68
|
+
rooms = rooms.to_a # execute original query, together with preloading the association
|
69
|
+
room1 = rooms.first
|
70
|
+
room2 = rooms.second
|
71
|
+
room3 = rooms.third
|
72
|
+
# association is autoloaded
|
73
|
+
expect(room1.people2.loaded?).to be true
|
74
|
+
expect(room2.people2.loaded?).to be true
|
75
|
+
expect(room3.people2.loaded?).to be true
|
76
|
+
|
77
|
+
expect(ActiveRecord::Base).not_to receive(:connection)
|
78
|
+
|
79
|
+
expect(room1.people2.map(&:name)).to match_array(%w(Miguel Rainer))
|
80
|
+
expect(room2.people2.map(&:name)).to match_array(%w(Filbert Miguel))
|
81
|
+
expect(room3.people2.map(&:name)).to match_array(%w())
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -1,192 +1,193 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
require 'has_and_belongs_to_many_with_deferred_save'
|
3
3
|
|
4
|
-
describe
|
5
|
-
describe
|
4
|
+
describe 'has_and_belongs_to_many_with_deferred_save' do
|
5
|
+
describe 'room maximum_occupancy' do
|
6
6
|
before :all do
|
7
7
|
@people = []
|
8
|
-
@people << Person.create(:
|
9
|
-
@people << Person.create(:
|
10
|
-
@people << Person.create(:
|
11
|
-
@room = Room.new(:
|
8
|
+
@people << Person.create(name: 'Filbert')
|
9
|
+
@people << Person.create(name: 'Miguel')
|
10
|
+
@people << Person.create(name: 'Rainer')
|
11
|
+
@room = Room.new(maximum_occupancy: 2)
|
12
12
|
end
|
13
13
|
after :all do
|
14
14
|
Person.delete_all
|
15
15
|
Room.delete_all
|
16
16
|
end
|
17
17
|
|
18
|
-
it
|
19
|
-
Room .count.
|
20
|
-
Person.count.
|
18
|
+
it 'passes initial checks' do
|
19
|
+
expect(Room .count).to eq(0)
|
20
|
+
expect(Person.count).to eq(3)
|
21
21
|
|
22
|
-
@room.people.
|
23
|
-
@room.people_without_deferred_save.
|
24
|
-
@room.people_without_deferred_save.object_id.
|
22
|
+
expect(@room.people).to eq([])
|
23
|
+
expect(@room.people_without_deferred_save).to eq([])
|
24
|
+
expect(@room.people_without_deferred_save.object_id).not_to eq(
|
25
25
|
@room.unsaved_people.object_id
|
26
|
+
)
|
26
27
|
end
|
27
28
|
|
28
|
-
it
|
29
|
+
it 'after adding people to room, it should not have saved anything to the database' do
|
29
30
|
@room.people << @people[0]
|
30
31
|
@room.people << @people[1]
|
31
32
|
|
32
33
|
# Still not saved to the association table!
|
33
|
-
Room.count_by_sql(
|
34
|
-
@room.people_without_deferred_save.size.
|
34
|
+
expect(Room.count_by_sql('select count(*) from people_rooms')).to eq(0)
|
35
|
+
expect(@room.people_without_deferred_save.size). to eq(0)
|
35
36
|
end
|
36
37
|
|
37
|
-
it
|
38
|
-
@room.people.size.
|
38
|
+
it 'but room.people.size should still report the current size of 2' do
|
39
|
+
expect(@room.people.size).to eq(2) # 2 because this looks at unsaved_people and not at the database
|
39
40
|
end
|
40
41
|
|
41
|
-
it
|
42
|
-
@room.save
|
43
|
-
@room.errors.full_messages.
|
44
|
-
Room.count_by_sql(
|
45
|
-
@room.people.size.
|
46
|
-
@room.people_without_deferred_save.size.
|
42
|
+
it 'after saving the model, the association should be saved in the join table' do
|
43
|
+
@room.save # Only here is it actually saved to the association table!
|
44
|
+
expect(@room.errors.full_messages).to eq([])
|
45
|
+
expect(Room.count_by_sql('select count(*) from people_rooms')).to eq(2)
|
46
|
+
expect(@room.people.size). to eq(2)
|
47
|
+
expect(@room.people_without_deferred_save.size). to eq(2)
|
47
48
|
end
|
48
49
|
|
49
|
-
it
|
50
|
-
|
51
|
-
@room.people.size.
|
50
|
+
it 'when we try to add a 3rd person, it should add a validation error to the errors object like any other validation error' do
|
51
|
+
expect { @room.people << @people[2] }.not_to raise_error
|
52
|
+
expect(@room.people.size). to eq(3)
|
52
53
|
|
53
|
-
Room.count_by_sql(
|
54
|
+
expect(Room.count_by_sql('select count(*) from people_rooms')).to eq(2)
|
54
55
|
@room.valid?
|
55
|
-
@room.get_error(:people).
|
56
|
-
@room.people.size.
|
56
|
+
expect(@room.get_error(:people)).to eq('This room has reached its maximum occupancy')
|
57
|
+
expect(@room.people.size). to eq(3) # Just like with normal attributes that fail validation... the attribute still contains the invalid data but we refuse to save until it is changed to something that is *valid*.
|
57
58
|
end
|
58
59
|
|
59
|
-
it
|
60
|
-
@room.save.
|
61
|
-
Room.count_by_sql(
|
62
|
-
@room.get_error(:people).
|
63
|
-
@room.people.size.
|
64
|
-
@people.map {|p| p.reload; p.rooms.size}.
|
60
|
+
it 'when we try to save, it should fail, because room.people is still invalid' do
|
61
|
+
expect(@room.save).to eq(false)
|
62
|
+
expect(Room.count_by_sql('select count(*) from people_rooms')).to eq(2) # It's still not there, because it didn't pass the validation.
|
63
|
+
expect(@room.get_error(:people)).to eq('This room has reached its maximum occupancy')
|
64
|
+
expect(@room.people.size). to eq(3)
|
65
|
+
expect(@people.map { |p| p.reload; p.rooms.size }).to eq([1, 1, 0])
|
65
66
|
end
|
66
67
|
|
67
|
-
it
|
68
|
+
it 'when we reload, it should go back to only having 2 people in the room' do
|
68
69
|
@room.reload
|
69
|
-
@room.people.size.
|
70
|
-
@room.people_without_deferred_save.size.
|
71
|
-
@people.map {|p| p.reload; p.rooms.size}.
|
70
|
+
expect(@room.people.size). to eq(2)
|
71
|
+
expect(@room.people_without_deferred_save.size). to eq(2)
|
72
|
+
expect(@people.map { |p| p.reload; p.rooms.size }). to eq([1, 1, 0])
|
72
73
|
end
|
73
74
|
|
74
|
-
it
|
75
|
-
|
75
|
+
it 'if they try to go around our accessors and use the original accessors, then (and only then) will the exception be raised in before_adding_person...' do
|
76
|
+
expect do
|
76
77
|
@room.people_without_deferred_save << @people[2]
|
77
|
-
end.
|
78
|
+
end.to raise_error(RuntimeError)
|
78
79
|
end
|
79
80
|
|
80
|
-
it
|
81
|
+
it 'lets you bypass the validation on Room if we add the association from the other side (person.rooms <<)?' do
|
81
82
|
@people[2].rooms << @room
|
82
|
-
@people[2].rooms.size.
|
83
|
+
expect(@people[2].rooms.size).to eq(1)
|
83
84
|
|
84
85
|
# Adding it from one direction does not add it to the other object's association (@room.people), so the validation passes.
|
85
|
-
@room.reload.people.size.
|
86
|
+
expect(@room.reload.people.size).to eq(2)
|
86
87
|
@people[2].valid?
|
87
|
-
@people[2].errors.full_messages.
|
88
|
-
@people[2].save.
|
88
|
+
expect(@people[2].errors.full_messages).to eq([])
|
89
|
+
expect(@people[2].save).to eq(true)
|
89
90
|
|
90
91
|
# It is only after reloading that @room.people has this 3rd object, causing it to be invalid, and by then it's too late to do anything about it.
|
91
|
-
@room.reload.people.size.
|
92
|
-
@room.valid
|
92
|
+
expect(@room.reload.people.size).to eq(3)
|
93
|
+
expect(@room.valid?).to eq(false)
|
93
94
|
end
|
94
95
|
|
95
|
-
it
|
96
|
-
@room.reload.people.size.
|
96
|
+
it 'only if you add the validation to both sides, can you ensure that the size of the association does not exceed some limit' do
|
97
|
+
expect(@room.reload.people.size).to eq(3)
|
97
98
|
@room.people.delete(@people[2])
|
98
|
-
@room.save.
|
99
|
-
@room.reload.people.size.
|
100
|
-
@people[2].reload.rooms.size.
|
99
|
+
expect(@room.save).to eq(true)
|
100
|
+
expect(@room.reload.people.size).to eq(2)
|
101
|
+
expect(@people[2].reload.rooms.size).to eq(0)
|
101
102
|
|
102
103
|
obj = @people[2]
|
103
104
|
obj.do_extra_validation = true
|
104
105
|
|
105
106
|
@people[2].rooms << @room
|
106
|
-
@people[2].rooms.size.
|
107
|
+
expect(@people[2].rooms.size).to eq(1)
|
107
108
|
|
108
|
-
@room.reload.people.size.
|
109
|
-
@people[2].valid
|
110
|
-
@people[2].get_error(:rooms).
|
111
|
-
@room.reload.people.size.
|
109
|
+
expect(@room.reload.people.size).to eq(2)
|
110
|
+
expect(@people[2].valid?).to be false
|
111
|
+
expect(@people[2].get_error(:rooms)).to eq('This room has reached its maximum occupancy')
|
112
|
+
expect(@room.reload.people.size).to eq(2)
|
112
113
|
end
|
113
114
|
|
114
|
-
it
|
115
|
+
it 'still lets you do find' do
|
115
116
|
if ar4?
|
116
|
-
@room.people2.where(:
|
117
|
-
@room.people_without_deferred_save.where(:
|
118
|
-
@room.people.where(:
|
117
|
+
expect(@room.people2.where(name: 'Filbert').first).to eq(@people[0])
|
118
|
+
expect(@room.people_without_deferred_save.where(name: 'Filbert').first).to eq(@people[0])
|
119
|
+
expect(@room.people.where(name: 'Filbert').first).to eq(@people[0])
|
119
120
|
else
|
120
|
-
@room.people2. find(:first, :
|
121
|
-
@room.people_without_deferred_save.find(:first, :
|
122
|
-
@room.people2.first(:
|
123
|
-
@room.people_without_deferred_save.first(:
|
124
|
-
|
125
|
-
@room.people.find(:first, :
|
126
|
-
@room.people.first(:
|
127
|
-
@room.people.last(:
|
121
|
+
expect(@room.people2. find(:first, conditions: { name: 'Filbert' })).to eq(@people[0])
|
122
|
+
expect(@room.people_without_deferred_save.find(:first, conditions: { name: 'Filbert' })).to eq(@people[0])
|
123
|
+
expect(@room.people2.first(conditions: { name: 'Filbert' })).to eq(@people[0])
|
124
|
+
expect(@room.people_without_deferred_save.first(conditions: { name: 'Filbert' })).to eq(@people[0])
|
125
|
+
|
126
|
+
expect(@room.people.find(:first, conditions: { name: 'Filbert' })).to eq(@people[0])
|
127
|
+
expect(@room.people.first(conditions: { name: 'Filbert' })). to eq(@people[0])
|
128
|
+
expect(@room.people.last(conditions: { name: 'Filbert' })). to eq(@people[0])
|
128
129
|
end
|
129
130
|
|
130
|
-
@room.people.first.
|
131
|
-
@room.people.last.
|
132
|
-
@room.people.find_by_name('Filbert').
|
133
|
-
@room.people_without_deferred_save.find_by_name('Filbert').
|
131
|
+
expect(@room.people.first). to eq(@people[0])
|
132
|
+
expect(@room.people.last). to eq(@people[1]) # @people[2] was removed before
|
133
|
+
expect(@room.people.find_by_name('Filbert')). to eq(@people[0])
|
134
|
+
expect(@room.people_without_deferred_save.find_by_name('Filbert')).to eq(@people[0])
|
134
135
|
end
|
135
136
|
|
136
|
-
it
|
137
|
-
|
138
|
-
|
137
|
+
it 'should be dumpable with Marshal' do
|
138
|
+
expect { Marshal.dump(@room.people) }.not_to raise_exception
|
139
|
+
expect { Marshal.dump(Room.new.people) }.not_to raise_exception
|
139
140
|
end
|
140
141
|
|
141
|
-
it
|
142
|
+
it 'should detect difference in association' do
|
142
143
|
@room = Room.find(@room.id)
|
143
|
-
@room.bs_diff_before_module.
|
144
|
-
@room.bs_diff_after_module.
|
145
|
-
@room.bs_diff_method.
|
144
|
+
expect(@room.bs_diff_before_module).to be_nil
|
145
|
+
expect(@room.bs_diff_after_module).to be_nil
|
146
|
+
expect(@room.bs_diff_method).to be_nil
|
146
147
|
|
147
|
-
@room.people.size.
|
148
|
+
expect(@room.people.size).to eq(2)
|
148
149
|
@room.people = [@room.people[0]]
|
149
|
-
@room.save.
|
150
|
+
expect(@room.save).to be true
|
150
151
|
|
151
|
-
@room.bs_diff_before_module.
|
152
|
-
@room.bs_diff_after_module.
|
152
|
+
expect(@room.bs_diff_before_module).to be true
|
153
|
+
expect(@room.bs_diff_after_module).to be true
|
153
154
|
if ar2?
|
154
|
-
@room.bs_diff_method.
|
155
|
+
expect(@room.bs_diff_method).to be true
|
155
156
|
else
|
156
|
-
@room.bs_diff_method.
|
157
|
+
expect(@room.bs_diff_method).to be_nil # Rails 3.2: nil (before_save filter is not supported)
|
157
158
|
end
|
158
159
|
end
|
159
160
|
|
160
|
-
it
|
161
|
+
it 'should act like original habtm when using ID array with array manipulation' do
|
161
162
|
@room = Room.find(@room.id)
|
162
163
|
@room.people = [@people[0]]
|
163
164
|
@room.save
|
164
165
|
@room = Room.find(@room.id) # we don't want to let id and object setters interfere with each other
|
165
166
|
@room.people2_ids << @people[1].id
|
166
|
-
@room.people2_ids.
|
167
|
+
expect(@room.people2_ids).to eq([@people[0].id]) # ID array manipulation is ignored
|
167
168
|
|
168
|
-
@room.person_ids.size.
|
169
|
+
expect(@room.person_ids.size).to eq(1)
|
169
170
|
@room.person_ids << @people[1].id
|
170
|
-
@room.person_ids.
|
171
|
-
Room.find(@room.id).person_ids.
|
172
|
-
@room.save.
|
173
|
-
Room.find(@room.id).person_ids.
|
171
|
+
expect(@room.person_ids).to eq([@people[0].id])
|
172
|
+
expect(Room.find(@room.id).person_ids).to eq([@people[0].id])
|
173
|
+
expect(@room.save).to be true
|
174
|
+
expect(Room.find(@room.id).person_ids).to eq([@people[0].id]) # ID array manipulation is ignored, too
|
174
175
|
end
|
175
176
|
|
176
|
-
it
|
177
|
+
it 'should work with id setters' do
|
177
178
|
@room = Room.find(@room.id)
|
178
179
|
@room.people = [@people[0], @people[1]]
|
179
180
|
@room.save
|
180
181
|
@room = Room.find(@room.id)
|
181
|
-
@room.person_ids.
|
182
|
+
expect(@room.person_ids).to eq([@people[0].id, @people[1].id])
|
182
183
|
@room.person_ids = [@people[1].id]
|
183
|
-
@room.person_ids.
|
184
|
-
Room.find(@room.id).person_ids.
|
185
|
-
@room.save.
|
186
|
-
Room.find(@room.id).person_ids.
|
184
|
+
expect(@room.person_ids).to eq([@people[1].id])
|
185
|
+
expect(Room.find(@room.id).person_ids).to eq([@people[0].id, @people[1].id])
|
186
|
+
expect(@room.save).to be true
|
187
|
+
expect(Room.find(@room.id).person_ids).to eq([@people[1].id])
|
187
188
|
end
|
188
189
|
|
189
|
-
it
|
190
|
+
it 'should work with multiple id setters and object setters' do
|
190
191
|
@room = Room.find(@room.id)
|
191
192
|
@room.people = [@people[0]]
|
192
193
|
@room.person_ids = [@people[0].id, @people[1].id]
|
@@ -195,50 +196,46 @@ describe "has_and_belongs_to_many_with_deferred_save" do
|
|
195
196
|
@room.people = [@people[1]]
|
196
197
|
@room.save
|
197
198
|
@room = Room.find(@room.id)
|
198
|
-
@room.people.
|
199
|
+
expect(@room.people).to eq([@people[1]])
|
199
200
|
end
|
200
201
|
|
201
|
-
it
|
202
|
-
unless ar2?
|
203
|
-
@room.people.klass.should == Person
|
204
|
-
end
|
202
|
+
it 'should give klass in AR 3/4' do
|
203
|
+
expect(@room.people.klass).to eq(Person) unless ar2?
|
205
204
|
end
|
206
205
|
|
207
|
-
it
|
208
|
-
if ar2?
|
209
|
-
@room.people.aliased_table_name.should == "people"
|
210
|
-
end
|
206
|
+
it 'should give aliased_table_name in AR 2.3' do
|
207
|
+
expect(@room.people.aliased_table_name).to eq('people') if ar2?
|
211
208
|
end
|
212
209
|
|
213
|
-
it
|
210
|
+
it 'should support reload both with and without params' do
|
214
211
|
# find options are deprecated with AR 4, but reload still
|
215
212
|
# supports them
|
216
|
-
@room.reload.id.
|
217
|
-
with_param = @room.reload(:
|
218
|
-
with_param.id.
|
213
|
+
expect(@room.reload.id).to eq(@room.id)
|
214
|
+
with_param = @room.reload(select: 'id')
|
215
|
+
expect(with_param.id).to eq(@room.id)
|
219
216
|
end
|
220
217
|
end
|
221
218
|
|
222
|
-
describe
|
219
|
+
describe 'doors' do
|
223
220
|
before :all do
|
224
221
|
@rooms = []
|
225
|
-
@rooms << Room.create(:
|
226
|
-
@rooms << Room.create(:
|
227
|
-
@door = Door.new(:
|
222
|
+
@rooms << Room.create(name: 'Kitchen', maximum_occupancy: 1)
|
223
|
+
@rooms << Room.create(name: 'Dining room', maximum_occupancy: 10)
|
224
|
+
@door = Door.new(name: 'Kitchen-Dining-room door')
|
228
225
|
end
|
229
226
|
|
230
|
-
it
|
231
|
-
Room.count.
|
232
|
-
Door.count.
|
227
|
+
it 'passes initial checks' do
|
228
|
+
expect(Room.count).to eq(2)
|
229
|
+
expect(Door.count).to eq(0)
|
233
230
|
|
234
|
-
@door.rooms.
|
235
|
-
@door.rooms_without_deferred_save.
|
231
|
+
expect(@door.rooms).to eq([])
|
232
|
+
expect(@door.rooms_without_deferred_save).to eq([])
|
236
233
|
end
|
237
234
|
|
238
|
-
it
|
235
|
+
it 'the association has an include? method' do
|
239
236
|
@door.rooms << @rooms[0]
|
240
|
-
@door.rooms.include?(@rooms[0]).
|
241
|
-
@door.rooms.include?(@rooms[1]).
|
237
|
+
expect(@door.rooms.include?(@rooms[0])).to be true
|
238
|
+
expect(@door.rooms.include?(@rooms[1])).to be false
|
242
239
|
end
|
243
240
|
end
|
244
241
|
end
|