Changing CouchRest::ExtendedDocument to allow chaining the inherit class callback

This commit is contained in:
Marcos Tapajós 2010-02-26 21:39:09 -03:00
parent b1f4040a44
commit 17dac85a02
4 changed files with 45 additions and 2 deletions

View file

@ -1,6 +1,7 @@
== Next Version == Next Version
* Major enhancements * Major enhancements
* CouchRest::ExtendedDocument allow chaining the inherit class callback (Kenneth Kalmer) - http://github.com/couchrest/couchrest/issues#issue/8
* Minor enhancements * Minor enhancements
* Fix attachment bug (Johannes Jörg Schmidt) * Fix attachment bug (Johannes Jörg Schmidt)

View file

@ -21,9 +21,11 @@ module CouchRest
end end
def self.inherited(subklass) def self.inherited(subklass)
super
subklass.send(:include, CouchRest::Mixins::Properties) subklass.send(:include, CouchRest::Mixins::Properties)
subklass.class_eval <<-EOS, __FILE__, __LINE__ + 1 subklass.class_eval <<-EOS, __FILE__, __LINE__ + 1
def self.inherited(subklass) def self.inherited(subklass)
super
subklass.properties = self.properties.dup subklass.properties = self.properties.dup
end end
EOS EOS

View file

@ -49,7 +49,7 @@ describe "assigning a value to casted attribute after initializing an object" do
end end
it "should cast attribute" do it "should cast attribute" do
@car.driver = JSON.parse(JSON.generate(@driver)) @car.driver = JSON.parse(@driver.to_json)
@car.driver.should be_instance_of(Driver) @car.driver.should be_instance_of(Driver)
end end
@ -60,7 +60,7 @@ describe "casting an extended document from parsed JSON" do
before(:each) do before(:each) do
@driver = Driver.new(:name => 'Matt') @driver = Driver.new(:name => 'Matt')
@car = Car.new(:name => 'Renault 306', :driver => @driver) @car = Car.new(:name => 'Renault 306', :driver => @driver)
@new_car = Car.new(JSON.parse(JSON.generate(@car))) @new_car = Car.new(JSON.parse(@car.to_json))
end end
it "should cast casted attribute" do it "should cast casted attribute" do

View file

@ -0,0 +1,40 @@
require File.expand_path('../../../spec_helper', __FILE__)
begin
require 'rubygems' unless ENV['SKIP_RUBYGEMS']
require 'activesupport'
ActiveSupport::JSON.backend = :JSONGem
class PlainParent
class_inheritable_accessor :foo
self.foo = :bar
end
class PlainChild < PlainParent
end
class ExtendedParent < CouchRest::ExtendedDocument
class_inheritable_accessor :foo
self.foo = :bar
end
class ExtendedChild < ExtendedParent
end
describe "Using chained inheritance without CouchRest::ExtendedDocument" do
it "should preserve inheritable attributes" do
PlainParent.foo.should == :bar
PlainChild.foo.should == :bar
end
end
describe "Using chained inheritance with CouchRest::ExtendedDocument" do
it "should preserve inheritable attributes" do
ExtendedParent.foo.should == :bar
ExtendedChild.foo.should == :bar
end
end
rescue LoadError
puts "This spec requires 'active_support' to be loaded"
end