Sha256: 48c47d281136136ddd792bcff6483c455be4daa7e0e3a076b06aea71cad5284f

Contents?: true

Size: 1.3 KB

Versions: 17

Compression:

Stored size: 1.3 KB

Contents

module ApiResource

	module Finders

		class SingleObjectAssociationFinder < AbstractFinder

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

				@internal_object = internal_object
			end

			# since it is only a single object we can just load from
			# the service_uri and deal with includes
			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 nil if data.blank?

					# we want to handle an array if we get one back from our endpoint
					# this allows for more flexibility
					data = data.first if data.is_a?(Array)

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

				@loaded = true
				# now that the object is loaded, resolve the includes
				id_hash = self.condition.included_objects.inject({}) do |accum, assoc|
					accum[assoc] = Array.wrap(
						@internal_object.send(
							@internal_object.class.association_foreign_key_field(assoc)
						)
					)
					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/single_object_association_finder.rb
api_resource-0.6.20 lib/api_resource/finders/single_object_association_finder.rb
api_resource-0.6.19 lib/api_resource/finders/single_object_association_finder.rb
api_resource-0.6.18 lib/api_resource/finders/single_object_association_finder.rb
api_resource-0.6.17 lib/api_resource/finders/single_object_association_finder.rb
api_resource-0.6.16 lib/api_resource/finders/single_object_association_finder.rb
api_resource-0.6.15 lib/api_resource/finders/single_object_association_finder.rb
api_resource-0.6.14 lib/api_resource/finders/single_object_association_finder.rb
api_resource-0.6.13 lib/api_resource/finders/single_object_association_finder.rb
api_resource-0.6.12 lib/api_resource/finders/single_object_association_finder.rb
api_resource-0.6.11 lib/api_resource/finders/single_object_association_finder.rb
api_resource-0.6.9 lib/api_resource/finders/single_object_association_finder.rb
api_resource-0.6.10 lib/api_resource/finders/single_object_association_finder.rb
api_resource-0.6.8 lib/api_resource/finders/single_object_association_finder.rb
api_resource-0.6.7 lib/api_resource/finders/single_object_association_finder.rb
api_resource-0.6.6 lib/api_resource/finders/single_object_association_finder.rb
api_resource-0.6.5 lib/api_resource/finders/single_object_association_finder.rb