Skip to content

Commit

Permalink
Fixed compatibility with edge rails (rails 3 master), with correspond…
Browse files Browse the repository at this point in the history
…ing tests.
  • Loading branch information
Fred Wu committed Mar 17, 2010
1 parent 34e6235 commit 6e5e531
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/mongo_mapper/plugins/rails.rb
Expand Up @@ -9,14 +9,22 @@ module InstanceMethods
def to_param
id.to_s
end


def to_key
persisted? ? [id.to_s] : nil
end

def to_model
self
end

def new_record?
new?
end

def persisted?
!(new_record? || destroyed?)
end

def read_attribute(name)
self[name]
Expand Down
20 changes: 20 additions & 0 deletions test/unit/test_document.rb
Expand Up @@ -111,6 +111,13 @@ class Post
doc.to_param.should be_instance_of(String)
end

should "have to_key that is array representation of id" do
doc = @document.new(:id => Mongo::ObjectID.new)
doc.save
doc.to_key.should == [doc.id.to_s]
doc.to_key.should be_instance_of(Array)
end

should "have access to logger" do
doc = @document.new
doc.logger.should == @document.logger
Expand Down Expand Up @@ -159,6 +166,19 @@ class Post
doc.id = '1234'
doc.new?.should be_true
end

should "have to_key that returns nil on non-persisted records (new records)" do
@document.new.to_key.should be(nil)
end
end

context "destroyed?" do
should "have to_key that returns nil on non-persisted records (destroyed records)" do
doc = @document.new
doc.save
doc.destroy
doc.to_key.should be(nil)
end
end

context "clone" do
Expand Down

5 comments on commit 6e5e531

@geetarista
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works perfectly!

@jnunemaker
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only with rails edge or something? I have not ran into this.

@geetarista
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jnunemaker: This is with Rails edge.

@fredwu
Copy link
Owner

@fredwu fredwu commented on 6e5e531 Mar 17, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jnunemaker: As geetarista stated above, this is a fix for the edge rails. Since one of the libraries I use (devise) requires edge rails, I think it makes sense to patch it up now. It does not effect v3.0.0.beta1 users. :)

@parasew
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this is an issue that only affects edge rails.
it looks like the form_for and similar refactoring is done, so i would say it is safe to patch it up.

Please sign in to comment.