Skip to content

Commit d47f6e6

Browse files
committed
fetch ticket field data after first custom field call
1 parent 4558c72 commit d47f6e6

File tree

8 files changed

+33
-58
lines changed

8 files changed

+33
-58
lines changed

lib/zendesk_api/client.rb

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

114113
# token impersonation for the scope of the block
@@ -157,11 +156,9 @@ def self.check_deprecated_namespace_usage(attributes, name)
157156
end
158157
end
159158

160-
def load_custom_fields_metadata
161-
return unless @config.preload_custom_fields_metadata
162-
163-
@account_data["custom_fields"] ||= {}
164-
ticket_fields.each { |field| @account_data["custom_fields"][field.title] = field.id }
159+
def refresh_custom_fields_metadata
160+
account_data[:custom_fields] ||= {}
161+
ticket_fields.each { |field| account_data[:custom_fields][field.title] = field.id }
165162
end
166163

167164
protected

lib/zendesk_api/configuration.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ class Configuration
5757
# specify if you want instrumentation to be used
5858
attr_accessor :instrumentation
5959

60-
# set to true if you want to preload custom fields metadata
61-
attr_accessor :preload_custom_fields_metadata
62-
6360
def initialize
6461
@client_options = {}
6562
@use_resource_cache = true

lib/zendesk_api/error.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,5 @@ 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
4943
end
5044
end

lib/zendesk_api/resource.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ def account_data
8080
@client.account_data
8181
end
8282

83+
def refresh_custom_fields_metadata
84+
@client.refresh_custom_fields_metadata
85+
end
86+
8387
# Passes the method onto the attributes hash.
8488
# If the attributes are nested (e.g. { :tickets => { :id => 1 } }), passes the method onto the nested hash.
8589
def method_missing(*args, &)

lib/zendesk_api/resources.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -551,26 +551,30 @@ def initialize(ticket)
551551
@ticket = ticket
552552
end
553553

554-
def [](name)
555-
find_by_name(name)["value"]
554+
def [](field_name)
555+
find_by_name(field_name)["value"]
556556
end
557557

558-
def []=(name, value)
559-
find_by_name(name)["value"] = value
558+
def []=(field_name, value)
559+
find_by_name(field_name)["value"] = value
560560
end
561561

562562
private
563563

564-
def find_by_name(name)
565-
raise ZendeskAPI::Error::CustomFieldsMetadataConfigurationError unless @ticket.account_data["custom_fields"]
564+
def find_by_name(field_name)
565+
@ticket.refresh_custom_fields_metadata unless @ticket.account_data.has_key?(:custom_fields)
566566

567-
custom_field_id = @ticket.account_data["custom_fields"][name]
568-
@ticket.custom_fields.find { |cf| cf["id"] == custom_field_id }
567+
custom_field_id = @ticket.account_data[:custom_fields][field_name]
568+
raise ArgumentError, "No custom field named '#{field_name}' found in field definitions" if custom_field_id.nil?
569+
570+
custom_field = @ticket.custom_fields.find { |cf| cf["id"] == custom_field_id }
571+
raise ArgumentError, "No custom field with id #{custom_field_id} found. Field name: '#{field_name}'" if custom_field.nil?
572+
573+
custom_field
569574
end
570575
end
571576

572577
# Returns a custom field accessor that supports bracket notation.
573-
# You need to enable `config.preload_custom_fields_metadata` before using it.
574578
# Usage:
575579
# ticket.custom_field["field name"] # get
576580
# ticket.custom_field["field name"] = "value" # set

spec/core/client_spec.rb

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -394,25 +394,4 @@ 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
418397
end

spec/core/configuration_spec.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,4 @@
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
3834
end

spec/live/ticket_spec.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,13 @@ def valid_attributes
112112

113113
describe "custom fields" do
114114
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
115+
@ticket = ZendeskAPI::Ticket.create(client, {
116+
subject: "live spec subject for custom fields test",
117+
description: "live spec description for custom fields test"
118+
})
123119
end
124120

125121
after do
126-
client.config.preload_custom_fields_metadata = false
127122
@ticket&.destroy!
128123
end
129124

@@ -134,6 +129,15 @@ def valid_attributes
134129
@ticket.reload!
135130
expect(@ticket.custom_field["Custom field name"]).to eq("Custom field value")
136131
end
132+
133+
it "raises ArgumentError for non-existent fields" do
134+
expect { @ticket.custom_field["This field does not exist"] }.to raise_error(ArgumentError)
135+
end
136+
137+
it "stores custom fields metadata on a client" do
138+
_custom_field_value = @ticket.custom_field["Custom field name"]
139+
expect(@ticket.account_data[:custom_fields]["Custom field name"]).to eq(9961714922394)
140+
end
137141
end
138142

139143
describe ".incremental_export" do

0 commit comments

Comments
 (0)