Skip to content

Commit

Permalink
only build immediate children as associations, multiple levels of nes…
Browse files Browse the repository at this point in the history
…ting
  • Loading branch information
zackchandler committed May 20, 2008
1 parent 662fa7f commit 40781be
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/acts_as_quickbooks_model.rb
Expand Up @@ -31,13 +31,14 @@ def qbxml=(xml)
has_many_associations.each do |k,v|
association_model = Inflector.camelize(Inflector.singularize(k))
element = association_model.to_s + 'Ret'
node.search(element).each do |association_node|
node.search("> #{element}").each do |association_node|
self.send(k).send(:build, :qbxml => association_node)
end
end
end

private

def hpricot_fetch(node, path)
element = node/path
return nil if element.nil? || element.empty?
Expand Down
14 changes: 14 additions & 0 deletions test/model_test.rb
Expand Up @@ -11,10 +11,12 @@ class Invoice < ActiveRecord::Base
class InvoiceLine < ActiveRecord::Base
acts_as_quickbooks_model
belongs_to :invoice
belongs_to :invoice_line_group
end
class InvoiceLineGroup < ActiveRecord::Base
acts_as_quickbooks_model
belongs_to :invoice
has_many :invoice_lines
end

CUSTOMER_RET = <<-XML
Expand Down Expand Up @@ -47,6 +49,12 @@ class InvoiceLineGroup < ActiveRecord::Base
<ItemGroupRef>
<ListID>987</ListID>
</ItemGroupRef>
<InvoiceLineRet>
<TxnLineID>345</TxnLineID>
<ItemRef>
<ListID>789</ListID>
</ItemRef>
</InvoiceLineRet>
</InvoiceLineGroupRet>
</InvoiceRet>
XML
Expand Down Expand Up @@ -74,9 +82,15 @@ class InvoiceLineGroup < ActiveRecord::Base
invoice.invoice_lines[1].txn_line_id.should.equal '012'
invoice.invoice_lines[1].item_ref_list_id.should.equal '567'

# invoice_line_groups
invoice.invoice_line_groups.count.should.equal 1
invoice.invoice_line_groups[0].invoice_id.should.equal invoice.id
invoice.invoice_line_groups[0].txn_line_id.should.equal '321'
invoice.invoice_line_groups[0].item_group_ref_list_id.should.equal '987'

# invoice_line_group invoice_lines
invoice.invoice_line_groups[0].invoice_lines.count.should.equal 1
invoice.invoice_line_groups[0].invoice_lines[0].txn_line_id.should.equal '345'
invoice.invoice_line_groups[0].invoice_lines[0].item_ref_list_id.should.equal '789'
end
end
1 change: 1 addition & 0 deletions test/schema.rb
Expand Up @@ -15,6 +15,7 @@
create_table :invoice_lines, :force => true do |t|
t.integer :id
t.integer :invoice_id
t.integer :invoice_line_group_id
t.string :txn_line_id
t.string :item_ref_list_id
end
Expand Down

0 comments on commit 40781be

Please sign in to comment.