sunstone 5.1.0.3 → 5.1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/active_record/associations/collection_association.rb +3 -2
- data/ext/active_record/attribute_methods.rb +15 -15
- data/ext/active_record/persistence.rb +2 -2
- data/lib/arel/visitors/sunstone.rb +15 -6
- data/lib/sunstone/version.rb +1 -1
- data/test/active_record/associations/belongs_to_test.rb +162 -0
- data/test/active_record/associations/has_and_belongs_to_many_test.rb +90 -0
- data/test/active_record/associations/has_many_test.rb +126 -6
- data/test/active_record/persistance_test.rb +0 -101
- data/test/active_record/query/limit_test.rb +19 -0
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d53af8c7411662e7621971b1c2a3f39f130ea8c3
|
4
|
+
data.tar.gz: 8a61637315c7faf35265b47b8334901340b41798
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 690936bd2ba33aab551c98da50ea1871e29fdfa9207dd71c7bcd78050bfa56f219ad859c068ac33346833ffb44feb058bc2f41a46ab7c2e4f70b11c0f1449a62
|
7
|
+
data.tar.gz: 6e9f804cc2b2e75584e184b9791dc3f5b2d0d682c71cd5ef1c60cb4ffdf1af23b6607bf3f650be218acb87f0173dad53b2621fffa3aa9dac40f72fc51efc4663
|
@@ -9,6 +9,7 @@ module ActiveRecord
|
|
9
9
|
if owner.new_record?
|
10
10
|
replace_records(other_array, original_target)
|
11
11
|
elsif owner.class.connection.is_a?(ActiveRecord::ConnectionAdapters::SunstoneAPIAdapter) && owner.instance_variable_defined?(:@updating) && owner.instance_variable_get(:@updating)
|
12
|
+
self.instance_variable_set(:@sunstone_changed, true)
|
12
13
|
replace_common_records_in_memory(other_array, original_target)
|
13
14
|
|
14
15
|
# Remove from target
|
@@ -71,7 +72,7 @@ module ActiveRecord
|
|
71
72
|
# will fail and false will be returned.
|
72
73
|
def update(attributes)
|
73
74
|
@updating = :updating
|
74
|
-
|
75
|
+
Thread.current[:sunstone_updating_model] = self
|
75
76
|
|
76
77
|
# The following transaction covers any possible database side-effects of the
|
77
78
|
# attributes assignment. For example, setting the IDs of a child collection.
|
@@ -81,7 +82,7 @@ module ActiveRecord
|
|
81
82
|
end
|
82
83
|
ensure
|
83
84
|
@updating = false
|
84
|
-
|
85
|
+
Thread.current[:sunstone_updating_model] = nil
|
85
86
|
end
|
86
87
|
|
87
88
|
end
|
@@ -16,7 +16,7 @@ module ActiveRecord
|
|
16
16
|
if self.class.connection.is_a?(ActiveRecord::ConnectionAdapters::SunstoneAPIAdapter)
|
17
17
|
self.class.reflect_on_all_associations.each do |reflection|
|
18
18
|
if reflection.belongs_to?
|
19
|
-
if association(reflection.name).loaded? && association(reflection.name).target ==
|
19
|
+
if association(reflection.name).loaded? && association(reflection.name).target == Thread.current[:sunstone_updating_model]
|
20
20
|
attrs.delete(arel_table[reflection.foreign_key])
|
21
21
|
else
|
22
22
|
add_attributes_for_belongs_to_association(reflection, attrs)
|
@@ -109,25 +109,25 @@ module ActiveRecord
|
|
109
109
|
if reflection.is_a?(ActiveRecord::Reflection::HasAndBelongsToManyReflection)
|
110
110
|
@_already_called[:"autosave_associated_records_for_#{self.class.name.downcase.pluralize}_#{reflection.name}"] = true
|
111
111
|
end
|
112
|
-
|
113
|
-
if association = association_instance_get(reflection.name)
|
114
|
-
autosave = reflection.options[:autosave]
|
115
112
|
|
116
|
-
|
113
|
+
if association = association_instance_get(reflection.name)
|
114
|
+
if new_record? || (association.instance_variable_defined?(:@sunstone_changed) && association.instance_variable_get(:@sunstone_changed)) || reflection.options[:autosave] || association.target.any?(&:changed_for_autosave?) || association.target.any?(&:new_record?)
|
115
|
+
attrs[Arel::Attributes::EmptyRelation.new(arel_table, reflection.name, true, true)] = [] if association.target.empty?
|
117
116
|
|
118
|
-
|
119
|
-
|
117
|
+
association.target.each_with_index do |record, idx|
|
118
|
+
next if record.destroyed?
|
120
119
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
120
|
+
if record.new_record?
|
121
|
+
record.send(:arel_attributes_with_values_for_create, record.send(:keys_for_partial_write) + [record.class.primary_key]).each do |k, v|
|
122
|
+
attrs[Arel::Attributes::Relation.new(k, reflection.name, idx, true)] = v
|
123
|
+
end
|
124
|
+
else
|
125
|
+
record.send(:arel_attributes_with_values_for_update, record.send(:keys_for_partial_write) + [record.class.primary_key]).each do |k, v|
|
126
|
+
attrs[Arel::Attributes::Relation.new(k, reflection.name, idx, true)] = v
|
127
|
+
end
|
128
128
|
end
|
129
129
|
end
|
130
|
-
|
130
|
+
association.instance_variable_set(:@sunstone_changed, false)
|
131
131
|
end
|
132
132
|
|
133
133
|
# reconstruct the scope now that we know the owner's id
|
@@ -16,7 +16,7 @@ module ActiveRecord
|
|
16
16
|
|
17
17
|
def create_or_update(*args)
|
18
18
|
@updating = new_record? ? :creating : :updating
|
19
|
-
|
19
|
+
Thread.current[:sunstone_updating_model] = self
|
20
20
|
|
21
21
|
raise ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
|
22
22
|
result = new_record? ? _create_record : _update_record(*args)
|
@@ -55,7 +55,7 @@ module ActiveRecord
|
|
55
55
|
raise ActiveRecord::RecordInvalid
|
56
56
|
ensure
|
57
57
|
@updating = false
|
58
|
-
|
58
|
+
Thread.current[:sunstone_updating_model] = nil
|
59
59
|
end
|
60
60
|
|
61
61
|
# Creates a record with values matching those of the instance attributes
|
@@ -34,7 +34,7 @@ module Arel
|
|
34
34
|
if !o.orders.empty?
|
35
35
|
collector.order = o.orders.map { |x| visit(x, collector) }
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
collector = maybe_visit o.limit, collector
|
39
39
|
collector = maybe_visit o.offset, collector
|
40
40
|
collector = maybe_visit o.eager_load, collector
|
@@ -635,11 +635,20 @@ module Arel
|
|
635
635
|
end
|
636
636
|
end
|
637
637
|
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
638
|
+
def visit_Arel_Nodes_Matches o, collector
|
639
|
+
key = visit(o.left, collector)
|
640
|
+
value = { like: visit(o.right, collector) }
|
641
|
+
|
642
|
+
if key.is_a?(Hash)
|
643
|
+
if o.left.is_a?(Arel::Attributes::Cast)
|
644
|
+
merge_to_bottom_hash(key, value)
|
645
|
+
else
|
646
|
+
add_to_bottom_of_hash(key, value)
|
647
|
+
end
|
648
|
+
else
|
649
|
+
{ key => value }
|
650
|
+
end
|
651
|
+
end
|
643
652
|
#
|
644
653
|
# def visit_Arel_Nodes_DoesNotMatch o, collector
|
645
654
|
# collector = visit o.left, collector
|
data/lib/sunstone/version.rb
CHANGED
@@ -0,0 +1,162 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ActiveRecord::Associations::BelongsToTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
schema do
|
6
|
+
create_table "ships" do |t|
|
7
|
+
t.string "name", limit: 255
|
8
|
+
t.integer "fleet_id"
|
9
|
+
end
|
10
|
+
|
11
|
+
create_table "fleets" do |t|
|
12
|
+
t.string "name", limit: 255
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Fleet < ActiveRecord::Base
|
17
|
+
has_many :ships
|
18
|
+
end
|
19
|
+
|
20
|
+
class Ship < ActiveRecord::Base
|
21
|
+
belongs_to :fleet
|
22
|
+
end
|
23
|
+
|
24
|
+
# Save includes =============================================================
|
25
|
+
|
26
|
+
test '#save new record includes new belongs_to associations' do
|
27
|
+
ship = Ship.new(name: 'Definant', fleet: Fleet.new(name: 'Armada Duo'))
|
28
|
+
|
29
|
+
req_stub = webmock(:post, '/ships', {include: :fleet}).with(
|
30
|
+
body: {
|
31
|
+
ship: { name: 'Definant', fleet_attributes: { name: 'Armada Duo' } }
|
32
|
+
}.to_json
|
33
|
+
).to_return(
|
34
|
+
body: {
|
35
|
+
id: 2,
|
36
|
+
fleet_id: 3,
|
37
|
+
name: 'Definant 001',
|
38
|
+
fleet: { id: 3, name: 'Armada 2' }
|
39
|
+
}.to_json
|
40
|
+
)
|
41
|
+
|
42
|
+
assert ship.save
|
43
|
+
assert_equal 2, ship.id
|
44
|
+
assert_equal 3, ship.fleet_id
|
45
|
+
assert_equal 3, ship.fleet.id
|
46
|
+
assert_equal 'Definant 001', ship.name
|
47
|
+
assert_equal 'Armada 2', ship.fleet.name
|
48
|
+
|
49
|
+
assert_requested req_stub
|
50
|
+
end
|
51
|
+
|
52
|
+
test '#save new record doesnt include persisted/unmodified belongs_to associations' do
|
53
|
+
webmock(:get, "/fleets", where: {id: 1}, limit: 1).to_return(
|
54
|
+
body: [{id: 1, name: 'Armada Original'}].to_json
|
55
|
+
)
|
56
|
+
|
57
|
+
fleet = Fleet.find(1)
|
58
|
+
ship = Ship.new(name: 'Definant', fleet: fleet)
|
59
|
+
|
60
|
+
req_stub = webmock(:post, '/ships').with(
|
61
|
+
body: {
|
62
|
+
ship: { name: 'Definant', fleet_id: 1 }
|
63
|
+
}.to_json
|
64
|
+
).to_return(
|
65
|
+
body: {
|
66
|
+
id: 2,
|
67
|
+
fleet_id: 1,
|
68
|
+
name: 'Definant 001'
|
69
|
+
}.to_json
|
70
|
+
)
|
71
|
+
|
72
|
+
assert ship.save
|
73
|
+
assert_equal 2, ship.id
|
74
|
+
assert_equal 1, ship.fleet_id
|
75
|
+
|
76
|
+
assert_requested req_stub
|
77
|
+
end
|
78
|
+
|
79
|
+
test '#save persisted record includes new belongs_to associations' do
|
80
|
+
webmock(:get, "/ships", where: {id: 1}, limit: 1).to_return(
|
81
|
+
body: [{id: 1, fleet_id: nil, name: 'Ship Uno'}].to_json
|
82
|
+
)
|
83
|
+
|
84
|
+
req_stub = webmock(:patch, '/ships/1', {include: :fleet}).with(
|
85
|
+
body: {
|
86
|
+
ship: { fleet_attributes: { name: 'Armada Duo' } }
|
87
|
+
}.to_json
|
88
|
+
).to_return(
|
89
|
+
body: {
|
90
|
+
id: 1, fleet_id: 2, name: 'Ship Uno',
|
91
|
+
fleet: { id: 2, name: 'Armada Duo' }
|
92
|
+
}.to_json
|
93
|
+
)
|
94
|
+
|
95
|
+
ship = Ship.find(1)
|
96
|
+
ship.fleet = Fleet.new(name: 'Armada Duo')
|
97
|
+
|
98
|
+
ship.save
|
99
|
+
|
100
|
+
assert_requested req_stub
|
101
|
+
end
|
102
|
+
|
103
|
+
test '#save persisted record doesnt include persisted/unmodified belongs_to associations but updates belongs_to key' do
|
104
|
+
webmock(:get, "/ships", where: {id: 1}, limit: 1).to_return(
|
105
|
+
body: [{id: 1, fleet_id: nil, name: 'Ship Uno'}].to_json
|
106
|
+
)
|
107
|
+
webmock(:get, "/fleets", where: {id: 1}, limit: 1).to_return(
|
108
|
+
body: [{id: 1, name: 'Armada Original'}].to_json
|
109
|
+
)
|
110
|
+
|
111
|
+
fleet = Fleet.find(1)
|
112
|
+
ship = Ship.find(1)
|
113
|
+
|
114
|
+
req_stub = webmock(:patch, '/ships/1').with(
|
115
|
+
body: {
|
116
|
+
ship: { fleet_id: 1 }
|
117
|
+
}.to_json
|
118
|
+
).to_return(
|
119
|
+
body: {
|
120
|
+
id: 1,
|
121
|
+
fleet_id: 1,
|
122
|
+
name: 'My Ship'
|
123
|
+
}.to_json
|
124
|
+
)
|
125
|
+
|
126
|
+
ship.fleet = fleet
|
127
|
+
assert ship.save
|
128
|
+
assert_equal 1, ship.id
|
129
|
+
assert_equal 1, ship.fleet_id
|
130
|
+
assert_equal 'My Ship', ship.name
|
131
|
+
|
132
|
+
assert_requested req_stub
|
133
|
+
end
|
134
|
+
|
135
|
+
test '#save persisted record doesnt include loaded belongs_to association' do
|
136
|
+
webmock(:get, "/ships", where: {id: 1}, limit: 1, include: [:fleet]).to_return(
|
137
|
+
body: [{id: 1, fleet_id: 1, name: 'Ship Uno', fleet: {id: 1, name: 'Armada Original'}}].to_json
|
138
|
+
)
|
139
|
+
|
140
|
+
ship = Ship.eager_load(:fleet).find(1)
|
141
|
+
|
142
|
+
req_stub = webmock(:patch, '/ships/1').with(
|
143
|
+
body: {
|
144
|
+
ship: { name: 'New NAME!!' }
|
145
|
+
}.to_json
|
146
|
+
).to_return(
|
147
|
+
body: {
|
148
|
+
id: 1,
|
149
|
+
fleet_id: 1,
|
150
|
+
name: 'New NAME!!'
|
151
|
+
}.to_json
|
152
|
+
)
|
153
|
+
|
154
|
+
|
155
|
+
assert ship.association(:fleet).loaded?
|
156
|
+
ship.name = 'New NAME!!'
|
157
|
+
assert ship.save
|
158
|
+
|
159
|
+
assert_requested req_stub
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
@@ -32,4 +32,94 @@ class ActiveRecord::Associations::HasAndBelongsToManyTest < ActiveSupport::TestC
|
|
32
32
|
assert_equal [43], Ship.find(42).sailor_ids
|
33
33
|
end
|
34
34
|
|
35
|
+
test '#update habtm relationships' do
|
36
|
+
webmock(:get, "/ships", where: {id: 1}, limit: 1).to_return(
|
37
|
+
body: [{id: 1, fleet_id: nil, name: 'Armada Uno'}].to_json
|
38
|
+
)
|
39
|
+
webmock(:get, "/sailors", where: {id: 1}, limit: 1).to_return(
|
40
|
+
body: [{id: 1, name: 'Captain'}].to_json
|
41
|
+
)
|
42
|
+
webmock(:get, "/sailors", where: {sailors_ships: {ship_id: {eq: 1}}}).to_return(
|
43
|
+
body: [].to_json
|
44
|
+
)
|
45
|
+
req_stub = webmock(:patch, '/ships/1').with(
|
46
|
+
body: {ship: {sailors_attributes: [{id: 1}]}}.to_json
|
47
|
+
).to_return(
|
48
|
+
body: {id: 1, name: 'Armada Uno'}.to_json
|
49
|
+
)
|
50
|
+
|
51
|
+
ship = Ship.find(1)
|
52
|
+
assert ship.update(sailors: [Sailor.find(1)])
|
53
|
+
assert_requested req_stub
|
54
|
+
end
|
55
|
+
|
56
|
+
test '#update clears habtm relationship' do
|
57
|
+
webmock(:get, "/ships", where: {id: 1}, limit: 1).to_return(
|
58
|
+
body: [{id: 1, fleet_id: nil, name: 'Armada Uno'}].to_json
|
59
|
+
)
|
60
|
+
webmock(:get, "/sailors", where: {id: 1}, limit: 1).to_return(
|
61
|
+
body: [{id: 1, name: 'Captain'}].to_json
|
62
|
+
)
|
63
|
+
webmock(:get, "/sailors", where: {sailors_ships: {ship_id: {eq: 1}}}).to_return(
|
64
|
+
body: [{id: 1, name: 'Captain'}].to_json
|
65
|
+
)
|
66
|
+
req_stub = webmock(:patch, '/ships/1').with(
|
67
|
+
body: {ship: {sailors_attributes: []}}.to_json
|
68
|
+
).to_return(
|
69
|
+
body: {id: 1, name: 'Armada Uno'}.to_json
|
70
|
+
)
|
71
|
+
|
72
|
+
ship = Ship.find(1)
|
73
|
+
assert ship.update(sailors: [])
|
74
|
+
assert_requested req_stub
|
75
|
+
end
|
76
|
+
|
77
|
+
test '#save persisted record doesnt include loaded habtm association' do
|
78
|
+
webmock(:get, "/ships", where: {id: 1}, limit: 1).to_return(
|
79
|
+
body: [{id: 1, name: 'Armada Uno'}].to_json
|
80
|
+
)
|
81
|
+
webmock(:get, "/sailors", where: {id: 1}, limit: 1).to_return(
|
82
|
+
body: [{id: 1, name: 'Captain'}].to_json
|
83
|
+
)
|
84
|
+
webmock(:get, "/sailors", where: {sailors_ships: {ship_id: {eq: 1}}}).to_return(
|
85
|
+
body: [{id: 1, name: 'Captain'}].to_json
|
86
|
+
)
|
87
|
+
|
88
|
+
ship = Ship.find(1)
|
89
|
+
|
90
|
+
|
91
|
+
req_stub = webmock(:patch, '/ships/1').with(
|
92
|
+
body: {
|
93
|
+
ship: { name: 'New NAME!!' }
|
94
|
+
}.to_json
|
95
|
+
).to_return(
|
96
|
+
body: {
|
97
|
+
id: 1,
|
98
|
+
name: 'New NAME!!'
|
99
|
+
}.to_json
|
100
|
+
)
|
101
|
+
|
102
|
+
ship.sailors.load
|
103
|
+
assert ship.sailors.loaded?
|
104
|
+
ship.name = 'New NAME!!'
|
105
|
+
assert ship.save
|
106
|
+
|
107
|
+
assert_requested req_stub
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
test "#destroy with habtm relationship" do
|
112
|
+
webmock(:get, "/ships", where: {id: 1}, limit: 1).to_return(
|
113
|
+
body: [{id: 1, fleet_id: nil, name: 'Armada Uno'}].to_json
|
114
|
+
)
|
115
|
+
req_stub = webmock(:delete, '/ships/1').to_return(
|
116
|
+
status: 204
|
117
|
+
)
|
118
|
+
|
119
|
+
ship = Ship.find(1)
|
120
|
+
assert ship.destroy
|
121
|
+
assert_requested req_stub
|
122
|
+
end
|
123
|
+
|
124
|
+
|
35
125
|
end
|
@@ -21,13 +21,15 @@ class ActiveRecord::Associations::HasManyTest < ActiveSupport::TestCase
|
|
21
21
|
belongs_to :fleet
|
22
22
|
end
|
23
23
|
|
24
|
+
# ID Setters ================================================================
|
25
|
+
|
24
26
|
test '#create with has_many_ids=' do
|
25
27
|
webmock(:get, "/ships", where: {id: 2}).to_return(body: [{id: 2, fleet_id: nil, name: 'Duo'}].to_json)
|
26
28
|
webmock(:post, "/fleets").with(
|
27
29
|
body: {
|
28
30
|
fleet: {
|
29
31
|
name: 'Spanish Armada',
|
30
|
-
ships_attributes: [{id: 2
|
32
|
+
ships_attributes: [{id: 2}]
|
31
33
|
}
|
32
34
|
}
|
33
35
|
).to_return(body: {id: 42, name: "Spanish Armada"}.to_json)
|
@@ -44,14 +46,52 @@ class ActiveRecord::Associations::HasManyTest < ActiveSupport::TestCase
|
|
44
46
|
webmock(:patch, "/fleets/42").with(
|
45
47
|
body: {
|
46
48
|
fleet: {
|
47
|
-
ships_attributes: [{id: 2
|
49
|
+
ships_attributes: [{id: 2}]
|
48
50
|
}
|
49
51
|
}).to_return(body: {id: 42, name: "Spanish Armada"}.to_json)
|
50
52
|
|
51
53
|
Fleet.find(42).update(ship_ids: ["2"])
|
52
54
|
end
|
55
|
+
|
56
|
+
# Modifing relationship ====================================================
|
53
57
|
|
54
|
-
test '#save includes
|
58
|
+
test '#save persisted records includes has_many associations when new record added' do
|
59
|
+
webmock(:get, '/fleets', where: {id: 1}, limit: 1, include: [:ships]).to_return({
|
60
|
+
body: [{
|
61
|
+
id: 1,
|
62
|
+
name: 'Armada Trio',
|
63
|
+
ships: [
|
64
|
+
{id: 2, fleet_id: 1, name: 'Definant'}
|
65
|
+
]
|
66
|
+
}].to_json
|
67
|
+
})
|
68
|
+
|
69
|
+
webmock(:get, '/ships', where: {id: 3}, limit: 1).to_return({
|
70
|
+
body: [{id: 3, fleet_id: nil, name: 'Enterprise'}].to_json
|
71
|
+
})
|
72
|
+
|
73
|
+
req_stub = webmock(:patch, '/fleets/1').with(
|
74
|
+
body: {
|
75
|
+
fleet: {
|
76
|
+
ships_attributes: [{ id: 2 }, {id: 3 }]
|
77
|
+
}
|
78
|
+
}.to_json
|
79
|
+
).to_return(
|
80
|
+
body: {
|
81
|
+
id: 1,
|
82
|
+
name: 'Armada Trio',
|
83
|
+
ships: [{ id: 2, fleet_id: 1, name: 'Voyager' }]
|
84
|
+
}.to_json
|
85
|
+
)
|
86
|
+
|
87
|
+
# fleet.ships = [ship]
|
88
|
+
fleet = Fleet.eager_load(:ships).find(1)
|
89
|
+
fleet.update(ships: fleet.ships + [Ship.find(3)])
|
90
|
+
|
91
|
+
assert_requested req_stub
|
92
|
+
end
|
93
|
+
|
94
|
+
test '#save persisted records includes has_many associations when replaced with new record' do
|
55
95
|
webmock(:get, '/fleets', where: {id: 1}, limit: 1, include: [:ships]).to_return({
|
56
96
|
body: [{
|
57
97
|
id: 1,
|
@@ -81,13 +121,93 @@ class ActiveRecord::Associations::HasManyTest < ActiveSupport::TestCase
|
|
81
121
|
assert fleet.update(ships: [Ship.new(name: 'Voyager')])
|
82
122
|
assert_equal 1, fleet.id
|
83
123
|
assert_equal [3], fleet.ships.map(&:id)
|
84
|
-
# assert_equal 3, ship.fleet.id
|
85
|
-
# assert_equal 'Definant 001', ship.name
|
86
|
-
# assert_equal 'Armada 2', ship.fleet.name
|
87
124
|
|
88
125
|
assert_requested req_stub
|
89
126
|
end
|
90
127
|
|
128
|
+
test '#save persisted records includes has_many associations when updating record in relationship' do
|
129
|
+
webmock(:get, '/fleets', where: {id: 1}, limit: 1, include: [:ships]).to_return({
|
130
|
+
body: [{
|
131
|
+
id: 1,
|
132
|
+
name: 'Armada Trio',
|
133
|
+
ships: [
|
134
|
+
{id: 2, fleet_id: 1, name: 'Definant'}, {id: 3, fleet_id: 1, name: 'Enterprise'}
|
135
|
+
]
|
136
|
+
}].to_json
|
137
|
+
})
|
138
|
+
|
139
|
+
req_stub = webmock(:patch, '/fleets/1').with(
|
140
|
+
body: {
|
141
|
+
fleet: {
|
142
|
+
ships_attributes: [{ name: 'Voyager', id: 2 }, {id: 3}]
|
143
|
+
}
|
144
|
+
}.to_json
|
145
|
+
).to_return(
|
146
|
+
body: {
|
147
|
+
id: 1,
|
148
|
+
name: 'Armada Trio',
|
149
|
+
ships: [{ id: 2, fleet_id: 1, name: 'Voyager' }, {id: 3, fleet_id: 1, name: 'Enterprise'}]
|
150
|
+
}.to_json
|
151
|
+
)
|
152
|
+
|
153
|
+
# fleet.ships = [ship]
|
154
|
+
fleet = Fleet.eager_load(:ships).find(1)
|
155
|
+
fleet.ships.first.name = 'Voyager'
|
156
|
+
fleet.save
|
157
|
+
|
158
|
+
assert_requested req_stub
|
159
|
+
end
|
160
|
+
|
161
|
+
test '#save persisted records doesnt include any loaded has_many associations' do
|
162
|
+
webmock(:get, '/fleets', where: {id: 1}, limit: 1, include: [:ships]).to_return({
|
163
|
+
body: [{
|
164
|
+
id: 1,
|
165
|
+
name: 'Armada Trio',
|
166
|
+
ships: [
|
167
|
+
{id: 2, fleet_id: 1, name: 'Definant'}, {id: 3, fleet_id: 1, name: 'Enterprise'}
|
168
|
+
]
|
169
|
+
}].to_json
|
170
|
+
})
|
171
|
+
|
172
|
+
req_stub = webmock(:patch, '/fleets/1').with(
|
173
|
+
body: { fleet: { name: 'New NAME!!' } }.to_json
|
174
|
+
).to_return(
|
175
|
+
body: {
|
176
|
+
id: 1,
|
177
|
+
name: 'New NAME!!'
|
178
|
+
}.to_json
|
179
|
+
)
|
180
|
+
|
181
|
+
# fleet.ships = [ship]
|
182
|
+
fleet = Fleet.eager_load(:ships).find(1)
|
183
|
+
assert fleet.ships.loaded?
|
184
|
+
fleet.name = 'New NAME!!'
|
185
|
+
fleet.save
|
186
|
+
|
187
|
+
assert_requested req_stub
|
188
|
+
end
|
189
|
+
|
190
|
+
# Clearing the relationship =================================================
|
191
|
+
|
192
|
+
test '#update clears has_many relationship' do
|
193
|
+
webmock(:get, "/fleets", where: {id: 1}, limit: 1).to_return(
|
194
|
+
body: [{id: 1, name: 'Armada Uno'}].to_json
|
195
|
+
)
|
196
|
+
webmock(:get, "/ships", where: {fleet_id: 1}).to_return(
|
197
|
+
body: [{id: 1, name: 'Saucer Trio'}].to_json
|
198
|
+
)
|
199
|
+
req_stub = webmock(:patch, '/fleets/1').with(
|
200
|
+
body: {fleet: {ships_attributes: []}}.to_json
|
201
|
+
).to_return(
|
202
|
+
body: {id: 1, name: 'Armada Uno'}.to_json
|
203
|
+
)
|
204
|
+
|
205
|
+
fleet = Fleet.find(1)
|
206
|
+
assert fleet.update(ships: [])
|
207
|
+
assert_requested req_stub
|
208
|
+
end
|
209
|
+
|
210
|
+
|
91
211
|
# test 'relation#delete_all' do
|
92
212
|
# webmock(:get, "/fleets", where: {id: 42}, limit: 1).to_return(body: [{id: 42, name: "Spanish Armada"}].to_json)
|
93
213
|
# Fleet.find(42).ships.delete_all
|
@@ -84,7 +84,6 @@ class ActiveRecord::PersistanceTest < ActiveSupport::TestCase
|
|
84
84
|
assert_equal 'Armada Duo', fleet.name
|
85
85
|
end
|
86
86
|
|
87
|
-
|
88
87
|
test '#save attempts another request while in transaction' do
|
89
88
|
webmock(:get, '/test_model_bs/schema').to_return(
|
90
89
|
body: {
|
@@ -110,33 +109,6 @@ class ActiveRecord::PersistanceTest < ActiveSupport::TestCase
|
|
110
109
|
end
|
111
110
|
end
|
112
111
|
|
113
|
-
test '#save includes modified belongs_to associations' do
|
114
|
-
ship = Ship.new(name: 'Definant', fleet: Fleet.new(name: 'Armada Duo'))
|
115
|
-
|
116
|
-
req_stub = webmock(:post, '/ships', {include: :fleet}).with(
|
117
|
-
body: {
|
118
|
-
ship: {
|
119
|
-
name: 'Definant', fleet_attributes: { name: 'Armada Duo' }
|
120
|
-
}
|
121
|
-
}.to_json
|
122
|
-
).to_return(
|
123
|
-
body: {
|
124
|
-
id: 2,
|
125
|
-
fleet_id: 3,
|
126
|
-
name: 'Definant 001',
|
127
|
-
fleet: { id: 3, name: 'Armada 2' }
|
128
|
-
}.to_json
|
129
|
-
)
|
130
|
-
|
131
|
-
assert ship.save
|
132
|
-
assert_equal 2, ship.id
|
133
|
-
assert_equal 3, ship.fleet_id
|
134
|
-
assert_equal 3, ship.fleet.id
|
135
|
-
assert_equal 'Definant 001', ship.name
|
136
|
-
assert_equal 'Armada 2', ship.fleet.name
|
137
|
-
|
138
|
-
assert_requested req_stub
|
139
|
-
end
|
140
112
|
|
141
113
|
|
142
114
|
test '#update clears belongs_to relationship' do
|
@@ -184,77 +156,4 @@ class ActiveRecord::PersistanceTest < ActiveSupport::TestCase
|
|
184
156
|
assert_requested req_stub
|
185
157
|
end
|
186
158
|
|
187
|
-
test '#update habtm relationships' do
|
188
|
-
webmock(:get, "/ships", where: {id: 1}, limit: 1).to_return(
|
189
|
-
body: [{id: 1, fleet_id: nil, name: 'Armada Uno'}].to_json
|
190
|
-
)
|
191
|
-
webmock(:get, "/sailors", where: {id: 1}, limit: 1).to_return(
|
192
|
-
body: [{id: 1, name: 'Captain'}].to_json
|
193
|
-
)
|
194
|
-
webmock(:get, "/sailors", where: {sailors_ships: {ship_id: {eq: 1}}}).to_return(
|
195
|
-
body: [].to_json
|
196
|
-
)
|
197
|
-
req_stub = webmock(:patch, '/ships/1').with(
|
198
|
-
body: {ship: {sailors_attributes: [{id: 1, name: "Captain"}]}}.to_json
|
199
|
-
).to_return(
|
200
|
-
body: {id: 1, name: 'Armada Uno'}.to_json
|
201
|
-
)
|
202
|
-
|
203
|
-
ship = Ship.find(1)
|
204
|
-
assert ship.update(sailors: [Sailor.find(1)])
|
205
|
-
assert_requested req_stub
|
206
|
-
end
|
207
|
-
|
208
|
-
test '#update clears habtm relationship' do
|
209
|
-
webmock(:get, "/ships", where: {id: 1}, limit: 1).to_return(
|
210
|
-
body: [{id: 1, fleet_id: nil, name: 'Armada Uno'}].to_json
|
211
|
-
)
|
212
|
-
webmock(:get, "/sailors", where: {id: 1}, limit: 1).to_return(
|
213
|
-
body: [{id: 1, name: 'Captain'}].to_json
|
214
|
-
)
|
215
|
-
webmock(:get, "/sailors", where: {sailors_ships: {ship_id: {eq: 1}}}).to_return(
|
216
|
-
body: [{id: 1, name: 'Captain'}].to_json
|
217
|
-
)
|
218
|
-
req_stub = webmock(:patch, '/ships/1').with(
|
219
|
-
body: {ship: {sailors_attributes: []}}.to_json
|
220
|
-
).to_return(
|
221
|
-
body: {id: 1, name: 'Armada Uno'}.to_json
|
222
|
-
)
|
223
|
-
|
224
|
-
ship = Ship.find(1)
|
225
|
-
assert ship.update(sailors: [])
|
226
|
-
assert_requested req_stub
|
227
|
-
end
|
228
|
-
|
229
|
-
test '#update clears has_many relationship' do
|
230
|
-
webmock(:get, "/fleets", where: {id: 1}, limit: 1).to_return(
|
231
|
-
body: [{id: 1, name: 'Armada Uno'}].to_json
|
232
|
-
)
|
233
|
-
webmock(:get, "/ships", where: {fleet_id: 1}).to_return(
|
234
|
-
body: [{id: 1, name: 'Saucer Trio'}].to_json
|
235
|
-
)
|
236
|
-
req_stub = webmock(:patch, '/fleets/1').with(
|
237
|
-
body: {fleet: {ships_attributes: []}}.to_json
|
238
|
-
).to_return(
|
239
|
-
body: {id: 1, name: 'Armada Uno'}.to_json
|
240
|
-
)
|
241
|
-
|
242
|
-
fleet = Fleet.find(1)
|
243
|
-
assert fleet.update(ships: [])
|
244
|
-
assert_requested req_stub
|
245
|
-
end
|
246
|
-
|
247
|
-
test "#destroy with habtm relationship" do
|
248
|
-
webmock(:get, "/ships", where: {id: 1}, limit: 1).to_return(
|
249
|
-
body: [{id: 1, fleet_id: nil, name: 'Armada Uno'}].to_json
|
250
|
-
)
|
251
|
-
req_stub = webmock(:delete, '/ships/1').to_return(
|
252
|
-
status: 204
|
253
|
-
)
|
254
|
-
|
255
|
-
ship = Ship.find(1)
|
256
|
-
assert ship.destroy
|
257
|
-
assert_requested req_stub
|
258
|
-
end
|
259
|
-
|
260
159
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ActiveRecord::LimitTest < ActiveSupport::TestCase
|
4
|
+
schema do
|
5
|
+
create_table "ships" do |t|
|
6
|
+
t.string "name", limit: 255
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class Ship < ActiveRecord::Base
|
11
|
+
end
|
12
|
+
|
13
|
+
test '::limit' do
|
14
|
+
webmock(:get, "/ships", {limit: 5000}).to_return(body: [{id: 42}].to_json)
|
15
|
+
assert_equal Ship.limit(5000).map(&:id), [42]
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sunstone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.1.0.
|
4
|
+
version: 5.1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jon Bracy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -286,6 +286,7 @@ files:
|
|
286
286
|
- lib/sunstone/gis.rb
|
287
287
|
- lib/sunstone/version.rb
|
288
288
|
- sunstone.gemspec
|
289
|
+
- test/active_record/associations/belongs_to_test.rb
|
289
290
|
- test/active_record/associations/has_and_belongs_to_many_test.rb
|
290
291
|
- test/active_record/associations/has_many_test.rb
|
291
292
|
- test/active_record/eager_loading_test.rb
|
@@ -295,6 +296,7 @@ files:
|
|
295
296
|
- test/active_record/query/count_test.rb
|
296
297
|
- test/active_record/query/distinct_test.rb
|
297
298
|
- test/active_record/query/find_test.rb
|
299
|
+
- test/active_record/query/limit_test.rb
|
298
300
|
- test/active_record/query/order_test.rb
|
299
301
|
- test/active_record/query/where_test.rb
|
300
302
|
- test/active_record/query_test.rb
|
@@ -324,11 +326,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
324
326
|
version: '0'
|
325
327
|
requirements: []
|
326
328
|
rubyforge_project:
|
327
|
-
rubygems_version: 2.6.
|
329
|
+
rubygems_version: 2.6.11
|
328
330
|
signing_key:
|
329
331
|
specification_version: 4
|
330
332
|
summary: A library for interacting with REST APIs
|
331
333
|
test_files:
|
334
|
+
- test/active_record/associations/belongs_to_test.rb
|
332
335
|
- test/active_record/associations/has_and_belongs_to_many_test.rb
|
333
336
|
- test/active_record/associations/has_many_test.rb
|
334
337
|
- test/active_record/eager_loading_test.rb
|
@@ -338,6 +341,7 @@ test_files:
|
|
338
341
|
- test/active_record/query/count_test.rb
|
339
342
|
- test/active_record/query/distinct_test.rb
|
340
343
|
- test/active_record/query/find_test.rb
|
344
|
+
- test/active_record/query/limit_test.rb
|
341
345
|
- test/active_record/query/order_test.rb
|
342
346
|
- test/active_record/query/where_test.rb
|
343
347
|
- test/active_record/query_test.rb
|