Sha256: 8d2d221944631e344d66d3a2d1c25e4ae7d4ef0f0445096f2b3383d4f6b4c5b4

Contents?: true

Size: 1.36 KB

Versions: 26

Compression:

Stored size: 1.36 KB

Contents

module ZendeskAPI
  # @private
  module Sideloading
    def self.included(klass)
      klass.send(:attr_reader, :included)
    end

    def set_includes(resource_or_resources, includes, body)
      @included = {}

      includes.each do |side_load|
        unless body.key?(side_load.to_s)
          @client.config.logger.warn "Missing #{side_load} key in response -- cannot side load"
        end
      end

      resources = [resource_or_resources].flatten.compact
      resource_class = resources.first.class

      return if resources.empty?

      body.keys.each do |name|
        @included[name] = body[name]
        _side_load(name, resource_class, resources)
      end
    end


    private

    # Traverses the resource looking for associations
    # then descends into those associations and checks for applicable
    # resources to side load
    def _side_load(name, klass, resources)
      associations = klass.associated_with(name)

      associations.each do |association|
        association.side_load(resources, @included[name])
      end

      resources.each do |resource|
        loaded_associations = resource.loaded_associations
        loaded_associations.each do |association|
          loaded = resource.send(association[:name])
          next unless loaded
          _side_load(name, association[:class], [loaded].flatten)
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
zendesk_api-0.3.14 lib/zendesk_api/sideloading.rb
zendesk_api-0.3.13 lib/zendesk_api/sideloading.rb
zendesk_api-0.3.12 lib/zendesk_api/sideloading.rb
zendesk_api-0.4.0.rc2 lib/zendesk_api/sideloading.rb
zendesk_api-0.3.11 lib/zendesk_api/sideloading.rb
zendesk_api-0.3.10 lib/zendesk_api/sideloading.rb
zendesk_api-0.3.9 lib/zendesk_api/sideloading.rb
zendesk_api-0.3.8 lib/zendesk_api/sideloading.rb
zendesk_api-0.4.0.rc1 lib/zendesk_api/sideloading.rb
zendesk_api-0.3.7 lib/zendesk_api/sideloading.rb
zendesk_api-0.3.6 lib/zendesk_api/sideloading.rb
zendesk_api-0.3.5 lib/zendesk_api/sideloading.rb
zendesk_api-0.3.4 lib/zendesk_api/sideloading.rb
zendesk_api-0.3.3 lib/zendesk_api/sideloading.rb
zendesk_api-0.3.2 lib/zendesk_api/sideloading.rb
zendesk_api-0.3.1 lib/zendesk_api/sideloading.rb
zendesk_api-0.3.0 lib/zendesk_api/sideloading.rb
zendesk_api-0.2.6 lib/zendesk_api/sideloading.rb
zendesk_api-0.2.5 lib/zendesk_api/sideloading.rb
zendesk_api-0.2.4 lib/zendesk_api/sideloading.rb