Class: DatastaxRails::Reflection::AssociationReflection

Inherits:
MacroReflection show all
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

ThroughReflection

Instance Attribute Summary collapse

Attributes inherited from MacroReflection

#datastax_rails, #denorms, #macro, #name, #options, #plural_name

Instance Method Summary collapse

Methods inherited from MacroReflection

#==, #class_name, #sanitized_conditions

Constructor Details

#initialize(macro, name, options, datastax_rails) ⇒ AssociationReflection

Returns a new instance of AssociationReflection.



156
157
158
159
# File 'lib/datastax_rails/reflection.rb', line 156

def initialize(macro, name, options, datastax_rails)
  super
  @collection = macro.in?([:has_many, :has_and_belongs_to_many])
end

Instance Attribute Details

#collectionObject (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.



138
139
140
# File 'lib/datastax_rails/reflection.rb', line 138

def collection
  @collection
end

Instance Method Details

#association_classObject



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/datastax_rails/reflection.rb', line 262

def association_class
  case macro
  when :belongs_to
    Associations::BelongsToAssociation
  when :has_and_belongs_to_many
    Associations::HasAndBelongsToManyAssociation
  when :has_many
    if options[:through]
      Associations::HasManyThroughAssociation
    else
      Associations::HasManyAssociation
    end
  when :has_one
    if options[:through]
      Associations::HasOneThroughAssociation
    else
      Associations::HasOneAssociation
    end
  end
end

#association_foreign_keyObject



187
188
189
# File 'lib/datastax_rails/reflection.rb', line 187

def association_foreign_key
  @association_foreign_key ||= options[:association_foreign_key] || class_name.foreign_key
end

#belongs_to?Boolean

Returns true if self is a belongs_to reflection.

Returns:

  • (Boolean)


258
259
260
# File 'lib/datastax_rails/reflection.rb', line 258

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.



163
164
165
# File 'lib/datastax_rails/reflection.rb', line 163

def build_association(*options, &block)
  klass.new(*options, &block)
end

#chainObject

A chain of reflections from this one back to the owner. For more see the explanation in ThroughReflection.



221
222
223
# File 'lib/datastax_rails/reflection.rb', line 221

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



200
201
202
# File 'lib/datastax_rails/reflection.rb', line 200

def check_validity!
  check_validity_of_inverse!
end

#check_validity_of_inverse!Object



204
205
206
207
208
209
# File 'lib/datastax_rails/reflection.rb', line 204

def check_validity_of_inverse!
  return if options[:polymorphic]
  if inverse? && inverse_of.nil?
    fail InverseOfAssociationNotFoundError.new(self)
  end
end

#column_familyObject



167
168
169
# File 'lib/datastax_rails/reflection.rb', line 167

def column_family
  @column_family ||= klass.column_family
end

#conditionsObject

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



228
229
230
# File 'lib/datastax_rails/reflection.rb', line 228

def conditions
  [[options[:conditions]].compact]
end

#foreign_keyObject



175
176
177
# File 'lib/datastax_rails/reflection.rb', line 175

def foreign_key
  @foreign_key ||= options[:foreign_key] || derive_foreign_key
end

#foreign_typeObject



179
180
181
# File 'lib/datastax_rails/reflection.rb', line 179

def foreign_type
  @foreign_type ||= options[:foreign_type] || "#{name}_type"
end

#inverse?Boolean

Returns:

  • (Boolean)


234
235
236
# File 'lib/datastax_rails/reflection.rb', line 234

def inverse?
  @options[:inverse_of]
end

#inverse_ofObject



238
239
240
241
242
# File 'lib/datastax_rails/reflection.rb', line 238

def inverse_of
  if inverse?
    @inverse_of ||= klass.reflect_on_association(options[:inverse_of])
  end
end

#klassObject

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.



152
153
154
# File 'lib/datastax_rails/reflection.rb', line 152

def klass
  @klass ||= datastax_rails.send(:compute_type, class_name)
end

#quoted_column_familyObject



171
172
173
# File 'lib/datastax_rails/reflection.rb', line 171

def quoted_column_family
  column_family
end

#source_reflectionObject



215
216
217
# File 'lib/datastax_rails/reflection.rb', line 215

def source_reflection
  nil
end

#through_reflectionObject



211
212
213
# File 'lib/datastax_rails/reflection.rb', line 211

def through_reflection
  nil
end

#typeObject



183
184
185
# File 'lib/datastax_rails/reflection.rb', line 183

def type
  @type ||= options[:as] && "#{options[: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

Returns:

  • (Boolean)


253
254
255
# File 'lib/datastax_rails/reflection.rb', line 253

def validate?
  !options[:validate].nil? ? options[:validate] : (options[:autosave] == true || macro == :has_many)
end