diff --git a/lib/couchrest/model/base.rb b/lib/couchrest/model/base.rb index b22e8d3..5a80d49 100644 --- a/lib/couchrest/model/base.rb +++ b/lib/couchrest/model/base.rb @@ -98,11 +98,14 @@ module CouchRest def persisted? !new? end + + def to_key + new? ? nil : [id] + end + alias :to_param :id alias :new_record? :new? alias :new_document? :new? - alias :to_key :id - alias :to_param :id end end end diff --git a/spec/couchrest/base_spec.rb b/spec/couchrest/base_spec.rb index a79110a..70057a4 100644 --- a/spec/couchrest/base_spec.rb +++ b/spec/couchrest/base_spec.rb @@ -40,7 +40,7 @@ describe "Model Base" do end end - describe "ActiveModel compatability" do + describe "ActiveModel compatability Basic" do before(:each) do @obj = Basic.new(nil) @@ -54,9 +54,9 @@ describe "Model Base" do end context "when the document is not new" do - it "returns id" do + it "returns id in an array" do @obj.save - @obj.to_key.should eql(@obj['_id']) + @obj.to_key.should eql([@obj['_id']]) end end end @@ -91,6 +91,13 @@ describe "Model Base" do end end + describe "#model_name" do + it "returns the name of the model" do + @obj.class.model_name.should eql('Basic') + WithDefaultValues.model_name.human.should eql("With default values") + end + end + end