Merge branch 'master' of git://github.com/couchrest/couchrest_model

master
Lucas Renan 2011-05-18 21:49:18 -03:00
commit a400d46333
7 changed files with 51 additions and 2 deletions

View File

@ -1,5 +1,16 @@
# CouchRest Model Change History
## 1.1.0 - 2011-05-XX
* Minor fixes
* #as_json now correctly uses ActiveSupports methods.
* nil properties are now no longer sent in the document body.
* Rails 3.1 support (Peter Williams)
* Initialization blocks when creating new models (Peter Williams)
* Removed railties dependency (DAddYE)
* DesignDoc cache refreshed if a database is deleted.
## 1.1.0.beta5 - 2011-04-30
* Major changes:

View File

@ -108,8 +108,6 @@ module CouchRest
}
end
end # module ClassMethods
end

View File

@ -12,6 +12,9 @@ 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)
end
# Returns the Class properties with their values
#

View File

@ -0,0 +1,13 @@
#
# Extend CouchRest's normal database delete! method to ensure any caches are
# also emptied. Given that this is a rare event, and the consequences are not
# very severe, we just completely empty the cache.
#
CouchRest::Database.class_eval do
def delete!
Thread.current[:couchrest_design_cache] = { }
CouchRest.delete @root
end
end

View File

@ -51,6 +51,7 @@ require "couchrest/model/designs/view"
# Monkey patches applied to couchrest
require "couchrest/model/support/couchrest_design"
require "couchrest/model/support/couchrest_database"
# Core Extensions
require "couchrest/model/core_extensions/hash"

View File

@ -155,6 +155,10 @@ describe "Design Documents" do
Article.by_date
Article.stored_design_doc['_rev'].should eql(orig)
end
it "should recreate the design doc if database deleted" do
Article.database.recreate!
lambda { Article.by_date }.should_not raise_error(RestClient::ResourceNotFound)
end
end
describe "when auto_update_design_doc false" do

View File

@ -1,5 +1,6 @@
# encoding: utf-8
require File.expand_path('../../spec_helper', __FILE__)
require File.join(FIXTURE_PATH, 'more', 'article')
require File.join(FIXTURE_PATH, 'more', 'cat')
require File.join(FIXTURE_PATH, 'more', 'person')
require File.join(FIXTURE_PATH, 'more', 'card')
@ -71,6 +72,24 @@ describe "Model properties" do
@card.updated_at.should_not be_nil
end
describe "#as_json" do
it "should provide a simple hash from model" do
@card.as_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
end
it "should pass options to Active Support's as_json" do
@card.last_name = "Aimonetti"
@card.as_json(:only => 'last_name').should eql('last_name' => 'Aimonetti')
end
end
describe '#read_attribute' do
it "should let you use read_attribute method" do
@card.last_name = "Aimonetti"