Class: DatastaxRails::Reflection::AssociationReflection
- Inherits:
-
MacroReflection
- Object
- MacroReflection
- DatastaxRails::Reflection::AssociationReflection
- Defined in:
- lib/datastax_rails/reflection.rb
Overview
Holds all the meta-data about an association as it was specified in the DatastaxRails class.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#collection ⇒ Object
(also: #collection?)
readonly
Returns whether or not this association reflection is for a collection association.
Attributes inherited from MacroReflection
#datastax_rails, #denorms, #macro, #name, #options, #plural_name
Instance Method Summary collapse
- #association_class ⇒ Object
- #association_foreign_key ⇒ Object
-
#belongs_to? ⇒ Boolean
Returns
true
ifself
is abelongs_to
reflection. -
#build_association(*options, &block) ⇒ Object
Returns a new, unsaved instance of the associated class.
-
#chain ⇒ Object
A chain of reflections from this one back to the owner.
-
#check_validity! ⇒ Object
klass option is necessary to support loading polymorphic associations def association_primary_key(klass = nil) options || primary_key(klass || self.klass) end.
- #check_validity_of_inverse! ⇒ Object
- #column_family ⇒ Object
-
#conditions ⇒ Object
An array of arrays of conditions.
- #foreign_key ⇒ Object
- #foreign_type ⇒ Object
-
#initialize(macro, name, options, datastax_rails) ⇒ AssociationReflection
constructor
A new instance of AssociationReflection.
- #inverse? ⇒ Boolean
- #inverse_of ⇒ Object
-
#klass ⇒ Object
Returns the target association’s class.
- #nested? ⇒ Boolean
- #quoted_column_family ⇒ Object
- #source_reflection ⇒ Object
- #through_reflection ⇒ Object
- #type ⇒ Object
-
#validate? ⇒ Boolean
Returns whether or not the association should be validated as part of the parent’s validation.
Methods inherited from MacroReflection
#==, #class_name, #sanitized_conditions
Constructor Details
#initialize(macro, name, options, datastax_rails) ⇒ AssociationReflection
Returns a new instance of AssociationReflection.
157 158 159 160 |
# File 'lib/datastax_rails/reflection.rb', line 157 def initialize(macro, name, , datastax_rails) super @collection = macro.in?([:has_many, :has_and_belongs_to_many]) end |
Instance Attribute Details
#collection ⇒ Object (readonly) Also known as: collection?
Returns whether or not this association reflection is for a collection association. Returns true
if the macro
is either has_many
or has_and_belongs_to_many
, false
otherwise.
139 140 141 |
# File 'lib/datastax_rails/reflection.rb', line 139 def collection @collection end |
Instance Method Details
#association_class ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/datastax_rails/reflection.rb', line 263 def association_class case macro when :belongs_to Associations::BelongsToAssociation when :has_and_belongs_to_many Associations::HasAndBelongsToManyAssociation when :has_many if [:through] Associations::HasManyThroughAssociation else Associations::HasManyAssociation end when :has_one if [:through] Associations::HasOneThroughAssociation else Associations::HasOneAssociation end end end |
#association_foreign_key ⇒ Object
188 189 190 |
# File 'lib/datastax_rails/reflection.rb', line 188 def association_foreign_key @association_foreign_key ||= [:association_foreign_key] || class_name.foreign_key end |
#belongs_to? ⇒ Boolean
Returns true
if self
is a belongs_to
reflection.
259 260 261 |
# File 'lib/datastax_rails/reflection.rb', line 259 def belongs_to? macro == :belongs_to end |
#build_association(*options, &block) ⇒ Object
Returns a new, unsaved instance of the associated class. options
will be passed to the class’s constructor.
164 165 166 |
# File 'lib/datastax_rails/reflection.rb', line 164 def build_association(*, &block) klass.new(*, &block) end |
#chain ⇒ Object
A chain of reflections from this one back to the owner. For more see the explanation in ThroughReflection.
222 223 224 |
# File 'lib/datastax_rails/reflection.rb', line 222 def chain [self] end |
#check_validity! ⇒ Object
klass option is necessary to support loading polymorphic associations def association_primary_key(klass = nil) options || primary_key(klass || self.klass) end
def datastax_rails_primary_key end
201 202 203 |
# File 'lib/datastax_rails/reflection.rb', line 201 def check_validity! check_validity_of_inverse! end |
#check_validity_of_inverse! ⇒ Object
205 206 207 208 209 210 |
# File 'lib/datastax_rails/reflection.rb', line 205 def check_validity_of_inverse! return if [:polymorphic] if inverse? && inverse_of.nil? fail InverseOfAssociationNotFoundError.new(self) end end |
#column_family ⇒ Object
168 169 170 |
# File 'lib/datastax_rails/reflection.rb', line 168 def column_family @column_family ||= klass.column_family end |
#conditions ⇒ Object
An array of arrays of conditions. Each item in the outside array corresponds to a reflection in the #chain. The inside arrays are simply conditions (and each condition may itself be a hash, array, arel predicate, etc…)
229 230 231 |
# File 'lib/datastax_rails/reflection.rb', line 229 def conditions [[[:conditions]].compact] end |
#foreign_key ⇒ Object
176 177 178 |
# File 'lib/datastax_rails/reflection.rb', line 176 def foreign_key @foreign_key ||= [:foreign_key] || derive_foreign_key end |
#foreign_type ⇒ Object
180 181 182 |
# File 'lib/datastax_rails/reflection.rb', line 180 def foreign_type @foreign_type ||= [:foreign_type] || "#{name}_type" end |
#inverse? ⇒ Boolean
235 236 237 |
# File 'lib/datastax_rails/reflection.rb', line 235 def inverse? @options[:inverse_of] end |
#inverse_of ⇒ Object
239 240 241 242 243 |
# File 'lib/datastax_rails/reflection.rb', line 239 def inverse_of if inverse? @inverse_of ||= klass.reflect_on_association([:inverse_of]) end end |
#klass ⇒ Object
Returns the target association’s class.
class Author < DatastaxRails::Base
has_many :books
end
Author.reflect_on_association(:books).klass
# => Book
Note: Do not call klass.new
or klass.create
to instantiate a new association object. Use build_association
or create_association
instead. This allows plugins to hook into association object creation.
153 154 155 |
# File 'lib/datastax_rails/reflection.rb', line 153 def klass @klass ||= datastax_rails.send(:compute_type, class_name) end |
#nested? ⇒ Boolean
284 285 286 |
# File 'lib/datastax_rails/reflection.rb', line 284 def nested? false end |
#quoted_column_family ⇒ Object
172 173 174 |
# File 'lib/datastax_rails/reflection.rb', line 172 def quoted_column_family column_family end |
#source_reflection ⇒ Object
216 217 218 |
# File 'lib/datastax_rails/reflection.rb', line 216 def source_reflection nil end |
#through_reflection ⇒ Object
212 213 214 |
# File 'lib/datastax_rails/reflection.rb', line 212 def through_reflection nil end |
#type ⇒ Object
184 185 186 |
# File 'lib/datastax_rails/reflection.rb', line 184 def type @type ||= [:as] && "#{[:as]}_type" end |
#validate? ⇒ Boolean
Returns whether or not the association should be validated as part of the parent’s validation.
Unless you explicitly disable validation with :validate => false
, validation will take place when:
-
you explicitly enable validation;
:validate => true
-
you use autosave;
:autosave => true
-
the association is a
has_many
association
254 255 256 |
# File 'lib/datastax_rails/reflection.rb', line 254 def validate? ![:validate].nil? ? [:validate] : ([:autosave] == true || macro == :has_many) end |