Skip to content

Commit a4472ae

Browse files
authored
Added dirty_tracking support for nested objects (#525)
1 parent c49b824 commit a4472ae

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

lib/intercom/traits/api_resource.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ def to_hash
4040
def to_submittable_hash
4141
submittable_hash = {}
4242
to_hash.each do |attribute, value|
43-
submittable_hash[attribute] = value if submittable_attribute?(attribute, value)
43+
next unless submittable_attribute?(attribute, value)
44+
45+
submittable_hash[attribute] = value.respond_to?(:to_submittable_hash) ? value.to_submittable_hash : value
4446
end
4547
submittable_hash
4648
end

lib/intercom/traits/dirty_tracking.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ def mark_field_as_changed!(field_name)
2222

2323
def field_changed?(field_name)
2424
@changed_fields ||= Set.new
25-
@changed_fields.include?(field_name.to_s)
25+
field = instance_variable_get("@#{field_name}")
26+
if field.respond_to?(:field_changed?)
27+
field.to_hash.any? do |attribute, _|
28+
field.field_changed?(attribute)
29+
end
30+
else
31+
@changed_fields.include?(field_name.to_s)
32+
end
2633
end
2734

2835
def instance_variables_excluding_dirty_tracking_field

spec/unit/intercom/traits/api_resource_spec.rb

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@
1717
'metadata' => {
1818
'type' => 'user',
1919
'color' => 'cyan'
20-
} }
20+
},
21+
'nested_fields' => {
22+
'type' => 'nested_fields_content',
23+
'field_1' => {
24+
'type' => 'field_content',
25+
'name' => 'Nested Field'
26+
}
27+
}
28+
}
2129
end
2230

2331
let(:object_hash) do
@@ -35,6 +43,13 @@
3543
metadata: {
3644
type: 'user',
3745
color: 'cyan'
46+
},
47+
nested_fields: {
48+
type: 'nested_fields_content',
49+
field_1: {
50+
type: 'field_content',
51+
name: 'Nested Field'
52+
}
3853
}
3954
}
4055
end
@@ -97,7 +112,7 @@
97112
it 'an initialized ApiResource is equal to one generated from a response' do
98113
class ConcreteApiResource; include Intercom::Traits::ApiResource; end
99114
initialized_api_resource = ConcreteApiResource.new(object_json)
100-
except(object_json, 'type').keys.each do |attribute|
115+
except(object_json, 'type', 'nested_fields').keys.each do |attribute|
101116
assert_equal initialized_api_resource.send(attribute), api_resource.send(attribute)
102117
end
103118
end
@@ -107,12 +122,28 @@ class ConcreteApiResource; include Intercom::Traits::ApiResource; end
107122

108123
api_resource.from_hash(object_hash)
109124
initialized_api_resource = ConcreteApiResource.new(object_hash)
110-
111-
except(object_json, 'type').keys.each do |attribute|
125+
except(object_json, 'type', 'nested_fields').keys.each do |attribute|
112126
assert_equal initialized_api_resource.send(attribute), api_resource.send(attribute)
113127
end
114128
end
115129

130+
it 'correctly generates submittable hash when no updates' do
131+
assert_equal api_resource.to_submittable_hash, {}
132+
end
133+
134+
it 'correctly generates submittable hash when there are updates' do
135+
api_resource.name = 'SuperSuite updated'
136+
api_resource.nested_fields.field_1.name = 'Updated Name'
137+
assert_equal api_resource.to_submittable_hash, {
138+
'name' => 'SuperSuite updated',
139+
'nested_fields' => {
140+
'field_1' => {
141+
'name' => 'Updated Name'
142+
}
143+
}
144+
}
145+
end
146+
116147
def except(h, *keys)
117148
keys.each { |key| h.delete(key) }
118149
h

0 commit comments

Comments
 (0)