Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions lib/mongoid_auto_increment_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,12 @@ def self.included(base)
end
end

# hack id nil when Document.new
def identify
Identity.new(self).create
nil
end
alias_method :super_initialize, :initialize

alias_method :super_as_document, :as_document
def as_document
result = super_as_document
if result[ID_FIELD].blank?
result[ID_FIELD] = Identity.generate_id(self)
end
result
def initialize(attrs = nil)
@attributes ||= {}
self[ID_FIELD] = Identity.generate_id(self)
super_initialize attrs
end
end
end
27 changes: 21 additions & 6 deletions spec/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

describe "Mongoid::AutoIncrementId" do
describe 'Base test' do
before do
Mongoid.purge!
end

after do
Post.delete_all
Tag.delete_all
User.delete_all
Log.delete_all
end

it "new record Id will be nil" do
expect(Post.new.id).to eq nil
it "new record Id will be integer" do
expect(Post.new.id).to be_a Integer
end

it "does Id start from 1" do
Expand Down Expand Up @@ -39,9 +43,9 @@
expect(p4.id - 2).to eq p2.id
end

it "does return nil id when Model.new" do
expect(Post.new.id).to eq nil
expect(Post.new._id).to eq nil
it "does return id when Model.new" do
expect(Post.new.id).to be_a Integer
expect(Post.new._id).to be_a Integer
end

it "does can find data by String and Integer id" do
Expand Down Expand Up @@ -113,6 +117,17 @@
expect(Log.where(:title => "test tag log").count).to eq 1
expect(Log.where(:title => "test tag log").first._type).to eq "TagLog"
end

context "when accepts_nested_attributes_for" do
before { User.accepts_nested_attributes_for :posts }

it "set association" do
user = User.new(:email => "[email protected]", :posts_attributes => { "0" => {:title => "This is title!"}})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Line is too long. [105/80]
Use the new Ruby 1.9 hash syntax.
Space inside } missing.
Space inside { missing.

user.save
user.reload
expect(user.posts.size).to eq(1)
end
end
end


Expand All @@ -134,4 +149,4 @@
end

end
end
end
3 changes: 0 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,4 @@ def database_id

RSpec.configure do |config|
config.mock_with :mocha
config.after :suite do
Mongoid.purge!
end
end