Permalink
Browse files

Swapping a bunch of association stuff out with plucky.

  • Loading branch information...
1 parent 4b058c2 commit 9d885fc3f65731feaa8400ebdd696259ec10120b @jnunemaker jnunemaker committed May 29, 2010
View
@@ -1,4 +1,5 @@
-0.7.6 => 0.7.7
+0.7.6 => 0.8
* Proxy#owner has been removed in favor of Proxy#proxy_owner
* @new instance variable renamed to @_new (you shouldn't be using this anyway)
- * Removed undefining of object_id in proxies and equal? method in Equality. This means checking equal? for a proxy and a regular document will always be false even if proxy refers to same document. Check Proxy#target instead. (ie: root.equal?(item.root.target))
+ * Removed undefining of object_id in proxies and equal? method in Equality. This means checking equal? for a proxy and a regular document will always be false even if proxy refers to same document. Check Proxy#target instead. (ie: root.equal?(item.root.target))
+ * find no longer takes options as a last argument. It only works with id, multiple ids, array of ids.
@@ -6,33 +6,31 @@ class InArrayProxy < Collection
include MongoMapper::Plugins::DynamicQuerying::ClassMethods
def find(*args)
- options = args.extract_options!
- klass.find(*scoped_ids(args) << scoped_options(options))
+ query.find(*scoped_ids(args))
end
def find!(*args)
- options = args.extract_options!
- klass.find!(*scoped_ids(args) << scoped_options(options))
+ query.find!(*scoped_ids(args))
end
def paginate(options)
query.paginate(options)
end
def all(options={})
- klass.all(scoped_options(options))
+ query(options).all
end
def first(options={})
- klass.first(scoped_options(options))
+ query(options).first
end
def last(options={})
- klass.last(scoped_options(options))
+ query(options).last
end
def count(options={})
- options.blank? ? ids.size : klass.count(scoped_options(options))
+ options.blank? ? ids.size : query(options).count
end
def destroy_all(options={})
@@ -44,7 +42,7 @@ def destroy_all(options={})
end
def delete_all(options={})
- docs = all(options.merge(:select => ['_id']))
+ docs = query(options).fields(:_id).all
docs.each { |doc| ids.delete(doc.id) }
klass.delete(docs.map(&:id))
reset
@@ -98,22 +96,22 @@ def replace(docs)
private
def query(options={})
- klass.query(scoped_options(options))
+ klass.
+ query(association.query_options).
+ update(options).
+ update(criteria)
end
- def scoped_conditions
+ def criteria
{:_id => ids}
end
- def scoped_options(options)
- association.query_options.merge(options).merge(scoped_conditions)
- end
-
def scoped_ids(args)
- args.flatten.select do |id|
+ valid = args.flatten.select do |id|
id = ObjectId.to_mongo(id) if klass.using_object_id?
ids.include?(id)
end
+ valid.empty? ? nil : valid
end
def find_target
@@ -4,7 +4,7 @@ module Plugins
module Associations
class ManyDocumentsAsProxy < ManyDocumentsProxy
protected
- def scoped_conditions
+ def criteria
{type_key_name => proxy_owner.class.name, id_key_name => proxy_owner.id}
end
@@ -6,13 +6,11 @@ class ManyDocumentsProxy < Collection
include MongoMapper::Plugins::DynamicQuerying::ClassMethods
def find(*args)
- options = args.extract_options!
- klass.find(*args << scoped_options(options))
+ query.find(*args)
end
def find!(*args)
- options = args.extract_options!
- klass.find!(*args << scoped_options(options))
+ query.find!(*args)
end
def paginate(options)
@@ -78,7 +76,7 @@ def destroy_all(options={})
end
def delete_all(options={})
- klass.delete_all(options.merge(scoped_conditions))
+ query(options).remove
reset
end
@@ -93,17 +91,16 @@ def save_to_collection(options={})
protected
def query(options={})
- klass.query(scoped_options(options))
+ klass.
+ query(association.query_options).
+ update(options).
+ update(criteria)
end
- def scoped_conditions
+ def criteria
{self.foreign_key => proxy_owner.id}
end
- def scoped_options(options)
- association.query_options.merge(options).merge(scoped_conditions)
- end
-
def find_target
all
end
@@ -9,6 +9,16 @@ def model(model=nil)
self
end
+ def find!(*ids)
+ raise DocumentNotFound, "Couldn't find without an ID" if ids.size == 0
+
+ find(*ids).tap do |result|
+ if result.nil? || ids.size != Array(result).size
+ raise DocumentNotFound, "Couldn't find all of the ids (#{ids.join(',')}). Found #{Array(result).size}, but was expecting #{ids.size}"
+ end
+ end
+ end
+
def all(opts={})
super.map { |doc| model.load(doc) }
end

0 comments on commit 9d885fc

Please sign in to comment.