Class: CapsuleCRM::Associations::BelongsToAssociation
- Inherits:
-
Object
- Object
- CapsuleCRM::Associations::BelongsToAssociation
- Defined in:
- lib/capsule_crm/associations/belongs_to_association.rb
Instance Attribute Summary collapse
-
#association_name ⇒ Object
readonly
Returns the value of attribute association_name.
-
#defined_on ⇒ Object
readonly
Returns the value of attribute defined_on.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #check_object!(object) ⇒ Object
-
#foreign_key ⇒ Object
Public: Build the foreign key column name.
-
#initialize(association_name, defined_on, options) ⇒ BelongsToAssociation
constructor
Public: Initialize a new CapsuleCRM::Associations::BelongsToAssociation.
- #inverse ⇒ Object
-
#macro ⇒ Object
Public: The type of association.
-
#parent(object) ⇒ Object
Public: Find the parent object of the supplied object.
-
#serializable_key ⇒ Object
Public: The key to use when serializing this association.
- #serialize ⇒ Object
Constructor Details
#initialize(association_name, defined_on, options) ⇒ BelongsToAssociation
Public: Initialize a new CapsuleCRM::Associations::BelongsToAssociation
association_name - The Symbol name of the association defined_on - The String name of the class that this association is defined on options - The Hash of association options
foreign_key - The String foreign_key column name
class_name - The String name of the parent class
Examples
CapsuleCRM::Associations::BelongsToAssociation.new(
:person, 'CapsuleCRM::Opportunity', class_name: 'CapsuleCRM::Person'
)
Returns a CapsuleCRM::Associations::BelongsToAssociation
22 23 24 25 26 |
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 22 def initialize(association_name, defined_on, ) @association_name = association_name @defined_on = defined_on @options = end |
Instance Attribute Details
#association_name ⇒ Object (readonly)
Returns the value of attribute association_name.
4 5 6 |
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 4 def association_name @association_name end |
#defined_on ⇒ Object (readonly)
Returns the value of attribute defined_on.
4 5 6 |
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 4 def defined_on @defined_on end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 4 def @options end |
Instance Method Details
#check_object!(object) ⇒ Object
81 82 83 |
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 81 def check_object!(object) association_mismatch!(object) if object_invalid?(object) end |
#foreign_key ⇒ Object
Public: Build the foreign key column name. If a foreign key name was supplied in the options during initialization, then that is returned. Otherwise it is inferred from the association name
Returns a String foreign key name
41 42 43 |
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 41 def foreign_key @foreign_key ||= [:foreign_key] || infer_foreign_key end |
#inverse ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 28 def inverse @inverse ||= target_klass.has_many_associations.find do |name, association| association.source == association_name && association.target_klass == defined_on end.try(:last) if target_klass.respond_to?(:has_many_associations) end |
#macro ⇒ Object
Public: The type of association. Just a convenience method
Returns a Symbol :belongs_to
66 67 68 |
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 66 def macro :belongs_to end |
#parent(object) ⇒ Object
Public: Find the parent object of the supplied object
object - The object to find the parent for
Examples
association = CapsuleCRM::Associations::BelongsToAssociation.new(
:person, 'CapsuleCRM::Opportunity', class_name: 'CapsuleCRM::Person'
) object = CapsuleCRM::Opportunity.first association.parent(object)
Returns an Object that is on the parent side of the belongs to association
59 60 61 |
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 59 def parent(object) target_klass.find(object.send(foreign_key)) end |
#serializable_key ⇒ Object
Public: The key to use when serializing this association
Returns the String key
73 74 75 |
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 73 def serializable_key @serializable_key ||= [:serializable_key] || foreign_key end |
#serialize ⇒ Object
77 78 79 |
# File 'lib/capsule_crm/associations/belongs_to_association.rb', line 77 def serialize @serialize ||= [:serialize] == false ? false : true end |