Class: ApiResource::Finders::MultiObjectAssociationFinder

Inherits:
AbstractFinder
  • Object
show all
Defined in:
lib/api_resource/finders/multi_object_association_finder.rb

Instance Attribute Summary

Attributes inherited from AbstractFinder

#condition, #found, #internal_object, #klass

Instance Method Summary collapse

Methods inherited from AbstractFinder

#all, #method_missing

Constructor Details

#initialize(klass, condition, internal_object = nil) ⇒ MultiObjectAssociationFinder

If they pass in the internal object just skip the first step and apply the includes



9
10
11
12
13
# File 'lib/api_resource/finders/multi_object_association_finder.rb', line 9

def initialize(klass, condition, internal_object = nil)
  super(klass, condition)

  @internal_object = internal_object
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ApiResource::Finders::AbstractFinder

Instance Method Details

#loadObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/api_resource/finders/multi_object_association_finder.rb', line 15

def load
  # otherwise just instantiate the record
  unless self.condition.remote_path
    raise "Tried to load association without a remote path"
  end

  unless @internal_object
    data = self.klass.connection.get(self.build_load_path)
    return [] if data.blank?

    # handle non-array data for more flexibility in our endpoints
    data = [data] unless data.is_a?(Array)

    @internal_object = self.klass.instantiate_collection(data)
  end

  @loaded = true

  id_hash = self.condition.included_objects.inject({}) do |accum, assoc|
    accum[assoc] = @internal_object.collect do |obj|
      obj.send(obj.class.association_foreign_key_field(assoc))
    end

    accum[assoc].flatten!
    accum[assoc].uniq!
    accum
  end

  included_objects = self.load_includes(id_hash)

  self.apply_includes(@internal_object, included_objects)

  return @internal_object
end