diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb index d84504c..fbcd71a 100644 --- a/lib/couchrest/core/model.rb +++ b/lib/couchrest/core/model.rb @@ -273,7 +273,7 @@ module CouchRest method_name = "by_#{keys.join('_and_')}" self.generated_design_doc ||= default_design_doc - + ducktype = opts.delete(:ducktype) if opts[:map] view = {} view['map'] = opts.delete(:map) @@ -288,7 +288,7 @@ module CouchRest key_emit = doc_keys.length == 1 ? "#{doc_keys.first}" : "[#{doc_keys.join(', ')}]" map_function = <<-JAVASCRIPT function(doc) { - if (doc['couchrest-type'] == '#{type}' && #{key_protection}) { + if (#{!ducktype ? "doc['couchrest-type'] == '#{type}' && " : ""}#{key_protection}) { emit(#{key_emit}, null); } } diff --git a/spec/couchrest/core/model_spec.rb b/spec/couchrest/core/model_spec.rb index 9667fee..76df212 100644 --- a/spec/couchrest/core/model_spec.rb +++ b/spec/couchrest/core/model_spec.rb @@ -39,6 +39,8 @@ class Article < CouchRest::Model view_by :date, :descending => true view_by :user_id, :date + view_by :title, :ducktype => true + view_by :tags, :map => "function(doc) { @@ -430,6 +432,26 @@ describe CouchRest::Model do end end + describe "a ducktype view" do + before(:each) do + @id = @db.save({:title => true})['id'] + @as = Article.by_title + end + it "should setup" do + puts id + duck = Article.get(@id) + duck.should == 'x' + end + it "should make the design doc" do + @doc = Article.design_doc + @doc["views"]["by_title"]["map"].should_not include("couchrest") + end + it "should not look for class" do |variable| + @as.should == 'x' + [0]['_id'].should == @id + end + end + describe "a model with a compound key view" do before(:all) do written_at = Time.now - 24 * 3600 * 7 @@ -498,7 +520,7 @@ describe CouchRest::Model do end it "should create a new design document on view access" do Article.view_by :updated_at - Article.by_created_at + Article.by_updated_at newdocs = Article.database.documents :startkey => "_design/", :endkey => "_design/\u9999" newdocs["rows"].length.should == @design_docs["rows"].length + 1 end