Class: Tenacity::Association
- Inherits:
-
Object
- Object
- Tenacity::Association
- 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
-
#as ⇒ Object
readonly
The interface this association is reffered to as.
-
#autosave ⇒ Object
readonly
Should the associated object be saved when the parent object is saved?.
-
#class_name ⇒ Object
readonly
The name of the associated class.
-
#dependent ⇒ Object
readonly
What happens to the associated object when the object is deleted.
-
#disable_foreign_key_constraints ⇒ Object
readonly
Should this association disable foreign key like constraints.
-
#limit ⇒ Object
readonly
The limit on the number of results to be returned.
-
#offset ⇒ Object
readonly
The offset from where the results should be fetched.
-
#polymorphic ⇒ Object
readonly
Is this association a polymorphic association?.
-
#readonly ⇒ Object
readonly
Are the associated objects read only?.
-
#source ⇒ Object
readonly
The class defining the association.
-
#type ⇒ Object
readonly
Type type of the association (
:t_has_one,:t_has_many, or:t_belongs_to).
Instance Method Summary collapse
-
#associate_class(object = nil) ⇒ Object
Get the associated class.
-
#foreign_key(clazz = nil) ⇒ Object
Get the foreign key used by this association.
-
#foreign_key_constraints_enabled? ⇒ Boolean
Are foreign key constraints enabled for this association?.
-
#initialize(type, name, source, options = {}) ⇒ Association
constructor
A new instance of Association.
-
#name ⇒ Object
The name of the association.
-
#polymorphic? ⇒ Boolean
Is this association a polymorphic association?.
-
#polymorphic_type ⇒ Object
The name of the property that stores the polymorphic type (for polymorphic associations).
-
#readonly? ⇒ Boolean
Are the associated objects read only?.
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, ={}) @type = type @name = name @source = source if [:class_name] @class_name = [:class_name] else @class_name = name.to_s.singularize.camelcase end @foreign_key = [:foreign_key] @dependent = [:dependent] @readonly = [:readonly] @limit = [:limit] @offset = [:offset] @autosave = [:autosave] @polymorphic = [:polymorphic] @as = [:as] @disable_foreign_key_constraints = [:disable_foreign_key_constraints] end |
Instance Attribute Details
#as ⇒ Object (readonly)
The interface this association is reffered to as
33 34 35 |
# File 'lib/tenacity/association.rb', line 33 def as @as end |
#autosave ⇒ Object (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_name ⇒ Object (readonly)
The name of the associated class
15 16 17 |
# File 'lib/tenacity/association.rb', line 15 def class_name @class_name end |
#dependent ⇒ Object (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_constraints ⇒ Object (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 |
#limit ⇒ Object (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 |
#offset ⇒ Object (readonly)
The offset from where the results should be fetched.
27 28 29 |
# File 'lib/tenacity/association.rb', line 27 def offset @offset end |
#polymorphic ⇒ Object (readonly)
Is this association a polymorphic association?
36 37 38 |
# File 'lib/tenacity/association.rb', line 36 def polymorphic @polymorphic end |
#readonly ⇒ Object (readonly)
Are the associated objects read only?
21 22 23 |
# File 'lib/tenacity/association.rb', line 21 def readonly @readonly end |
#source ⇒ Object (readonly)
The class defining the association
12 13 14 |
# File 'lib/tenacity/association.rb', line 12 def source @source end |
#type ⇒ Object (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?
106 107 108 |
# File 'lib/tenacity/association.rb', line 106 def foreign_key_constraints_enabled? @disable_foreign_key_constraints != true end |
#name ⇒ Object
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?
96 97 98 |
# File 'lib/tenacity/association.rb', line 96 def polymorphic? @polymorphic == true || !@as.nil? end |
#polymorphic_type ⇒ Object
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?
91 92 93 |
# File 'lib/tenacity/association.rb', line 91 def readonly? @readonly == true end |