From d62c2d1439aa45aae4543180b7857ea5ed0a1881 Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Wed, 27 Apr 2011 13:30:08 +0200 Subject: [PATCH] Adding filter support to design docs --- lib/couchrest/model/designs.rb | 11 +++++++++++ spec/couchrest/designs_spec.rb | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/couchrest/model/designs.rb b/lib/couchrest/model/designs.rb index ddc157a..e17f1bc 100644 --- a/lib/couchrest/model/designs.rb +++ b/lib/couchrest/model/designs.rb @@ -65,6 +65,17 @@ module CouchRest create_view_method(name) end + # Really simple design function that allows a filter + # to be added. Filters are simple functions used when listening + # to the _changes feed. + # + # No methods are created here, the design is simply updated. + # See the CouchDB API for more information on how to use this. + def filter(name, function) + filters = (self.model.design_doc['filters'] ||= {}) + filters[name.to_s] = function + end + def create_view_method(name) model.class_eval <<-EOS, __FILE__, __LINE__ + 1 def self.#{name}(opts = {}) diff --git a/spec/couchrest/designs_spec.rb b/spec/couchrest/designs_spec.rb index 9f578f8..3fa2f6a 100644 --- a/spec/couchrest/designs_spec.rb +++ b/spec/couchrest/designs_spec.rb @@ -87,6 +87,20 @@ describe "Design" do end + describe "#filter" do + + before :each do + @object = @klass.new(DesignModel) + end + + it "should add the provided function to the design doc" do + @object.filter(:important, "function(doc, req) { return doc.priority == 'high'; }") + DesignModel.design_doc['filters'].should_not be_empty + DesignModel.design_doc['filters']['important'].should_not be_blank + end + + end + describe "#create_view_method" do before :each do @object = @klass.new(DesignModel)