Class: Tenacity::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/tenacity/association.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, name, source, options = {}) ⇒ Association

Returns a new instance of Association.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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

Instance Attribute Details

#asObject (readonly)

The interface this association is reffered to as



33
34
35
# File 'lib/tenacity/association.rb', line 33

def as
  @as
end

#autosaveObject (readonly)

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



30
31
32
# File 'lib/tenacity/association.rb', line 30

def autosave
  @autosave
end

#class_nameObject (readonly)

The name of the associated class



15
16
17
# File 'lib/tenacity/association.rb', line 15

def class_name
  @class_name
end

#dependentObject (readonly)

What happens to the associated object when the object is deleted



18
19
20
# File 'lib/tenacity/association.rb', line 18

def dependent
  @dependent
end

#disable_foreign_key_constraintsObject (readonly)

Should this association disable foreign key like constraints



39
40
41
# File 'lib/tenacity/association.rb', line 39

def disable_foreign_key_constraints
  @disable_foreign_key_constraints
end

#limitObject (readonly)

The limit on the number of results to be returned.



24
25
26
# File 'lib/tenacity/association.rb', line 24

def limit
  @limit
end

#offsetObject (readonly)

The offset from where the results should be fetched.



27
28
29
# File 'lib/tenacity/association.rb', line 27

def offset
  @offset
end

#polymorphicObject (readonly)

Is this association a polymorphic association?



36
37
38
# File 'lib/tenacity/association.rb', line 36

def polymorphic
  @polymorphic
end

#readonlyObject (readonly)

Are the associated objects read only?



21
22
23
# File 'lib/tenacity/association.rb', line 21

def readonly
  @readonly
end

#sourceObject (readonly)

The class defining the association



12
13
14
# File 'lib/tenacity/association.rb', line 12

def source
  @source
end

#typeObject (readonly)

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



9
10
11
# File 'lib/tenacity/association.rb', line 9

def type
  @type
end

Instance Method Details

#associate_class(object = nil) ⇒ Object

Get the associated class



69
70
71
72
73
74
75
# 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) ⇒ Object

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.



80
81
82
83
84
85
86
87
88
# 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?Boolean

Are foreign key constraints enabled for this association?

Returns:

  • (Boolean)


106
107
108
# File 'lib/tenacity/association.rb', line 106

def foreign_key_constraints_enabled?
  @disable_foreign_key_constraints != true
end

#nameObject

The name of the association



64
65
66
# File 'lib/tenacity/association.rb', line 64

def name
  @as.nil? ? @name : @as
end

#polymorphic?Boolean

Is this association a polymorphic association?

Returns:

  • (Boolean)


96
97
98
# File 'lib/tenacity/association.rb', line 96

def polymorphic?
  @polymorphic == true || !@as.nil?
end

#polymorphic_typeObject

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



101
102
103
# File 'lib/tenacity/association.rb', line 101

def polymorphic_type
  (name.to_s + "_type").to_sym
end

#readonly?Boolean

Are the associated objects read only?

Returns:

  • (Boolean)


91
92
93
# File 'lib/tenacity/association.rb', line 91

def readonly?
  @readonly == true
end