Skip to content

Commit d471f70

Browse files
committed
switch to bracket methods
1 parent 048ef78 commit d471f70

File tree

7 files changed

+92
-17
lines changed

7 files changed

+92
-17
lines changed

lib/zendesk_api/client.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def initialize
108108
set_token_auth
109109
set_default_logger
110110
add_warning_callback
111-
preload_custom_fields_metadata
111+
load_custom_fields_metadata
112112
end
113113

114114
# token impersonation for the scope of the block
@@ -157,6 +157,11 @@ def self.check_deprecated_namespace_usage(attributes, name)
157157
end
158158
end
159159

160+
def load_custom_fields_metadata
161+
@account_data["custom_fields"] ||= {}
162+
ticket_fields.each { |field| @account_data["custom_fields"][field.title] = field.id }
163+
end
164+
160165
protected
161166

162167
# Called by {#connection} to build a connection. Can be overwritten in a
@@ -270,13 +275,6 @@ def add_warning_callback
270275
end
271276
end
272277

273-
def preload_custom_fields_metadata
274-
return unless @config.preload_custom_fields_metadata
275-
276-
@account_data["custom_fields"] ||= {}
277-
ticket_fields.each { |field| @account_data["custom_fields"][field.title] = field.id }
278-
end
279-
280278
# See https://lostisland.github.io/faraday/middleware/authentication
281279
def set_authentication(builder, config)
282280
if config.access_token && !config.url_based_access_token

lib/zendesk_api/error.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,11 @@ def generate_error_msg(response_body)
4040
class NetworkError < ClientError; end
4141
class RecordNotFound < ClientError; end
4242
class RateLimited < ClientError; end
43+
44+
class CustomFieldsMetadataConfigurationError < StandardError
45+
def to_s
46+
"Custom fields metadata missing. Enable config.preload_custom_fields_metadata"
47+
end
48+
end
4349
end
4450
end

lib/zendesk_api/resources.rb

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -546,18 +546,37 @@ def to_param
546546

547547
has_many :incidents, class: Ticket
548548

549-
def custom_field_by_name(name)
550-
raise "Custom fields metadata missing. Enable config.preload_custom_fields_metadata" unless account_data["custom_fields"]
549+
class CustomFieldAccessor
550+
def initialize(ticket)
551+
@ticket = ticket
552+
end
551553

552-
custom_field_id = account_data["custom_fields"][name]
553-
custom_fields.find { |cf| cf["id"] == custom_field_id }["value"]
554-
end
554+
def [](name)
555+
find_by_name(name)["value"]
556+
end
557+
558+
def []=(name, value)
559+
find_by_name(name)["value"] = value
560+
end
555561

556-
def set_custom_field_by_name(name, value)
557-
raise "Custom fields metadata missing. Enable config.preload_custom_fields_metadata" unless account_data["custom_fields"]
562+
private
563+
564+
def find_by_name(name)
565+
raise ZendeskAPI::Error::CustomFieldsMetadataConfigurationError unless @ticket.account_data["custom_fields"]
566+
567+
custom_field_id = @ticket.account_data["custom_fields"][name]
568+
@ticket.custom_fields.find { |cf| cf["id"] == custom_field_id }
569+
end
570+
end
558571

559-
custom_field_id = account_data["custom_fields"][name]
560-
custom_fields.find { |cf| cf["id"] == custom_field_id }["value"] = value
572+
# Returns a custom field accessor that supports bracket notation.
573+
# You need to enable `config.preload_custom_fields_metadata` before using it.
574+
# Usage:
575+
# ticket.custom_field["field name"] # get
576+
# ticket.custom_field["field name"] = "value" # set
577+
# You need to call `save!` on a ticket after changing a custom field value.
578+
def custom_field
579+
@custom_field_accessor ||= CustomFieldAccessor.new(self)
561580
end
562581

563582
# Gets a incremental export of tickets from the start_time until now.

spec/core/client_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,25 @@ def url.to_str
394394
expect(Thread.current[:zendesk_thread_local_username]).to eq(original)
395395
end
396396
end
397+
398+
context "custom fields metadata preloading" do
399+
it "doesn’t preload metadata by default" do
400+
expect(subject.account_data["custom_fields"]).to be_nil
401+
end
402+
403+
it "preloads metadata if configured" do
404+
fields = {ticket_fields: [{title: "field one", id: 12}]}.to_json
405+
client = ZendeskAPI::Client.new do |config|
406+
config.preload_custom_fields_metadata = true
407+
config.url = "https://example.zendesk.com/api/v2"
408+
config.adapter = :test
409+
config.adapter_proc = proc do |stub|
410+
stub.get "/api/v2/ticket_fields" do |env|
411+
[200, {"content-type": "application/json", Authorization: env.request_headers["Authorization"]}, fields]
412+
end
413+
end
414+
end
415+
expect(client.account_data["custom_fields"]).to eq({"field one" => 12})
416+
end
417+
end
397418
end

spec/core/configuration_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@
3131
it "sets a default for use_resource_cache" do
3232
expect(subject.use_resource_cache).to be true
3333
end
34+
35+
it "has preload_custom_fields_metadata disabled by default" do
36+
expect(subject.preload_custom_fields_metadata).to be_nil
37+
end
3438
end

spec/live/Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ In case you want to create a test account from scratch:
1212
- Add photo to user profile of that end user.
1313
- Create a new ticket and cc "zendesk-api-client-ruby-end-user-#{client.config.username}" (run tests once to create this user)
1414
- Ensure you allow admins to set up user password (or `POST /api/v2/users/{user_id}/password.json` will fail). You can check this in the admin centre > security > advanced
15+
- Add ticket custom field with the display name "Custom field name" and field type "Text".

spec/live/ticket_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,32 @@ def valid_attributes
110110
it_should_be_readable :tickets, :recent
111111
end
112112

113+
describe "custom fields" do
114+
before do
115+
client.config.preload_custom_fields_metadata = true
116+
client.load_custom_fields_metadata
117+
VCR.use_cassette("ticket_create_custom_fields") do
118+
@ticket = ZendeskAPI::Ticket.create(client, {
119+
subject: "live spec subject for custom fields test",
120+
description: "live spec description for custom fields test"
121+
})
122+
end
123+
end
124+
125+
after do
126+
client.config.preload_custom_fields_metadata = false
127+
@ticket&.destroy!
128+
end
129+
130+
it "can write and read custom fields" do
131+
@ticket.custom_field["Custom field name"] = "Custom field value"
132+
@ticket.save!
133+
134+
@ticket.reload!
135+
expect(@ticket.custom_field["Custom field name"]).to eq("Custom field value")
136+
end
137+
end
138+
113139
describe ".incremental_export" do
114140
let(:results) { ZendeskAPI::Ticket.incremental_export(client, Time.at(1023059503)) } # ~ 10 years ago
115141

0 commit comments

Comments
 (0)