Sha256: 2007ea9b0a2ee597cbe82b5a7b756e10563584ff3d11867646ea96045775f300

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

module ApiResource

	module Finders

		class MultiObjectAssociationFinder < AbstractFinder

			delegate :select,
				to: :internal_object

			# If they pass in the internal object just skip the first
			# step and apply the includes
			def initialize(klass, condition, internal_object = nil)
				super(klass, condition)

				if internal_object
					@internal_object = internal_object
				end
			end

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

				return [] if self.response.blank?

				@internal_object ||= self.klass.instantiate_collection(
					Array.wrap(self.response)
				)

				@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
		end

	end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
api_resource-0.6.25 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.24 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.23 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.22 lib/api_resource/finders/multi_object_association_finder.rb