From 17dac85a02eed3ddb7fa9ccc7ab8ff4ef55f649e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Tapaj=C3=B3s?= Date: Fri, 26 Feb 2010 21:39:09 -0300 Subject: [PATCH] Changing CouchRest::ExtendedDocument to allow chaining the inherit class callback --- history.txt | 1 + lib/couchrest/more/extended_document.rb | 2 + .../more/casted_extended_doc_spec.rb | 4 +- .../more/extended_doc_inherited_spec.rb | 40 +++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 spec/couchrest/more/extended_doc_inherited_spec.rb diff --git a/history.txt b/history.txt index fecc3c3..967ea12 100644 --- a/history.txt +++ b/history.txt @@ -1,6 +1,7 @@ == Next Version * Major enhancements + * CouchRest::ExtendedDocument allow chaining the inherit class callback (Kenneth Kalmer) - http://github.com/couchrest/couchrest/issues#issue/8 * Minor enhancements * Fix attachment bug (Johannes Jörg Schmidt) diff --git a/lib/couchrest/more/extended_document.rb b/lib/couchrest/more/extended_document.rb index 0c26240..c6f3c85 100644 --- a/lib/couchrest/more/extended_document.rb +++ b/lib/couchrest/more/extended_document.rb @@ -21,9 +21,11 @@ module CouchRest end def self.inherited(subklass) + super subklass.send(:include, CouchRest::Mixins::Properties) subklass.class_eval <<-EOS, __FILE__, __LINE__ + 1 def self.inherited(subklass) + super subklass.properties = self.properties.dup end EOS diff --git a/spec/couchrest/more/casted_extended_doc_spec.rb b/spec/couchrest/more/casted_extended_doc_spec.rb index 4df60c1..e92d34f 100644 --- a/spec/couchrest/more/casted_extended_doc_spec.rb +++ b/spec/couchrest/more/casted_extended_doc_spec.rb @@ -49,7 +49,7 @@ describe "assigning a value to casted attribute after initializing an object" do end 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) end @@ -60,7 +60,7 @@ describe "casting an extended document from parsed JSON" do before(:each) do @driver = Driver.new(:name => 'Matt') @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 it "should cast casted attribute" do diff --git a/spec/couchrest/more/extended_doc_inherited_spec.rb b/spec/couchrest/more/extended_doc_inherited_spec.rb new file mode 100644 index 0000000..d52fbd9 --- /dev/null +++ b/spec/couchrest/more/extended_doc_inherited_spec.rb @@ -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