lib/couchrest/model/designs/view.rb in couchrest_model-2.0.0 vs lib/couchrest/model/designs/view.rb in couchrest_model-2.0.1
- old
+ new
@@ -18,11 +18,15 @@
# Initialize a new View object. This method should not be called from
# outside CouchRest Model.
def initialize(design_doc, parent, new_query = {}, name = nil)
self.design_doc = design_doc
- proxy = new_query.delete(:proxy)
+
+ # Extrace important non-regular query values
+ proxy = new_query.delete(:proxy)
+ delete = new_query.delete(:delete)
+
if parent.is_a?(Class) && parent < CouchRest::Model::Base
raise "Name must be provided for view to be initialized" if name.nil?
self.model = (proxy || parent)
self.owner = parent
self.name = name.to_s
@@ -34,11 +38,15 @@
self.name = parent.name
self.query = parent.query.dup
else
raise "View cannot be initialized without a parent Model or View"
end
+
+ # Update the local query hash
query.update(new_query)
+ delete.each{|k| query.delete(k)} if delete
+
super()
end
# == View Execution Methods
@@ -274,11 +282,11 @@
# Use the reduce function on the view. If none is available this method
# will fail.
def reduce
raise "Cannot reduce a view without a reduce method" unless can_reduce?
- update_query(:reduce => true, :include_docs => nil)
+ update_query(:reduce => true, :delete => [:include_docs])
end
# Control whether the reduce function reduces to a set of distinct keys
# or to a single result row.
#
@@ -412,11 +420,11 @@
# Remove the reduce value if its not needed to prevent CouchDB errors
query.delete(:reduce) unless can_reduce?
design_doc.sync(use_database)
- self.result = design_doc.view_on(use_database, name, query.reject{|k,v| v.nil?})
+ self.result = design_doc.view_on(use_database, name, query)
end
# Class Methods
class << self
@@ -458,11 +466,11 @@
# Is this an all view?
if name.to_s == 'all'
opts[:map] = <<-EOF
function(doc) {
- if (doc['#{model.model_type_key}'] == '#{model.to_s}') {
+ if (doc['#{model.model_type_key}'] == '#{model.model_type_value}') {
emit(doc._id, null);
}
}
EOF
elsif !opts[:map]
@@ -471,10 +479,10 @@
end
raise "View cannot be created without recognised name, :map or :by options" if opts[:by].nil?
opts[:allow_blank] = opts[:allow_blank].nil? ? true : opts[:allow_blank]
opts[:guards] ||= []
- opts[:guards].push "(doc['#{model.model_type_key}'] == '#{model.to_s}')"
+ opts[:guards].push "(doc['#{model.model_type_key}'] == '#{model.model_type_value}')"
keys = opts[:by].map{|o| "doc['#{o}']"}
emit = keys.length == 1 ? keys.first : "[#{keys.join(', ')}]"
opts[:guards] += keys.map{|k| "(#{k} != null)"} unless opts[:allow_nil]
opts[:guards] += keys.map{|k| "(#{k} != '')"} unless opts[:allow_blank]