class Tenacity::Association

The Associaiton class represents a Tenacity association. Using this class, you can retrieve all sorts of information about the association, including it name, type, source, target class, etc.

Attributes

as[R]

The interface this association is reffered to as

autosave[R]

Should the associated object be saved when the parent object is saved?

class_name[R]

The name of the associated class

dependent[R]

What happens to the associated object when the object is deleted

disable_foreign_key_constraints[R]

Should this association disable foreign key like constraints

limit[R]

The limit on the number of results to be returned.

offset[R]

The offset from where the results should be fetched.

polymorphic[R]

Is this association a polymorphic association?

readonly[R]

Are the associated objects read only?

source[R]

The class defining the association

type[R]

Type type of the association (:t_has_one, :t_has_many, or :t_belongs_to)

Public Class Methods

new(type, name, source, options={}) click to toggle source
# File lib/tenacity/association.rb, line 41
def initialize(type, name, source, options={})
  @type = type
  @name = name
  @source = source

  if options[:class_name]
    @class_name = options[:class_name]
  else
    @class_name = name.to_s.singularize.camelcase
  end

  @foreign_key = options[:foreign_key]
  @dependent = options[:dependent]
  @readonly = options[:readonly]
  @limit = options[:limit]
  @offset = options[:offset]
  @autosave = options[:autosave]
  @polymorphic = options[:polymorphic]
  @as = options[:as]
  @disable_foreign_key_constraints = options[:disable_foreign_key_constraints]
end

Public Instance Methods

associate_class(object=nil) click to toggle source

Get the associated class

# File lib/tenacity/association.rb, line 69
def associate_class(object=nil)
  if @type == :t_belongs_to && polymorphic?
    qualified_const_get(object.send(polymorphic_type))
  else
    @clazz ||= qualified_const_get(@class_name)
  end
end
foreign_key(clazz=nil) click to toggle source

Get the foreign key used by this association. t_has_one and t_has_many associations need the class of the associated object to be specified in order to properly determine the name of the foreign key.

# File lib/tenacity/association.rb, line 80
def foreign_key(clazz=nil)
  @foreign_key || begin
    if @type == :t_belongs_to
      belongs_to_foreign_key
    elsif @type == :t_has_one || @type == :t_has_many
      has_x_foreign_key(clazz)
    end
  end
end
foreign_key_constraints_enabled?() click to toggle source

Are foreign key constraints enabled for this association?

# File lib/tenacity/association.rb, line 106
def foreign_key_constraints_enabled?
  @disable_foreign_key_constraints != true
end
name() click to toggle source

The name of the association

# File lib/tenacity/association.rb, line 64
def name
  @as.nil? ? @name : @as
end
polymorphic?() click to toggle source

Is this association a polymorphic association?

# File lib/tenacity/association.rb, line 96
def polymorphic?
  @polymorphic == true || !@as.nil?
end
polymorphic_type() click to toggle source

The name of the property that stores the polymorphic type (for polymorphic associations)

# File lib/tenacity/association.rb, line 101
def polymorphic_type
  (name.to_s + "_type").to_sym
end
readonly?() click to toggle source

Are the associated objects read only?

# File lib/tenacity/association.rb, line 91
def readonly?
  @readonly == true
end

Private Instance Methods

belongs_to_foreign_key() click to toggle source
# File lib/tenacity/association.rb, line 140
def belongs_to_foreign_key
  if polymorphic?
    (name.to_s + "_id").to_sym
  else
    unqualified_class_name(@class_name).underscore + "_id"
  end
end
has_x_foreign_key(clazz) click to toggle source
# File lib/tenacity/association.rb, line 148
def has_x_foreign_key(clazz)
  raise "The class of the associate must be provided in order to determine the name of the foreign key" if clazz.nil?
  if polymorphic?
    (@as.to_s + "_id").to_sym
  else
    "#{ActiveSupport::Inflector.underscore(unqualified_class_name(clazz))}_id"
  end
end
qualified_const_get(clazz) click to toggle source

Shamelessly copied from redcorundum.blogspot.com/2006/05/kernelqualifiedconstget.html

# File lib/tenacity/association.rb, line 113
def qualified_const_get(clazz)
  path = clazz.to_s.split('::')
  from_root = path[0].empty?

  if from_root
    from_root = []
    path = path[1..-1]
  else
    start_ns = ((Class === self) || (Module === self)) ? self : self.class
    from_root = start_ns.to_s.split('::')
  end

  until from_root.empty?
    begin
      return (from_root + path).inject(Object) { |ns,name| ns.const_get(name) }
    rescue NameError
      from_root.delete_at(-1)
    end
  end

  path.inject(Object) { |ns,name| ns.const_get(name) }
end
unqualified_class_name(clazz) click to toggle source
# File lib/tenacity/association.rb, line 136
def unqualified_class_name(clazz)
  clazz.to_s.split('::').last
end