From 3258ac22e99c7e3e90fc9623c5d0193e24f04f80 Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Tue, 19 Jul 2011 21:28:44 +0200 Subject: [PATCH] updating to couchrest 1.1.2 and as_couch_json method --- couchrest_model.gemspec | 2 +- history.md | 6 ++++++ lib/couchrest/model/properties.rb | 6 ++++-- spec/unit/property_spec.rb | 14 +++++++++++--- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/couchrest_model.gemspec b/couchrest_model.gemspec index 8e7e315..4c873e8 100644 --- a/couchrest_model.gemspec +++ b/couchrest_model.gemspec @@ -23,7 +23,7 @@ Gem::Specification.new do |s| s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] - s.add_dependency(%q, "1.1.1") + s.add_dependency(%q, "~> 1.1.2") s.add_dependency(%q, "~> 1.15") s.add_dependency(%q, "~> 3.0") s.add_dependency(%q, "~> 0.3.22") diff --git a/history.md b/history.md index e1c8329..387f171 100644 --- a/history.md +++ b/history.md @@ -1,5 +1,11 @@ # CouchRest Model Change History +## 1.1.2 - 2011-07-XX + +* Minor fixes + * Upgrade to couchrest 1.1.2 + * Override as_couch_json to ensure nil values not stored + ## 1.1.1 - 2011-07-04 * Minor fix diff --git a/lib/couchrest/model/properties.rb b/lib/couchrest/model/properties.rb index 7b2c494..54cfb71 100644 --- a/lib/couchrest/model/properties.rb +++ b/lib/couchrest/model/properties.rb @@ -12,8 +12,10 @@ module CouchRest raise "You can only mixin Properties in a class responding to [] and []=, if you tried to mixin CastedModel, make sure your class inherits from Hash or responds to the proper methods" unless (method_defined?(:[]) && method_defined?(:[]=)) end - def as_json(options = nil) - Hash[self].reject{|k,v| v.nil?}.as_json(options) + # Provide an attribute hash ready to be sent to CouchDB but with + # all the nil attributes removed. + def as_couch_json + super.delete_if{|k,v| v.nil?} end # Returns the Class properties with their values diff --git a/spec/unit/property_spec.rb b/spec/unit/property_spec.rb index b799908..790beb4 100644 --- a/spec/unit/property_spec.rb +++ b/spec/unit/property_spec.rb @@ -62,15 +62,23 @@ describe CouchRest::Model::Property do @card.updated_at.should_not be_nil end - describe "#as_json" do + describe "#as_couch_json" do it "should provide a simple hash from model" do - @card.as_json.class.should eql(Hash) + @card.as_couch_json.class.should eql(Hash) end it "should remove properties from Hash if value is nil" do @card.last_name = nil - @card.as_json.keys.include?('last_name').should be_false + @card.as_couch_json.keys.include?('last_name').should be_false + end + + end + + describe "#as_json" do + + it "should provide a simple hash from model" do + @card.as_json.class.should eql(Hash) end it "should pass options to Active Support's as_json" do