From 048ef78f538f1373d59ec2f49a5ee0abc4cb3db8 Mon Sep 17 00:00:00 2001 From: Jury Razumau Date: Fri, 7 Nov 2025 17:00:31 +0100 Subject: [PATCH] add option to preload custom fields metadata --- lib/zendesk_api/client.rb | 11 +++++++++++ lib/zendesk_api/configuration.rb | 3 +++ lib/zendesk_api/resource.rb | 4 ++++ lib/zendesk_api/resources.rb | 14 ++++++++++++++ 4 files changed, 32 insertions(+) diff --git a/lib/zendesk_api/client.rb b/lib/zendesk_api/client.rb index b9bf116e..9e5af517 100644 --- a/lib/zendesk_api/client.rb +++ b/lib/zendesk_api/client.rb @@ -32,6 +32,8 @@ class Client attr_reader :config # @return [Array] Custom response callbacks attr_reader :callbacks + # @return [Hash] Memoized account data + attr_reader :account_data # Handles resources such as 'tickets'. Any options are passed to the underlying collection, except reload which disregards # memoization and creates a new Collection instance. @@ -95,6 +97,7 @@ def initialize @callbacks = [] @resource_cache = {} + @account_data = {} check_url check_instrumentation @@ -105,6 +108,7 @@ def initialize set_token_auth set_default_logger add_warning_callback + preload_custom_fields_metadata end # token impersonation for the scope of the block @@ -266,6 +270,13 @@ def add_warning_callback end end + def preload_custom_fields_metadata + return unless @config.preload_custom_fields_metadata + + @account_data["custom_fields"] ||= {} + ticket_fields.each { |field| @account_data["custom_fields"][field.title] = field.id } + end + # See https://lostisland.github.io/faraday/middleware/authentication def set_authentication(builder, config) if config.access_token && !config.url_based_access_token diff --git a/lib/zendesk_api/configuration.rb b/lib/zendesk_api/configuration.rb index b2e22a7f..a9b52d66 100644 --- a/lib/zendesk_api/configuration.rb +++ b/lib/zendesk_api/configuration.rb @@ -57,6 +57,9 @@ class Configuration # specify if you want instrumentation to be used attr_accessor :instrumentation + # set to true if you want to preload custom fields metadata + attr_accessor :preload_custom_fields_metadata + def initialize @client_options = {} @use_resource_cache = true diff --git a/lib/zendesk_api/resource.rb b/lib/zendesk_api/resource.rb index 3cf01ba9..16285658 100644 --- a/lib/zendesk_api/resource.rb +++ b/lib/zendesk_api/resource.rb @@ -76,6 +76,10 @@ def initialize(client, attributes = {}) @attributes.clear_changes unless new_record? end + def account_data + @client.account_data + end + # Passes the method onto the attributes hash. # If the attributes are nested (e.g. { :tickets => { :id => 1 } }), passes the method onto the nested hash. def method_missing(*args, &) diff --git a/lib/zendesk_api/resources.rb b/lib/zendesk_api/resources.rb index ec55822d..599b8e05 100644 --- a/lib/zendesk_api/resources.rb +++ b/lib/zendesk_api/resources.rb @@ -546,6 +546,20 @@ def to_param has_many :incidents, class: Ticket + def custom_field_by_name(name) + raise "Custom fields metadata missing. Enable config.preload_custom_fields_metadata" unless account_data["custom_fields"] + + custom_field_id = account_data["custom_fields"][name] + custom_fields.find { |cf| cf["id"] == custom_field_id }["value"] + end + + def set_custom_field_by_name(name, value) + raise "Custom fields metadata missing. Enable config.preload_custom_fields_metadata" unless account_data["custom_fields"] + + custom_field_id = account_data["custom_fields"][name] + custom_fields.find { |cf| cf["id"] == custom_field_id }["value"] = value + end + # Gets a incremental export of tickets from the start_time until now. # @param [Client] client The {Client} object to be used # @param [Integer] start_time The start_time parameter