Skip to content

Commit

Permalink
Tweaked basic association collections
Browse files Browse the repository at this point in the history
  • Loading branch information
chicks committed Dec 28, 2010
1 parent 32defb1 commit 4da3fd3
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 20 deletions.
26 changes: 22 additions & 4 deletions lib/sugarcrm/associations/association_collection.rb
Expand Up @@ -15,6 +15,11 @@ def initialize(parent, array)
def each(&block)
@collection.each(&block)
end

# we should probably delegate this
def length
@collection.length
end

# return any added elements
def added
Expand All @@ -28,8 +33,11 @@ def removed

# Removes an object from the collection, uses the id of the object as a test for inclusion.
def delete(object)
raise InvalidRecord, "#{object.class} does not have a valid :id!"
puts "Attempting to delete #{object._id}"
@collection.each do |record|
if record._id == object._id
puts "Found object to delete"
@collection.delete(record)
return true
end
Expand All @@ -47,19 +55,30 @@ def include?(object)

# Add +records+ to this association. Returns +self+ so method calls may be chained.
def <<(object)
return false if include?(object)
result = true
result = false if include?(object)
@collection << object
true
result && self
end
alias :add :<<

def save
begin
save!
rescue
return false
end
end

def save!
added.each do |record|
associate!(record)
end
removed.each do |record|
disassociate!(record)
end
@original = @collection
@original.freeze
true
end

Expand All @@ -71,11 +90,10 @@ def save
# i.e. user.email_addresses.associate!(EmailAddress.new(:email_address => "abc@abc.com"))
# user would be the parent, and EmailAddress.new() is the target
def associate!(target, opts={})
puts "Saving before associate!"
target.save! if target.new?
response = SugarCRM.connection.set_relationship(
@parent.class._module.name, @parent._id,
target.class._module.name, target._id,
target.class._module.name.downcase, [target._id],
opts
)
raise AssociationFailed,
Expand Down
4 changes: 2 additions & 2 deletions lib/sugarcrm/associations/association_methods.rb
Expand Up @@ -42,6 +42,8 @@ def load_associations_for(association)
SugarCRM.connection.get_relationships(self.class._module.name, self._id, association.to_s)
end

# Returns the records from the associated module or returns the cached copy if we've already
# loaded it. Force a reload of the records with reload=true
#
# {"email_addresses"=>
# {"name"=>"email_addresses",
Expand All @@ -50,8 +52,6 @@ def load_associations_for(association)
# "relationship"=>"users_email_addresses",
# "type"=>"link"},
#
# Returns the records from the associated module or returns the cached copy if we've already
# loaded it. Force a reload of the records with reload=true
def query_association(association, reload=false)
return @association_cache[association] if association_cached?(association) && !reload
# TODO: Some relationships aren't fetchable via get_relationship (i.e users.contacts)
Expand Down
4 changes: 0 additions & 4 deletions lib/sugarcrm/attributes/attribute_typecast.rb
Expand Up @@ -7,10 +7,6 @@ def attr_type_for(attribute)
# sometimes the module fields aren't loaded. Why?
fields = self.class._module.fields
field = fields[attribute]
if attribute.to_sym == :system_generated_password
puts "system_generated_password class: #{field.class}"
pp field
end
raise UninitializedModule, "SugarCRM::Module #{self.class._module.name} was not initialized properly (fields.length == 0)" if fields.length == 0
raise InvalidAttribute, "#{self.class}_module.fields does not contain an entry for #{attribute} (of type: #{attribute.class})\nValid fields: #{self.class._module.fields.keys.sort.join(", ")}" if field.nil?
raise InvalidAttributeType, "#{self.class}._module.fields[#{attribute}] does not have a key for \'type\'" if field["type"].nil?
Expand Down
8 changes: 7 additions & 1 deletion lib/sugarcrm/base.rb
Expand Up @@ -150,7 +150,6 @@ def find_every(options)
end

def find_by_sql(options)
pp options
query = query_from_options(options)
SugarCRM.connection.get_entry_list(self._module.name, query, options)
end
Expand Down Expand Up @@ -382,6 +381,13 @@ def delete
(SugarCRM.connection.set_entry(self.class._module.name, params).class == Hash)
end

# Delegates to id in order to allow two records of the same type and id to work with something like:
# [ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
def hash
@id.hash
end


# Wrapper around class attribute
def attribute_methods_generated?
self.class.attribute_methods_generated
Expand Down
9 changes: 2 additions & 7 deletions test/connection/test_set_relationship.rb
Expand Up @@ -5,20 +5,15 @@ class TestSetRelationship < Test::Unit::TestCase
should "add and remove a relationship when #set_relationship" do
SugarCRM.connect(URL, USER, PASS, {:debug => false})
meeting = SugarCRM::Meeting.new
SugarCRM.connection.debug = true
meeting.date_start = DateTime.now
meeting.duration_hours = 0.5
meeting.name = "Stupid Meeting"
assert meeting.save!
response = SugarCRM.connection.set_relationship("Users","1","Meetings", [meeting._id])
puts "relating: User 1 -> Meetings #{meeting._id}"
response = SugarCRM.connection.set_relationship("Users","1","meetings", [meeting._id])
assert response["created"] == 1
response = SugarCRM.connection.set_relationship("Users","1","Meetings", [meeting._id], {:delete => 1})
puts "unrelating"
response = SugarCRM.connection.set_relationship("Users","1","meetings", [meeting._id], {:delete => 1})
assert response["deleted"] == 1
puts "deleting"
assert meeting.delete
SugarCRM.connection.debug = false
end
end
end
2 changes: 1 addition & 1 deletion test/test_association_collection.rb
Expand Up @@ -2,7 +2,7 @@

class TestAssociationCollection < Test::Unit::TestCase
context "A SugarCRM::AssociationCollection instance" do
should "create a new instance when #new()" do
should "create a new instance when #new" do
SugarCRM.connect!(URL, USER, PASS)
u = SugarCRM::User.find("seed_sarah_id")
c = SugarCRM::Contact.find_by_assigned_user_id("seed_sarah_id")
Expand Down
3 changes: 2 additions & 1 deletion test/test_sugarcrm.rb
Expand Up @@ -116,9 +116,10 @@ class TestSugarCRM < Test::Unit::TestCase
e.email_address = "admin@gmail.com"
e.email_address_caps = "ADMIN@GMAIL.COM"
u.email_addresses << e
puts "Saving Association Collection"
assert u.email_addresses.include?(e)
assert u.save!
u = SugarCRM::User.find(1)
assert u.email_addresses.include?(e)
assert u.email_addresses.delete(e)
end

Expand Down

0 comments on commit 4da3fd3

Please sign in to comment.