From 5cee1734da7725a088a7428347e2439c05ee8253 Mon Sep 17 00:00:00 2001 From: Kostiantyn Kahanskyi Date: Mon, 6 Jun 2011 12:51:02 +0200 Subject: [PATCH 1/5] Should always include docs when paginating by Model##paginate Fixes https://github.com/couchrest/couchrest_model/issues/81 --- lib/couchrest/model/collection.rb | 1 + spec/couchrest/collection_spec.rb | 13 ++++++------- 2 files changed, 7 insertions(+), 7 deletions(-) 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 From d50d47c32a5bd8a11777c37f606f7b3fe1779aed Mon Sep 17 00:00:00 2001 From: Kostiantyn Kahanskyi Date: Mon, 6 Jun 2011 18:07:33 +0200 Subject: [PATCH 2/5] Should be able to assign a casted hash to a hash property MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes https://github.com/couchrest/couchrest_model/issues/68 Signed-off-by: Marcos Tapajós --- lib/couchrest/model/property.rb | 2 +- spec/couchrest/property_spec.rb | 11 +++++++++++ spec/fixtures/more/key_chain.rb | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/more/key_chain.rb diff --git a/lib/couchrest/model/property.rb b/lib/couchrest/model/property.rb index 7661858..0ea9962 100644 --- a/lib/couchrest/model/property.rb +++ b/lib/couchrest/model/property.rb @@ -39,7 +39,7 @@ module CouchRest::Model arr = value.collect { |data| cast_value(parent, data) } # allow casted_by calls to be passed up chain by wrapping in CastedArray CastedArray.new(arr, self, parent) - elsif (type == Object || type == Hash) && (value.class == Hash) + elsif (type == Object || type == Hash) && (value.kind_of?(Hash)) # allow casted_by calls to be passed up chain by wrapping in CastedHash CastedHash[value, self, parent] elsif !value.nil? diff --git a/spec/couchrest/property_spec.rb b/spec/couchrest/property_spec.rb index bb55bfc..13ada9f 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 From f244b51fbff2e6e2b50ec0491bea9ba730f78de0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Tapaj=C3=B3s?= Date: Tue, 7 Jun 2011 20:51:37 -0300 Subject: [PATCH 3/5] Update History --- history.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/history.md b/history.md index b2df9f5..bec70d7 100644 --- a/history.md +++ b/history.md @@ -18,8 +18,8 @@ * #destroy freezes object instead of removing _id and _rev, better for callbacks (pointer by karmi) * #destroyed? method now available * #reload no longer uses Hash#merge! which was causing issues with dirty tracking on casted models. (pointer by kostia) - - + * Fixing problem assigning a CastedHash to a property declared as a Hash (Kostiantyn Kahanskyi, gfmtim) + ## 1.1.0.beta5 - 2011-04-30 * Major changes: From 0bb00860d1344280fc729ec6374047b07c178bfa Mon Sep 17 00:00:00 2001 From: Kostiantyn Kahanskyi Date: Mon, 6 Jun 2011 11:55:15 +0200 Subject: [PATCH 4/5] Casted array should notify changes on deletion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Otherwise there is no direct way to delete particular elements. Workaround with assigning to another array without those elements is pretty ugly. * Should notify on deletion at a particular index (Array#delete_at) * Should notify on deletion of a particular element (Array#delete) Signed-off-by: Marcos Tapajós --- lib/couchrest/model/casted_array.rb | 10 +++++++ spec/couchrest/dirty_spec.rb | 44 +++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) 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/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") From d99150547c879da89254a583c6637c634f2492a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Tapaj=C3=B3s?= Date: Tue, 7 Jun 2011 21:00:41 -0300 Subject: [PATCH 5/5] History update --- history.md | 1 + 1 file changed, 1 insertion(+) diff --git a/history.md b/history.md index bec70d7..40e594f 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.