Merge branch 'casted-extendeddocument'
* casted-extendeddocument: fixed a bug where :include_docs on a view couldn't be overwritten spec'd casted extended document
This commit is contained in:
commit
b1999ac8b7
File diff suppressed because one or more lines are too long
|
@ -27,7 +27,7 @@ require 'couchrest/monkeypatches'
|
|||
|
||||
# = CouchDB, close to the metal
|
||||
module CouchRest
|
||||
VERSION = '0.16' unless self.const_defined?("VERSION")
|
||||
VERSION = '0.17' unless self.const_defined?("VERSION")
|
||||
|
||||
autoload :Server, 'couchrest/core/server'
|
||||
autoload :Database, 'couchrest/core/database'
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
module CouchRest
|
||||
class Response < Hash
|
||||
def initialize(keys = {})
|
||||
keys.each do |k,v|
|
||||
def initialize(pkeys = {})
|
||||
pkeys ||= {}
|
||||
pkeys.each do |k,v|
|
||||
self[k.to_s] = v
|
||||
end
|
||||
end
|
||||
def []= key, value
|
||||
def []=(key, value)
|
||||
super(key.to_s, value)
|
||||
end
|
||||
def [] key
|
||||
def [](key)
|
||||
super(key.to_s)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -55,9 +55,14 @@ module CouchRest
|
|||
else
|
||||
# Let people use :send as a Time parse arg
|
||||
klass = ::CouchRest.constantize(target)
|
||||
# I'm not convince we should or should not create a new instance if we are casting a doc/extended doc without default value and nothing was passed
|
||||
# unless (property.casted &&
|
||||
# (klass.superclass == CouchRest::ExtendedDocument || klass.superclass == CouchRest::Document) &&
|
||||
# (self[key].nil? || property.default.nil?))
|
||||
klass.send(property.init_method, self[key])
|
||||
#end
|
||||
end
|
||||
self[key].casted_by = self if self[key].respond_to?(:casted_by)
|
||||
self[property.name].casted_by = self if self[property.name].respond_to?(:casted_by)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -133,7 +133,9 @@ module CouchRest
|
|||
fetch_view(name, opts, &block)
|
||||
else
|
||||
begin
|
||||
view = fetch_view name, opts.merge({:include_docs => true}), &block
|
||||
# auto load mentioned documents unless asked differently
|
||||
opts.merge({:include_docs => true}) unless opts.has_key?(:include_docs)
|
||||
view = fetch_view name, opts, &block
|
||||
view['rows'].collect{|r|new(r['doc'])} if view['rows']
|
||||
rescue
|
||||
# fallback for old versions of couchdb that don't
|
||||
|
|
|
@ -24,15 +24,17 @@ module CouchRest
|
|||
subklass.send(:include, CouchRest::Mixins::Properties)
|
||||
end
|
||||
|
||||
# Accessors
|
||||
attr_accessor :casted_by
|
||||
|
||||
# Callbacks
|
||||
define_callbacks :create
|
||||
define_callbacks :save
|
||||
define_callbacks :update
|
||||
define_callbacks :destroy
|
||||
|
||||
def initialize(keys={})
|
||||
def initialize(passed_keys={})
|
||||
apply_defaults # defined in CouchRest::Mixins::Properties
|
||||
keys ||= {}
|
||||
super
|
||||
cast_keys # defined in CouchRest::Mixins::Properties
|
||||
unless self['_id'] && self['_rev']
|
||||
|
|
40
spec/couchrest/more/casted_extended_doc_spec.rb
Normal file
40
spec/couchrest/more/casted_extended_doc_spec.rb
Normal file
|
@ -0,0 +1,40 @@
|
|||
require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
|
||||
require File.join(FIXTURE_PATH, 'more', 'card')
|
||||
|
||||
class Car < CouchRest::ExtendedDocument
|
||||
use_database TEST_SERVER.default_database
|
||||
|
||||
property :name
|
||||
property :driver, :cast_as => 'Driver'
|
||||
end
|
||||
|
||||
class Driver < CouchRest::ExtendedDocument
|
||||
use_database TEST_SERVER.default_database
|
||||
# You have to add a casted_by accessor if you want to reach a casted extended doc parent
|
||||
attr_accessor :casted_by
|
||||
|
||||
property :name
|
||||
end
|
||||
|
||||
describe "casting an extended document" do
|
||||
|
||||
before(:each) do
|
||||
@car = Car.new(:name => 'Renault 306')
|
||||
@driver = Driver.new(:name => 'Matt')
|
||||
end
|
||||
|
||||
# it "should not create an empty casted object" do
|
||||
# @car.driver.should be_nil
|
||||
# end
|
||||
|
||||
it "should let you assign the casted attribute after instantializing an object" do
|
||||
@car.driver = @driver
|
||||
@car.driver.name.should == 'Matt'
|
||||
end
|
||||
|
||||
it "should let the casted document who casted it" do
|
||||
Car.new(:name => 'Renault 306', :driver => @driver)
|
||||
@car.driver.casted_by.should == @car
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in a new issue