Fixing ActiveModel special calls

This commit is contained in:
Sam Lown 2010-06-21 13:58:19 +02:00
parent c280b3a29b
commit 188fd0d4de
5 changed files with 94 additions and 5 deletions

View file

@ -54,7 +54,7 @@ module CouchRest
end end
after_initialize if respond_to?(:after_initialize) after_initialize if respond_to?(:after_initialize)
end end
# Temp solution to make the view_by methods available # Temp solution to make the view_by methods available
def self.method_missing(m, *args, &block) def self.method_missing(m, *args, &block)
@ -85,9 +85,24 @@ module CouchRest
!@casted_by !@casted_by
end 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_record? :new?
alias :new_document? :new? alias :new_document? :new?
alias :to_key :id
alias :to_param :id
end end
end end
end end

View file

@ -40,6 +40,19 @@ module CouchRest::Model
@casted_by.nil? ? true : @casted_by.new? @casted_by.nil? ? true : @casted_by.new?
end end
alias :new_record? :new? 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 # Sets the attributes from a hash
def update_attributes_without_saving(hash) def update_attributes_without_saving(hash)
@ -51,6 +64,5 @@ module CouchRest::Model
end end
end end
alias :attributes= :update_attributes_without_saving alias :attributes= :update_attributes_without_saving
end end
end end

View file

@ -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

View file

@ -46,11 +46,11 @@ require "couchrest/model/associations"
# Monkey patches applied to couchrest # Monkey patches applied to couchrest
require "couchrest/model/support/couchrest" require "couchrest/model/support/couchrest"
require "couchrest/model/support/hash"
# Base libraries # Base libraries
require "couchrest/model/casted_model" require "couchrest/model/casted_model"
require "couchrest/model/base" require "couchrest/model/base"
# Add rails support *after* everything has loaded # Add rails support *after* everything has loaded
require "couchrest/model/support/rails" if defined?(Rails)

View file

@ -39,7 +39,60 @@ describe "Model Base" do
@obj.should == { 'couchrest-type' => 'Basic' } @obj.should == { 'couchrest-type' => 'Basic' }
end end
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 describe "update attributes without saving" do
before(:each) do before(:each) do