Class: DatastaxRails::Reflection::ThroughReflection
- Inherits:
-
AssociationReflection
- Object
- MacroReflection
- AssociationReflection
- DatastaxRails::Reflection::ThroughReflection
- Defined in:
- lib/datastax_rails/reflection.rb
Overview
Holds all the meta-data about a :through association as it was specified in the DatastaxRails class.
Instance Attribute Summary
Attributes inherited from AssociationReflection
Attributes inherited from MacroReflection
#datastax_rails, #denorms, #macro, #name, #options, #plural_name
Instance Method Summary collapse
-
#association_primary_key(klass = nil) ⇒ Object
We want to use the klass from this reflection, rather than just delegate straight to the source_reflection, because the source_reflection may be polymorphic.
-
#chain ⇒ Object
Returns an array of reflections which are involved in this association.
- #check_validity! ⇒ Object
-
#conditions ⇒ Object
Consider the following example:.
-
#nested? ⇒ Boolean
A through association is nested iff there would be more than one join table.
-
#source_macro ⇒ Object
The macro used by the source association.
- #source_options ⇒ Object
-
#source_reflection ⇒ Object
Gets the source of the through reflection.
-
#source_reflection_names ⇒ Object
Gets an array of possible
:through
source reflection names:. - #through_options ⇒ Object
-
#through_reflection ⇒ Object
Returns the AssociationReflection object specified in the
:through
option of a HasManyThrough or HasOneThrough association.
Methods inherited from AssociationReflection
#association_class, #association_foreign_key, #belongs_to?, #build_association, #check_validity_of_inverse!, #column_family, #foreign_key, #foreign_type, #initialize, #inverse?, #inverse_of, #klass, #quoted_column_family, #type, #validate?
Methods inherited from MacroReflection
#==, #class_name, #initialize, #klass, #sanitized_conditions
Constructor Details
This class inherits a constructor from DatastaxRails::Reflection::AssociationReflection
Instance Method Details
#association_primary_key(klass = nil) ⇒ Object
We want to use the klass from this reflection, rather than just delegate straight to the source_reflection, because the source_reflection may be polymorphic. We still need to respect the source_reflection’s :primary_key option, though.
420 421 422 423 424 425 426 427 428 429 |
# File 'lib/datastax_rails/reflection.rb', line 420 def association_primary_key(klass = nil) # Get the "actual" source reflection if the immediate source reflection has a # source reflection itself source_reflection = self.source_reflection while source_reflection.source_reflection source_reflection = source_reflection.source_reflection end source_reflection.[:primary_key] || primary_key(klass || self.klass) end |
#chain ⇒ Object
Returns an array of reflections which are involved in this association. Each item in the array corresponds to a table which will be part of the query for this association.
The chain is built by recursively calling #chain on the source reflection and the through reflection. The base case for the recursion is a normal association, which just returns
- self
-
as its #chain.
352 353 354 355 356 357 358 |
# File 'lib/datastax_rails/reflection.rb', line 352 def chain @chain ||= begin chain = source_reflection.chain + through_reflection.chain chain[0] = self # Use self so we don't lose the information from :source_type chain end end |
#check_validity! ⇒ Object
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
# File 'lib/datastax_rails/reflection.rb', line 448 def check_validity! if through_reflection.nil? fail HasManyThroughAssociationNotFoundError.new(datastax_rails.name, self) end if through_reflection.[:polymorphic] fail HasManyThroughAssociationPolymorphicThroughError.new(datastax_rails.name, self) end if source_reflection.nil? fail HasManyThroughSourceAssociationNotFoundError.new(self) end if [:source_type] && source_reflection.[:polymorphic].nil? fail HasManyThroughAssociationPointlessSourceTypeError.new(datastax_rails.name, self, source_reflection) end if source_reflection.[:polymorphic] && [:source_type].nil? fail HasManyThroughAssociationPolymorphicSourceError.new(datastax_rails.name, self, source_reflection) end if macro == :has_one && through_reflection.collection? fail HasOneThroughCantAssociateThroughCollection.new(datastax_rails.name, self, through_reflection) end check_validity_of_inverse! end |
#conditions ⇒ Object
Consider the following example:
class Person
has_many :articles
has_many :comment_tags, :through => :articles
end
class Article
has_many :comments
has_many :comment_tags, :through => :comments, :source => :tags
end
class Comment
has_many :tags
end
There may be conditions on Person.comment_tags, Article.comment_tags and/or Comment.tags, but only Comment.tags will be represented in the #chain. So this method creates an array of conditions corresponding to the chain. Each item in the #conditions array corresponds to an item in the #chain, and is itself an array of conditions from an arbitrary number of relevant reflections, plus any :source_type or polymorphic :as constraints.
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/datastax_rails/reflection.rb', line 386 def conditions @conditions ||= begin conditions = source_reflection.conditions.map { |c| c.dup } # Add to it the conditions from this reflection if necessary. conditions.first << [:conditions] if [:conditions] through_conditions = through_reflection.conditions if [:source_type] through_conditions.first << { foreign_type => [:source_type] } end # Recursively fill out the rest of the array from the through reflection conditions += through_conditions # And return conditions end end |
#nested? ⇒ Boolean
A through association is nested iff there would be more than one join table
361 362 363 |
# File 'lib/datastax_rails/reflection.rb', line 361 def nested? chain.length > 2 || through_reflection.macro == :has_and_belongs_to_many end |
#source_macro ⇒ Object
The macro used by the source association
408 409 410 |
# File 'lib/datastax_rails/reflection.rb', line 408 def source_macro source_reflection.source_macro end |
#source_options ⇒ Object
440 441 442 |
# File 'lib/datastax_rails/reflection.rb', line 440 def source_reflection. end |
#source_reflection ⇒ Object
Gets the source of the through reflection. It checks both a singularized and pluralized form for :belongs_to
or :has_many
.
class Post < DatastaxRails::Base
has_many :taggings
has_many :tags, :through => :taggings
end
325 326 327 328 329 |
# File 'lib/datastax_rails/reflection.rb', line 325 def source_reflection @source_reflection ||= source_reflection_names.map do |name| through_reflection.klass.reflect_on_association(name) end.compact.first end |
#source_reflection_names ⇒ Object
Gets an array of possible :through
source reflection names:
[:singularized, :pluralized]
435 436 437 438 |
# File 'lib/datastax_rails/reflection.rb', line 435 def source_reflection_names @source_reflection_names ||= ([:source] ? [[:source]] : [name.to_s.singularize, name]).map { |n| n.to_sym } end |
#through_options ⇒ Object
444 445 446 |
# File 'lib/datastax_rails/reflection.rb', line 444 def through_reflection. end |
#through_reflection ⇒ Object
Returns the AssociationReflection object specified in the :through
option of a HasManyThrough or HasOneThrough association.
class Post < DatastaxRails::Base
has_many :taggings
has_many :tags, :through => :taggings
end
= Post.reflect_on_association(:tags)
taggings_reflection = .through_reflection
342 343 344 |
# File 'lib/datastax_rails/reflection.rb', line 342 def through_reflection @through_reflection ||= datastax_rails.reflect_on_association([:through]) end |