-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48cbe6b
commit d4c7c1d
Showing
16 changed files
with
392 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module CapsuleCRM | ||
module Associations | ||
class BelongsToAssociation | ||
attr_reader :association_name, :defined_on, :options | ||
|
||
# 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 | ||
def initialize(association_name, defined_on, options) | ||
@association_name = association_name | ||
@defined_on = defined_on | ||
@options = options | ||
end | ||
|
||
# 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 | ||
def foreign_key | ||
@foreign_key ||= options[:foreign_key] || "#{association_name}_id" | ||
end | ||
|
||
# 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 | ||
def parent(object) | ||
target_klass.find(object.send(foreign_key)) | ||
end | ||
|
||
# Public: The type of association. Just a convenience method | ||
# | ||
# Returns a Symbol :belongs_to | ||
def macro | ||
:belongs_to | ||
end | ||
|
||
private | ||
|
||
def target_klass | ||
@target_klass ||= options[:class_name].constantize | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
module CapsuleCRM | ||
module Associations | ||
class HasManyAssociation | ||
attr_reader :association_name, :options, :defined_on | ||
|
||
# Public: Initialize a new CapsuleCRM::Associations::HasManyAssociation | ||
# | ||
# association_name - The Symbox association name | ||
# defined_on - The String name of the class that this association | ||
# is defined on | ||
# options - The Hash of association options | ||
# :class_name - The String name of the belongs to | ||
# class | ||
# :source - The Symbol name of the accessor method on | ||
# the belongs to class | ||
# | ||
# Examples | ||
# | ||
# CapsuleCRM::Associations::HasManyAssociation.new( | ||
# :opportunities, CapsuleCRM::Person, class_name: | ||
# 'CapsuleCRM::Opportunity, source: :person | ||
# ) | ||
# | ||
# Returns a CapsuleCRM::Associations::HasManyAssociation | ||
def initialize(association_name, defined_on, options) | ||
@association_name = association_name | ||
@options = options | ||
@defined_on = defined_on | ||
end | ||
|
||
# Public: Build the HasManyProxy object | ||
# | ||
# parent - The instance of the class that the has many assocation is | ||
# defined on | ||
# collection - An optional Array or Hash to use as the target for the | ||
# proxy | ||
# | ||
# Returns a CapsuleCRM::Associations::HasManyProxy | ||
def proxy(parent, collection = nil) | ||
CapsuleCRM::Associations::HasManyProxy.new( | ||
parent, target_klass, build_target(parent, collection), source | ||
) | ||
end | ||
|
||
# Public: The type of association. Just a convenience method | ||
# | ||
# Return a Symbol :has_many | ||
def macro | ||
:has_many | ||
end | ||
|
||
private | ||
|
||
def build_target(parent, collection) | ||
collection.nil? ? target(parent) : collection_to_array(collection) | ||
end | ||
|
||
def collection_to_array(collection) | ||
if collection.is_a?(Hash) | ||
Array(target_klass.new(collection[collection.keys.first])) | ||
else | ||
collection | ||
end | ||
end | ||
|
||
def target_klass | ||
@target_klass ||= options[:class_name].constantize | ||
end | ||
|
||
def target(parent) | ||
target_klass. | ||
send("_for_#{parent.class.to_s.demodulize.downcase}", parent.id) | ||
end | ||
|
||
def source | ||
@source ||= options[:source] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.