diff --git a/history.md b/history.md index d8737a0..99cf842 100644 --- a/history.md +++ b/history.md @@ -6,6 +6,7 @@ * Properties with a nil value are now no longer sent to the database. * Now possible to build new objects via CastedArray#build * Implement #get! and #find! class methods + * Now is possible delete particular elements in casted array(Kostiantyn Kahanskyi) * Minor fixes * #as_json now correctly uses ActiveSupports methods. @@ -20,7 +21,7 @@ * #reload no longer uses Hash#merge! which was causing issues with dirty tracking on casted models. (pointer by kostia) * Non-property mass assignment on #new no longer possible without :directly_set_attributes option. * Using CouchRest 1.1.0.pre3. (No more Hashes!) - + * Fixing problem assigning a CastedHash to a property declared as a Hash (Kostiantyn Kahanskyi, gfmtim) ## 1.1.0.beta5 - 2011-04-30 diff --git a/lib/couchrest/model/casted_array.rb b/lib/couchrest/model/casted_array.rb index 8e4ff66..65b7f46 100644 --- a/lib/couchrest/model/casted_array.rb +++ b/lib/couchrest/model/casted_array.rb @@ -50,6 +50,16 @@ module CouchRest::Model super end + def delete(obj) + couchrest_parent_will_change! if use_dirty? && self.length > 0 + super(obj) + end + + def delete_at(index) + couchrest_parent_will_change! if use_dirty? && self.length > 0 + super(index) + end + def build(*args) obj = casted_by_property.build(*args) self.push(obj) diff --git a/lib/couchrest/model/collection.rb b/lib/couchrest/model/collection.rb index 540b299..5d6dd76 100644 --- a/lib/couchrest/model/collection.rb +++ b/lib/couchrest/model/collection.rb @@ -244,6 +244,7 @@ module CouchRest else options = { :limit => per_page, :skip => per_page * (page - 1) } end + options[:include_docs] = true view_options.merge(options) end diff --git a/spec/couchrest/collection_spec.rb b/spec/couchrest/collection_spec.rb index 528a350..63a1626 100644 --- a/spec/couchrest/collection_spec.rb +++ b/spec/couchrest/collection_spec.rb @@ -27,21 +27,20 @@ describe "Collections" do end it "should provide a class method for paginate" do articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date', - :per_page => 3, :descending => true, :key => Date.today, :include_docs => true) + :per_page => 3, :descending => true, :key => Date.today) articles.size.should == 3 - + articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date', - :per_page => 3, :page => 2, :descending => true, :key => Date.today, :include_docs => true) + :per_page => 3, :page => 2, :descending => true, :key => Date.today) articles.size.should == 3 - + articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date', - :per_page => 3, :page => 3, :descending => true, :key => Date.today, :include_docs => true) + :per_page => 3, :page => 3, :descending => true, :key => Date.today) articles.size.should == 1 end it "should provide a class method for paginated_each" do options = { :design_doc => 'Article', :view_name => 'by_date', - :per_page => 3, :page => 1, :descending => true, :key => Date.today, - :include_docs => true } + :per_page => 3, :page => 1, :descending => true, :key => Date.today } Article.paginated_each(options) do |a| a.should_not be_nil end diff --git a/spec/couchrest/dirty_spec.rb b/spec/couchrest/dirty_spec.rb index 8cbd7be..9129a23 100644 --- a/spec/couchrest/dirty_spec.rb +++ b/spec/couchrest/dirty_spec.rb @@ -257,6 +257,50 @@ describe "Dirty" do end end + it "should report changes on deletion from an array" do + should_change_array do |array, obj| + array << "keyword" + obj.save! + array.delete_at(0) + end + + should_change_array do |array, obj| + array << "keyword" + obj.save! + array.delete("keyword") + end + end + + it "should report changes on deletion from an array after reload" do + should_change_array do |array, obj| + array << "keyword" + obj.save! + obj.reload + array.delete_at(0) + end + + should_change_array do |array, obj| + array << "keyword" + obj.save! + obj.reload + array.delete("keyword") + end + end + + it "should report no changes on deletion from an empty array" do + should_not_change_array do |array, obj| + array.clear + obj.save! + array.delete_at(0) + end + + should_not_change_array do |array, obj| + array.clear + obj.save! + array.delete("keyword") + end + end + it "should report changes if an array is pushed" do should_change_array do |array, obj| array.push("keyword") diff --git a/spec/couchrest/property_spec.rb b/spec/couchrest/property_spec.rb index fd82a14..5379269 100644 --- a/spec/couchrest/property_spec.rb +++ b/spec/couchrest/property_spec.rb @@ -9,6 +9,7 @@ require File.join(FIXTURE_PATH, 'more', 'service') require File.join(FIXTURE_PATH, 'more', 'event') require File.join(FIXTURE_PATH, 'more', 'user') require File.join(FIXTURE_PATH, 'more', 'course') +require File.join(FIXTURE_PATH, "more", "key_chain") describe "Model properties" do @@ -239,6 +240,16 @@ describe "Model properties" do end +describe "properties of hash of casted models" do + it "should be able to assign a casted hash to a hash property" do + chain = KeyChain.new + keys = {"House" => "8==$", "Office" => "<>==U"} + chain.keys = keys + chain.keys = chain.keys + chain.keys.should == keys + end +end + describe "properties of array of casted models" do before(:each) do diff --git a/spec/fixtures/more/key_chain.rb b/spec/fixtures/more/key_chain.rb new file mode 100644 index 0000000..0837e39 --- /dev/null +++ b/spec/fixtures/more/key_chain.rb @@ -0,0 +1,5 @@ +class KeyChain < CouchRest::Model::Base + use_database(DB) + + property(:keys, Hash) +end