Sha256: 491109beba89ccd80672a395614764df8023c08f5a5589bcf806947483dcadc7

Contents?: true

Size: 1.22 KB

Versions: 17

Compression:

Stored size: 1.22 KB

Contents

module ApiResource

	module Finders

		class MultiObjectAssociationFinder < AbstractFinder

			# 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)

				@internal_object = internal_object
			end

			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
		end

	end

end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
api_resource-0.6.21 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.20 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.19 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.18 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.17 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.16 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.15 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.14 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.13 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.12 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.11 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.9 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.10 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.8 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.7 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.6 lib/api_resource/finders/multi_object_association_finder.rb
api_resource-0.6.5 lib/api_resource/finders/multi_object_association_finder.rb