Skip to content

Commit

Permalink
Pass through block in first_or_create(!):
Browse files Browse the repository at this point in the history
Instead of calling after.

[ fix #3034 ]
  • Loading branch information
durran committed May 14, 2013
1 parent eaaab84 commit f26b41f
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 205 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,9 @@ For instructions on upgrading to newer versions, visit

### Resolved Issues

* \#3034 `first_or_create` on criterion now properly passes the block to create
instead of calling after the document was created.

* \#3021 Removed `mongoid.yml` warning from initializer, this is now handled by
the session configuration options.

Expand Down
6 changes: 2 additions & 4 deletions lib/mongoid/criterion/modifiable.rb
Expand Up @@ -181,10 +181,8 @@ def find_or(method, attrs = {}, &block)
# @return [ Document ] The first or new document.
#
# @since 3.1.0
def first_or(method, attrs = nil)
document = first || create_document(method, attrs)
yield(document) if block_given?
document
def first_or(method, attrs = {}, &block)
first || create_document(method, attrs, &block)
end
end
end
Expand Down
201 changes: 0 additions & 201 deletions spec/mongoid/criteria_spec.rb
Expand Up @@ -1952,207 +1952,6 @@
end
end

describe "first_or_create" do

let!(:band) do
Band.create(name: "Depeche Mode")
end

context "when the document is found" do

let(:found) do
Band.where(name: "Depeche Mode").first_or_create
end

it "returns the document" do
found.should eq(band)
end
end

context "when the document is not found" do

context "when attributes are provided" do

let(:document) do
Band.where(name: "Tool").first_or_create(origin: "Essex")
end

it "returns a new document" do
document.name.should eq("Tool")
end

it "returns a persisted document" do
document.should be_persisted
end

it "sets the additional attributes" do
document.origin.should eq("Essex")
end
end

context "when attributes are not provided" do

let(:document) do
Band.where(name: "Tool").first_or_create
end

it "returns a new document" do
document.name.should eq("Tool")
end

it "returns a persisted document" do
document.should be_persisted
end
end

context "when a block is provided" do

let(:document) do
Band.where(name: "Tool").first_or_create do |doc|
doc.active = false
end
end

it "returns a new document" do
document.name.should eq("Tool")
end

it "returns a persisted document" do
document.should be_persisted
end

it "yields to the block" do
document.active.should be_false
end
end

context "when the criteria is complex" do

context "when the document is not found" do

let(:document) do
Band.in(name: [ "New Order" ]).first_or_create(active: false)
end

it "returns a new document" do
document.active.should be_false
end

it "returns a persisted document" do
document.should be_persisted
end
end
end
end
end

describe "first_or_create!" do

context "when validation fails on the new document" do

it "raises an error" do
expect {
Account.where(number: "12345").first_or_create!
}.to raise_error(Mongoid::Errors::Validations)
end
end

context "when the document is found" do

let!(:band) do
Band.with(safe: true).create!(name: "Depeche Mode")
end

let(:found) do
Band.where(name: "Depeche Mode").first_or_create!
end

it "returns the document" do
found.should eq(band)
end
end

context "when the document is not found" do

let!(:band) do
Band.with(safe: true).create!(name: "Depeche Mode")
end

context "when attributes are provided" do

let(:document) do
Band.where(name: "Tool").first_or_create!(origin: "Essex")
end

it "returns a new document" do
document.name.should eq("Tool")
end

it "returns a persisted document" do
document.should be_persisted
end

it "sets the additional attributes" do
document.origin.should eq("Essex")
end
end

context "when attributes are not provided" do

let(:document) do
Band.where(name: "Tool").first_or_create!
end

it "returns a new document" do
document.name.should eq("Tool")
end

it "returns a persisted document" do
document.should be_persisted
end
end

context "when a block is provided" do

let(:document) do
Band.where(name: "Tool").first_or_create! do |doc|
doc.active = false
end
end

it "returns a new document" do
document.name.should eq("Tool")
end

it "returns a persisted document" do
document.should be_persisted
end

it "yields to the block" do
document.active.should be_false
end
end

context "when the criteria is complex" do

context "when the document is not found" do

let(:document) do
Band.in(name: [ "New Order" ]).first_or_create!(active: false)
end

it "returns a new document" do
document.active.should be_false
end

it "returns a persisted document" do
document.should be_persisted
end
end
end
end
end

describe "first_or_initialize" do

let!(:band) do
Expand Down

0 comments on commit f26b41f

Please sign in to comment.