Skip to content
Browse files

Interface method for turning partial updates on.

This makes it easier to fail fast if someone tries something that won't work
  • Loading branch information...
1 parent 4e5b22d commit 2dc16c643658e38748af40b542e6454dd42afb14 @jnunemaker committed
Showing with 31 additions and 3 deletions.
  1. +16 −0 lib/toy/mongo/partial_updating.rb
  2. +15 −3 spec/toy/mongo/partial_updating_spec.rb
View
16 lib/toy/mongo/partial_updating.rb
@@ -1,5 +1,11 @@
module Toy
module Mongo
+ class IncompatibleAdapter < Error
+ def initialize(name)
+ super "In order to use partial updates, you need to be using the :mongo_atomic adapter, but you are using :#{name}"
+ end
+ end
+
module PartialUpdating
extend ActiveSupport::Concern
@@ -9,6 +15,16 @@ module PartialUpdating
self.partial_updates = false
end
+ module ClassMethods
+ def use_partial_updates
+ if adapter.name != :mongo_atomic
+ raise IncompatibleAdapter.new(adapter.name)
+ end
+
+ self.partial_updates = true
+ end
+ end
+
# Very basic method for determining what has changed locally
# so we can just update changes instead of entire document
#
View
18 spec/toy/mongo/partial_updating_spec.rb
@@ -97,28 +97,40 @@
end
describe ".partial_updates" do
+ before do
+ User.adapter :mongo_atomic, STORE
+ end
+
it "defaults to false" do
User.partial_updates.should be_false
end
it "can be turned on" do
- User.partial_updates = true
+ User.use_partial_updates
User.partial_updates.should be_true
end
it "is inherited" do
- User.partial_updates = true
+ User.use_partial_updates
subclass = Class.new(User)
subclass.partial_updates.should be_true
end
it "is inherited separate from superclass" do
- User.partial_updates = true
+ User.use_partial_updates
subclass = Class.new(User)
subclass.partial_updates = false
User.partial_updates.should be_true
subclass.partial_updates.should be_false
end
+
+ it "raises an error if not using mongo_atomic" do
+ User.adapter :mongo, STORE
+
+ expect {
+ User.use_partial_updates
+ }.to raise_error Toy::Mongo::IncompatibleAdapter, "In order to use partial updates, you need to be using the :mongo_atomic adapter, but you are using :mongo"
+ end
end
describe "#persist" do

0 comments on commit 2dc16c6

Please sign in to comment.
Something went wrong with that request. Please try again.