Commit 856fe259 authored by Sam Lown's avatar Sam Lown
Browse files

Adding :allow_nil option to views

parent 91f6c510
......@@ -9,6 +9,7 @@
* Streamer now available for all queries that return multiple documents (thanks @pcapr for pointer)
* Streamer#view method removed, use Database.view with block
* Database#changes method added (with streamer option)
* Added :allow_nil option when creating views
WARNING: If you depend on ordering by Time, this release may cause issues on old databases!
Either update your documents to use the new format (JSON standard), or use Javascript's Date.parse
......
......@@ -15,7 +15,8 @@ module CouchRest
doc_keys = keys.collect{|k| "doc['#{k}']"}
key_emit = doc_keys.length == 1 ? "#{doc_keys.first}" : "[#{doc_keys.join(', ')}]"
guards = opts.delete(:guards) || []
guards += doc_keys.map{|k| "(#{k} != null)"}
guards += doc_keys.map{|k| "(#{k} != null)"} unless opts.delete(:allow_nil)
guards << 'true' if guards.empty?
map_function = <<-JAVASCRIPT
function(doc) {
if (#{guards.join(' && ')}) {
......
......@@ -154,6 +154,26 @@ describe CouchRest::Design do
end
end
describe "a view with nil and 0 values and :allow_nil" do
before(:all) do
@db = reset_test_db!
@des = CouchRest::Design.new
@des.name = "test"
@des.view_by :code, :allow_nil => true
@des.database = @db
@des.save
@db.bulk_save([{"code" => "a", "age" => 2},
{"code" => nil, "age" => 4},{"code" => 0, "age" => 9}])
end
it "should work" do
res = @des.view :by_code
res["rows"][0]["key"].should == nil
res["rows"][1]["key"].should == 0
res["rows"][2]["key"].should == "a"
end
end
describe "a view with a reduce function" do
before(:all) do
@db = reset_test_db!
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment