Fixing ActiveModel special calls
This commit is contained in:
parent
c280b3a29b
commit
188fd0d4de
5 changed files with 94 additions and 5 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
9
lib/couchrest/model/support/hash.rb
Normal file
9
lib/couchrest/model/support/hash.rb
Normal 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
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue