diff --git a/lib/couchrest/model/base.rb b/lib/couchrest/model/base.rb index 4c4856b..b22e8d3 100644 --- a/lib/couchrest/model/base.rb +++ b/lib/couchrest/model/base.rb @@ -54,7 +54,7 @@ module CouchRest end after_initialize if respond_to?(:after_initialize) end - + # Temp solution to make the view_by methods available def self.method_missing(m, *args, &block) @@ -85,9 +85,24 @@ module CouchRest !@casted_by end - # for compatibility with old-school frameworks + ## Compatibility with ActiveSupport and older frameworks + + # Hack so that CouchRest::Document, which descends from Hash, + # doesn't appear to Rails routing as a Hash of options + def is_a?(klass) + return false if klass == Hash + super + end + alias :kind_of? :is_a? + + def persisted? + !new? + end + alias :new_record? :new? alias :new_document? :new? + alias :to_key :id + alias :to_param :id end end end diff --git a/lib/couchrest/model/casted_model.rb b/lib/couchrest/model/casted_model.rb index 6aa9bce..9ef1603 100644 --- a/lib/couchrest/model/casted_model.rb +++ b/lib/couchrest/model/casted_model.rb @@ -40,6 +40,19 @@ module CouchRest::Model @casted_by.nil? ? true : @casted_by.new? end alias :new_record? :new? + + def persisted? + !new? + end + + # The to_param method is needed for rails to generate resourceful routes. + # In your controller, remember that it's actually the id of the document. + def id + return nil if base_doc.nil? + base_doc.id + end + alias :to_key :id + alias :to_param :id # Sets the attributes from a hash def update_attributes_without_saving(hash) @@ -51,6 +64,5 @@ module CouchRest::Model end end alias :attributes= :update_attributes_without_saving - end end diff --git a/lib/couchrest/model/support/hash.rb b/lib/couchrest/model/support/hash.rb new file mode 100644 index 0000000..8d7b8e7 --- /dev/null +++ b/lib/couchrest/model/support/hash.rb @@ -0,0 +1,9 @@ +# This file contains various hacks for Rails compatibility. +class Hash + # Hack so that CouchRest::Document, which descends from Hash, + # doesn't appear to Rails routing as a Hash of options + def self.===(other) + return false if self == Hash && other.is_a?(CouchRest::Document) + super + end +end diff --git a/lib/couchrest_model.rb b/lib/couchrest_model.rb index 0bd2083..7859c02 100644 --- a/lib/couchrest_model.rb +++ b/lib/couchrest_model.rb @@ -46,11 +46,11 @@ require "couchrest/model/associations" # Monkey patches applied to couchrest require "couchrest/model/support/couchrest" +require "couchrest/model/support/hash" # Base libraries require "couchrest/model/casted_model" require "couchrest/model/base" # Add rails support *after* everything has loaded -require "couchrest/model/support/rails" if defined?(Rails) diff --git a/spec/couchrest/base_spec.rb b/spec/couchrest/base_spec.rb index ecee222..a79110a 100644 --- a/spec/couchrest/base_spec.rb +++ b/spec/couchrest/base_spec.rb @@ -39,7 +39,60 @@ describe "Model Base" do @obj.should == { 'couchrest-type' => 'Basic' } end end - + + describe "ActiveModel compatability" do + + before(:each) do + @obj = Basic.new(nil) + end + + describe "#to_key" do + context "when the document is new" do + it "returns nil" do + @obj.to_key.should be_nil + end + end + + context "when the document is not new" do + it "returns id" do + @obj.save + @obj.to_key.should eql(@obj['_id']) + end + end + end + + describe "#to_param" do + context "when the document is new" do + it "returns nil" do + @obj.to_param.should be_nil + end + end + + context "when the document is not new" do + it "returns id" do + @obj.save + @obj.to_param.should eql(@obj['_id']) + end + end + end + + describe "#persisted?" do + context "when the document is new" do + it "returns false" do + @obj.persisted?.should == false + end + end + + context "when the document is not new" do + it "returns id" do + @obj.save + @obj.persisted?.should == true + end + end + end + + + end describe "update attributes without saving" do before(:each) do