Skip to content

Added dirty_tracking support for nested objects #525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/intercom/traits/api_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def to_hash
def to_submittable_hash
submittable_hash = {}
to_hash.each do |attribute, value|
submittable_hash[attribute] = value if submittable_attribute?(attribute, value)
next unless submittable_attribute?(attribute, value)

submittable_hash[attribute] = value.respond_to?(:to_submittable_hash) ? value.to_submittable_hash : value
end
submittable_hash
end
Expand Down
9 changes: 8 additions & 1 deletion lib/intercom/traits/dirty_tracking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ def mark_field_as_changed!(field_name)

def field_changed?(field_name)
@changed_fields ||= Set.new
@changed_fields.include?(field_name.to_s)
field = instance_variable_get("@#{field_name}")
if field.respond_to?(:field_changed?)
field.to_hash.any? do |attribute, _|
field.field_changed?(attribute)
end
else
@changed_fields.include?(field_name.to_s)
end
end

def instance_variables_excluding_dirty_tracking_field
Expand Down
39 changes: 35 additions & 4 deletions spec/unit/intercom/traits/api_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@
'metadata' => {
'type' => 'user',
'color' => 'cyan'
} }
},
'nested_fields' => {
'type' => 'nested_fields_content',
'field_1' => {
'type' => 'field_content',
'name' => 'Nested Field'
}
}
}
end

let(:object_hash) do
Expand All @@ -35,6 +43,13 @@
metadata: {
type: 'user',
color: 'cyan'
},
nested_fields: {
type: 'nested_fields_content',
field_1: {
type: 'field_content',
name: 'Nested Field'
}
}
}
end
Expand Down Expand Up @@ -97,7 +112,7 @@
it 'an initialized ApiResource is equal to one generated from a response' do
class ConcreteApiResource; include Intercom::Traits::ApiResource; end
initialized_api_resource = ConcreteApiResource.new(object_json)
except(object_json, 'type').keys.each do |attribute|
except(object_json, 'type', 'nested_fields').keys.each do |attribute|
Copy link
Contributor Author

@rahulgdsouza rahulgdsouza Jun 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Temporarily added.

  • The test checks for equality, The typed objects currently have no definition for equality.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Equality for lightweight classes will be addressed in a separate PR.

assert_equal initialized_api_resource.send(attribute), api_resource.send(attribute)
end
end
Expand All @@ -107,12 +122,28 @@ class ConcreteApiResource; include Intercom::Traits::ApiResource; end

api_resource.from_hash(object_hash)
initialized_api_resource = ConcreteApiResource.new(object_hash)

except(object_json, 'type').keys.each do |attribute|
except(object_json, 'type', 'nested_fields').keys.each do |attribute|
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Temporarily added.

  • Typed objects are created only when the hash has a key type as string. Symbol is not taken into account.

Even though this test passes. It should've ideally failed like the previous one.
Added nested_fields here just to maintain consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be addressed in a separate PR, Possible solutions

  • Manual check of type as string/symbol
  • Hash.symbolize_keys
  • Hash.with_indifferent_access

assert_equal initialized_api_resource.send(attribute), api_resource.send(attribute)
end
end

it 'correctly generates submittable hash when no updates' do
assert_equal api_resource.to_submittable_hash, {}
end

it 'correctly generates submittable hash when there are updates' do
api_resource.name = 'SuperSuite updated'
api_resource.nested_fields.field_1.name = 'Updated Name'
assert_equal api_resource.to_submittable_hash, {
'name' => 'SuperSuite updated',
'nested_fields' => {
'field_1' => {
'name' => 'Updated Name'
}
}
}
end

def except(h, *keys)
keys.each { |key| h.delete(key) }
h
Expand Down