diff --git a/lib/sugarcrm/base.rb b/lib/sugarcrm/base.rb index f9eafeb..462b177 100644 --- a/lib/sugarcrm/base.rb +++ b/lib/sugarcrm/base.rb @@ -20,6 +20,7 @@ module SugarCRM; class Base # Contains a list of attributes attr :attributes, true attr :modified_attributes, true + attr :relate, true attr :associations, true attr :debug, true attr :errors, true @@ -128,6 +129,7 @@ def create(attributes = nil, &block) # Creates an instance of a Module Class, i.e. Account, User, Contact, etc. def initialize(attributes={}, &block) attributes.delete('id') + @relate = {} @errors = {} @modified_attributes = {} merge_attributes(attributes.with_indifferent_access) @@ -305,4 +307,4 @@ def superclasses include AssociationCache end -end; end \ No newline at end of file +end; end diff --git a/lib/sugarcrm/connection/response.rb b/lib/sugarcrm/connection/response.rb index 9ec8a77..53bc0d7 100644 --- a/lib/sugarcrm/connection/response.rb +++ b/lib/sugarcrm/connection/response.rb @@ -4,6 +4,7 @@ class << self # It tries to convert the response into an object such as User # or an object collection. If it fails, it just returns the response hash def handle(json, session) + #puts "returned json: #{json.inspect}" r = new(json, session) begin return r.to_obj @@ -39,8 +40,10 @@ def to_obj return @response unless @response && @response["entry_list"] objects = [] - @response["entry_list"].each do |object| + @response["entry_list"].each_index do |index| + object = @response["entry_list"][index] attributes = [] + related_attributes = {} _module = resolve_module(object) attributes = flatten_name_value_list(object) namespace = @session.namespace_const @@ -48,7 +51,19 @@ def to_obj if attributes.length == 0 raise AttributeParsingError, "response contains objects without attributes!" end - objects << namespace.const_get(_module).new(attributes) + obj = namespace.const_get(_module).new(attributes) + # Check to see if there is a relationship for this object + if @response["relationship_list"][index] + @response["relationship_list"][index].each do |list| + relate = list["name"] + related_attributes[relate] = [] + list["records"].each do |record| + related_attributes[relate] << flatten_name_value_list({ "name_value_list" => record }) + end + obj.relate = related_attributes + end + end + objects << obj else raise InvalidModule, "#{_module} does not exist, or is not accessible" end