class DatastaxRails::Reflection::AssociationReflection

Holds all the meta-data about an association as it was specified in the DatastaxRails class.

Attributes

collection[R]

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.

collection?[R]

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.

Public Class Methods

new(macro, name, options, datastax_rails) click to toggle source
# File lib/datastax_rails/reflection.rb, line 157
def initialize(macro, name, options, datastax_rails)
  super
  @collection = macro.in?([:has_many, :has_and_belongs_to_many])
end

Public Instance Methods

association_class() click to toggle source
# File lib/datastax_rails/reflection.rb, line 267
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_key() click to toggle source
# File lib/datastax_rails/reflection.rb, line 192
def association_foreign_key
  @association_foreign_key ||= options[:association_foreign_key] || class_name.foreign_key
end
association_primary_key(klass = nil) click to toggle source

klass option is necessary to support loading polymorphic associations

# File lib/datastax_rails/reflection.rb, line 197
def association_primary_key(klass = nil)
  options[:primary_key] || primary_key(klass || self.klass)
end
belongs_to?() click to toggle source

Returns true if self is a belongs_to reflection.

# File lib/datastax_rails/reflection.rb, line 263
def belongs_to?
  macro == :belongs_to
end
build_association(*options, &block) click to toggle source

Returns a new, unsaved instance of the associated class. options will be passed to the class's constructor.

# File lib/datastax_rails/reflection.rb, line 164
def build_association(*options, &block)
  klass.new(*options, &block)
end
chain() click to toggle source

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

# File lib/datastax_rails/reflection.rb, line 226
def chain
  [self]
end
check_validity!() click to toggle source
# File lib/datastax_rails/reflection.rb, line 205
def check_validity!
  check_validity_of_inverse!
end
check_validity_of_inverse!() click to toggle source
# File lib/datastax_rails/reflection.rb, line 209
def check_validity_of_inverse!
  return if options[:polymorphic]
  if inverse? && inverse_of.nil?
    fail InverseOfAssociationNotFoundError.new(self)
  end
end
column_family() click to toggle source
# File lib/datastax_rails/reflection.rb, line 168
def column_family
  @column_family ||= klass.column_family
end
conditions() click to toggle source

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

# File lib/datastax_rails/reflection.rb, line 233
def conditions
  [[options[:conditions]].compact]
end
datastax_rails_primary_key() click to toggle source
# File lib/datastax_rails/reflection.rb, line 201
def datastax_rails_primary_key
  @datastax_rails_primary_key ||= options[:primary_key] || primary_key(datastax_rails)
end
foreign_key() click to toggle source
# File lib/datastax_rails/reflection.rb, line 176
def foreign_key
  @foreign_key ||= options[:foreign_key] || derive_foreign_key
end
foreign_type() click to toggle source
# File lib/datastax_rails/reflection.rb, line 180
def foreign_type
  @foreign_type ||= options[:foreign_type] || "#{name}_type"
end
inverse?() click to toggle source
# File lib/datastax_rails/reflection.rb, line 239
def inverse?
  @options[:inverse_of]
end
inverse_of() click to toggle source
# File lib/datastax_rails/reflection.rb, line 243
def inverse_of
  if inverse?
    @inverse_of ||= klass.reflect_on_association(options[:inverse_of])
  end
end
klass() click to toggle source

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.

# File lib/datastax_rails/reflection.rb, line 153
def klass
  @klass ||= datastax_rails.send(:compute_type, class_name)
end
nested?() click to toggle source
# File lib/datastax_rails/reflection.rb, line 288
def nested?
  false
end
primary_key_column() click to toggle source
# File lib/datastax_rails/reflection.rb, line 188
def primary_key_column
  @primary_key_column ||= klass.columns.find { |c| c.name == klass.primary_key }
end
quoted_column_family() click to toggle source
# File lib/datastax_rails/reflection.rb, line 172
def quoted_column_family
  column_family
end
source_reflection() click to toggle source
# File lib/datastax_rails/reflection.rb, line 220
def source_reflection
  nil
end
through_reflection() click to toggle source
# File lib/datastax_rails/reflection.rb, line 216
def through_reflection
  nil
end
type() click to toggle source
# File lib/datastax_rails/reflection.rb, line 184
def type
  @type ||= options[:as] && "#{options[:as]}_type"
end
validate?() click to toggle source

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

# File lib/datastax_rails/reflection.rb, line 258
def validate?
  !options[:validate].nil? ? options[:validate] : (options[:autosave] == true || macro == :has_many)
end

Private Instance Methods

derive_class_name() click to toggle source
# File lib/datastax_rails/reflection.rb, line 294
def derive_class_name
  class_name = name.to_s.camelize
  class_name = class_name.singularize if collection?
  class_name
end
derive_foreign_key() click to toggle source
# File lib/datastax_rails/reflection.rb, line 300
def derive_foreign_key
  if belongs_to?
    "#{name}_id"
  elsif options[:as]
    "#{options[:as]}_id"
  else
    datastax_rails.name.foreign_key
  end
end
primary_key(klass) click to toggle source
# File lib/datastax_rails/reflection.rb, line 310
def primary_key(klass)
  klass.primary_key || fail(UnknownPrimaryKey.new(klass))
end