Skip to content

Commit ced6d9a

Browse files
Add External ID Field to Contact Model (#185)
* [del-1611] Added support for 'external_id' field in Contact model requests * Responded to PR feedback * Incremented library version to 4.12.0
1 parent a966e55 commit ced6d9a

9 files changed

Lines changed: 160 additions & 17 deletions

File tree

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# chartmogul-ruby Change Log
22

3+
## Version 4.12.0 - Mar 16, 2026
4+
- Add `external_id` key to Contact model
5+
36
## Version 4.11.0 - Jan 6, 2026
47
- Add new API usage for Customer Subscriptions connect and disconnect
58
- Update retrieve to accept query params

lib/chartmogul/contact.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Contact < APIResource
2020
writeable_attr :linked_in
2121
writeable_attr :twitter
2222
writeable_attr :notes
23+
writeable_attr :external_id
2324
writeable_attr :custom
2425

2526
include API::Actions::Create
@@ -43,13 +44,15 @@ def serialize_for_write
4344
if attribute_name == :custom && attribute_value.is_a?(Hash)
4445
payload = attribute_value.each_with_object([]) do |custom_value, arr|
4546
key, value = custom_value
46-
arr << { key: key, value: value }
47+
arr << ({ key:, value: })
4748
end
4849
attributes[:custom] = payload
4950
else
5051
attributes[attribute_name] = attribute_value
5152
end
5253
end
54+
# Include external_id attribute even when nil so callers can explicitly clear it
55+
attributes[:external_id] = nil if instance_variable_defined?(:@external_id) && external_id.nil?
5356
end
5457
end
5558
end

lib/chartmogul/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module ChartMogul
4-
VERSION = '4.11.0'
4+
VERSION = '4.12.0'
55
end

spec/chartmogul/contact_spec.rb

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33
require 'spec_helper'
44

55
describe ChartMogul::Contact do
6+
shared_examples 'creates contact with nil external_id' do
7+
it 'creates the contact correctly' do
8+
contact = described_class.create!(**create_attributes)
9+
expect(contact).to have_attributes(uuid: contact_uuid, external_id: nil)
10+
end
11+
end
12+
613
let(:attrs) do
714
{
815
uuid: contact_uuid,
9-
customer_uuid: customer_uuid,
10-
data_source_uuid: data_source_uuid,
16+
customer_uuid:,
17+
data_source_uuid:,
1118
customer_external_id: 'cus_004',
19+
external_id: 'contact_external_id_001',
1220
first_name: 'First name',
1321
last_name: 'Last name',
1422
position: 9,
@@ -20,7 +28,7 @@
2028
notes: 'Heading\nBody\nFooter',
2129
custom: {
2230
MyStringAttribute: 'Test',
23-
MyIntegerAttribute: 123,
31+
MyIntegerAttribute: 123
2432
}
2533
}
2634
end
@@ -29,6 +37,7 @@
2937
let(:data_source_uuid) { 'ds_03cfd2c4-2c7e-11ee-ab23-cb0f008cff46' }
3038
let(:updated_attributes) do
3139
{
40+
external_id: 'contact_external_id_002',
3241
first_name: 'Foo',
3342
last_name: 'Bar',
3443
email: 'contact2@example.com',
@@ -71,35 +80,55 @@
7180

7281
expect(contact).to have_attributes(
7382
uuid: contact_uuid,
74-
customer_uuid: customer_uuid,
75-
data_source_uuid: data_source_uuid,
83+
customer_uuid:,
84+
data_source_uuid:,
7685
email: 'contact@example.com'
7786
)
7887
end
7988

8089
it 'creates the contact correctly' do
8190
attributes = {
82-
customer_uuid: customer_uuid,
83-
data_source_uuid: data_source_uuid,
84-
email: 'contact@example.com'
91+
customer_uuid:,
92+
data_source_uuid:,
93+
email: 'contact@example.com',
94+
external_id: 'contact_external_id_001'
8595
}
8696
contact = described_class.create!(**attributes)
8797
expect(contact).to have_attributes(uuid: contact_uuid, **attributes)
8898
end
8999

100+
context 'with null external_id' do
101+
let(:create_attributes) do
102+
{ customer_uuid:, data_source_uuid:, email: 'contact@example.com', external_id: nil }
103+
end
104+
include_examples 'creates contact with nil external_id'
105+
end
106+
107+
context 'without external_id' do
108+
let(:create_attributes) do
109+
{ customer_uuid:, data_source_uuid:, email: 'contact@example.com' }
110+
end
111+
include_examples 'creates contact with nil external_id'
112+
end
113+
90114
it 'updates the contact correctly with the class method' do
91115
updated_contact = described_class.update!(
92116
contact_uuid, **updated_attributes
93117
)
94118

95119
expect(updated_contact).to have_attributes(
96120
uuid: contact_uuid,
97-
data_source_uuid: data_source_uuid,
98-
customer_uuid: customer_uuid,
121+
data_source_uuid:,
122+
customer_uuid:,
99123
**updated_attributes
100124
)
101125
end
102126

127+
it 'updates the contact with null external_id correctly' do
128+
updated_contact = described_class.update!(contact_uuid, external_id: nil)
129+
expect(updated_contact).to have_attributes(uuid: contact_uuid, external_id: nil)
130+
end
131+
103132
it 'destroys the contact correctly' do
104133
uuid_to_delete = 'con_ab1e60d4-7690-11ee-84d7-f7e55168a5df'
105134
deleted_contact = described_class.destroy!(uuid: uuid_to_delete)
@@ -111,7 +140,7 @@
111140
from_uuid = 'con_6f0b7208-7690-11ee-8857-9f75f1321afd'
112141

113142
contact_result = described_class.merge!(
114-
into_uuid: into_uuid, from_uuid: from_uuid
143+
into_uuid:, from_uuid:
115144
)
116145
expect(contact_result).to eq(true)
117146
end

spec/fixtures/vcr_cassettes/ChartMogul_Contact/API_Actions/creates_the_contact_correctly.yml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/ChartMogul_Contact/API_Actions/updates_the_contact_correctly_with_the_class_method.yml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/ChartMogul_Contact/API_Actions/updates_the_contact_with_null_external_id_correctly.yml

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/ChartMogul_Contact/API_Actions/with_null_external_id/creates_the_contact_correctly.yml

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/ChartMogul_Contact/API_Actions/without_external_id/creates_the_contact_correctly.yml

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)