Skip to content

Commit

Permalink
Merge 9e78193 into 35aea60
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rk5eed committed Feb 5, 2015
2 parents 35aea60 + 9e78193 commit 03d2568
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
18 changes: 18 additions & 0 deletions examples/mongoid_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class Person
field :name
end

class Item
include Mongoid::Document
field :title
validates :title, presence: true, on: :my_context
end

class BugTest < Minitest::Test
def test_query_count
p = Person.create!(name: 'arthurnn')
Expand All @@ -35,4 +41,16 @@ def test_query_count

assert_equal 2, query_counter
end

def test_default_context
item = Item.new
assert item.valid?
assert item.save
end

def test_my_context
item = Item.new
assert_equal item.valid?(:my_context), false, 'Cannot validate context'
assert_equal item.save(context: :my_context), false, 'Cannot save correct context'
end
end
3 changes: 2 additions & 1 deletion lib/mongoid/persistable/creatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def post_process_insert
#
# @since 4.0.0
def prepare_insert(options = {})
return self if performing_validations?(options) && invalid?(:create)
return self if performing_validations?(options) &&
invalid?(options[:context] || :create)
result = run_callbacks(:save) do
run_callbacks(:create) do
yield(self)
Expand Down
3 changes: 2 additions & 1 deletion lib/mongoid/persistable/updatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def init_atomic_updates
#
# @since 4.0.0
def prepare_update(options = {})
return false if performing_validations?(options) && invalid?(:update)
return false if performing_validations?(options) &&
invalid?(options[:context] || :update)
process_flagged_destroys
result = run_callbacks(:save) do
run_callbacks(:update) do
Expand Down
5 changes: 5 additions & 0 deletions spec/app/models/contextable_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ContextableItem
include Mongoid::Document
field :title
validates :title, presence: true, on: :in_context
end
37 changes: 37 additions & 0 deletions spec/mongoid/persistable/savable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
Person.create
end

let(:contextable_item) do
ContextableItem.new
end

let(:persisted_contextable_item) do
ContextableItem.create(title: 'sir')
end

context "when skipping validation" do

context "when no relations are involved" do
Expand Down Expand Up @@ -285,6 +293,35 @@
}.to raise_error(Mongoid::Errors::ReadonlyDocument)
end
end

context "when validation context isn't assigned" do
it "returns true" do
expect(contextable_item.save).to be true
end
end

context "when validation context exists" do
context "on new document" do
it "returns true" do
contextable_item.title = "sir"
expect(contextable_item.save(context: :in_context)).to be true
end
it "returns false" do
expect(contextable_item.save(context: :in_context)).to be false
end
end

context "on persisted document" do
it "returns true" do
persisted_contextable_item.title = "lady"
expect(persisted_contextable_item.save(context: :in_context)).to be true
end
it "returns false" do
persisted_contextable_item.title = nil
expect(persisted_contextable_item.save(context: :in_context)).to be false
end
end
end
end

describe "save!" do
Expand Down

0 comments on commit 03d2568

Please sign in to comment.