diff --git a/lib/couchrest/model/associations.rb b/lib/couchrest/model/associations.rb index 6b97d6d..0f83f3e 100644 --- a/lib/couchrest/model/associations.rb +++ b/lib/couchrest/model/associations.rb @@ -2,7 +2,7 @@ module CouchRest module Model module Associations - # Basic support for relationships between ExtendedDocuments + # Basic support for relationships between CouchRest::Model::Base def self.included(base) base.extend(ClassMethods) @@ -175,14 +175,17 @@ module CouchRest end super(array) end + def << obj casted_by[property.to_s] << obj.id super(obj) end + def push(obj) casted_by[property.to_s].push obj.id super(obj) end + def unshift(obj) casted_by[property.to_s].unshift obj.id super(obj) @@ -197,6 +200,7 @@ module CouchRest casted_by[property.to_s].pop super end + def shift casted_by[property.to_s].shift super diff --git a/spec/couchrest/assocations_spec.rb b/spec/couchrest/assocations_spec.rb index 61c6966..21ad658 100644 --- a/spec/couchrest/assocations_spec.rb +++ b/spec/couchrest/assocations_spec.rb @@ -1,31 +1,6 @@ # encoding: utf-8 require File.expand_path('../../spec_helper', __FILE__) - -class Client < CouchRest::Model::Base - use_database DB - - property :name - property :tax_code -end - -class SaleEntry < CouchRest::Model::Base - use_database DB - - property :description - property :price -end - -class SaleInvoice < CouchRest::Model::Base - use_database DB - - belongs_to :client - belongs_to :alternate_client, :class_name => 'Client', :foreign_key => 'alt_client_id' - - collection_of :entries, :class_name => 'SaleEntry' - - property :date, Date - property :price, Integer -end +require File.join(FIXTURE_PATH, 'more', 'sale_invoice') describe "Assocations" do @@ -159,7 +134,7 @@ describe "Assocations" do end describe "proxy" do - + it "should ensure new entries to proxy are matched" do @invoice.entries << @entries.first @invoice.entry_ids.first.should eql(@entries.first.id) @@ -202,12 +177,17 @@ describe "Assocations" do @invoice.entries.first.should eql(@entries[1]) @invoice.entry_ids.first.should eql(@entries[1].id) end - + + it "should save all entries when invoice is saved" do + SaleEntry.find_by_description('test entry').should be_nil + entry = SaleEntry.new(:description => 'test entry', :price => 500) + @invoice.entries << entry + @invoice.save.should be_true + SaleEntry.find_by_description('test entry').should_not be_nil + end end - end -end - +end \ No newline at end of file diff --git a/spec/fixtures/more/client.rb b/spec/fixtures/more/client.rb new file mode 100644 index 0000000..820d138 --- /dev/null +++ b/spec/fixtures/more/client.rb @@ -0,0 +1,6 @@ +class Client < CouchRest::Model::Base + use_database DB + + property :name + property :tax_code +end \ No newline at end of file diff --git a/spec/fixtures/more/sale_entry.rb b/spec/fixtures/more/sale_entry.rb new file mode 100644 index 0000000..a44fc5b --- /dev/null +++ b/spec/fixtures/more/sale_entry.rb @@ -0,0 +1,9 @@ +class SaleEntry < CouchRest::Model::Base + use_database DB + + property :description + property :price + + view_by :description + +end \ No newline at end of file diff --git a/spec/fixtures/more/sale_invoice.rb b/spec/fixtures/more/sale_invoice.rb new file mode 100644 index 0000000..ad6beb2 --- /dev/null +++ b/spec/fixtures/more/sale_invoice.rb @@ -0,0 +1,13 @@ +require File.join(FIXTURE_PATH, 'more', 'client') +require File.join(FIXTURE_PATH, 'more', 'sale_entry') +class SaleInvoice < CouchRest::Model::Base + use_database DB + + belongs_to :client + belongs_to :alternate_client, :class_name => 'Client', :foreign_key => 'alt_client_id' + + collection_of :entries, :class_name => 'SaleEntry' + + property :date, Date + property :price, Integer +end \ No newline at end of file