Module: ActiveShopifyGraphQL::Model::Connections

Extended by:
ActiveSupport::Concern
Included in:
ActiveShopifyGraphQL::Model
Defined in:
lib/active_shopify_graphql/model/connections.rb

Instance Method Summary collapse

Instance Method Details

#populate_inverse_cache_for_connection(records, connection_config, parent) ⇒ Object

Instance method to populate inverse cache for lazy-loaded connections



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/active_shopify_graphql/model/connections.rb', line 152

def populate_inverse_cache_for_connection(records, connection_config, parent)
  return unless connection_config[:inverse_of]
  return if records.nil? || (records.is_a?(Array) && records.empty?)

  inverse_name = connection_config[:inverse_of]
  target_class = connection_config[:class_name].constantize

  # Ensure target class has the inverse connection defined
  return unless target_class.respond_to?(:connections) && target_class.connections[inverse_name]

  inverse_type = target_class.connections[inverse_name][:type]
  records_array = records.is_a?(Array) ? records : [records]

  records_array.each do |record|
    next unless record

    record.instance_variable_set(:@_connection_cache, {}) unless record.instance_variable_get(:@_connection_cache)
    cache = record.instance_variable_get(:@_connection_cache)

    cache[inverse_name] =
      if inverse_type == :singular
        parent
      else
        # For collection inverses, wrap parent in an array
        [parent]
      end
  end
end