adding ducktype support

This commit is contained in:
Chris Anderson 2008-10-13 02:01:24 -07:00
parent adcaaaf59a
commit d13159039a
2 changed files with 25 additions and 3 deletions

View file

@ -273,7 +273,7 @@ module CouchRest
method_name = "by_#{keys.join('_and_')}" method_name = "by_#{keys.join('_and_')}"
self.generated_design_doc ||= default_design_doc self.generated_design_doc ||= default_design_doc
ducktype = opts.delete(:ducktype)
if opts[:map] if opts[:map]
view = {} view = {}
view['map'] = opts.delete(:map) view['map'] = opts.delete(:map)
@ -288,7 +288,7 @@ module CouchRest
key_emit = doc_keys.length == 1 ? "#{doc_keys.first}" : "[#{doc_keys.join(', ')}]" key_emit = doc_keys.length == 1 ? "#{doc_keys.first}" : "[#{doc_keys.join(', ')}]"
map_function = <<-JAVASCRIPT map_function = <<-JAVASCRIPT
function(doc) { function(doc) {
if (doc['couchrest-type'] == '#{type}' && #{key_protection}) { if (#{!ducktype ? "doc['couchrest-type'] == '#{type}' && " : ""}#{key_protection}) {
emit(#{key_emit}, null); emit(#{key_emit}, null);
} }
} }

View file

@ -39,6 +39,8 @@ class Article < CouchRest::Model
view_by :date, :descending => true view_by :date, :descending => true
view_by :user_id, :date view_by :user_id, :date
view_by :title, :ducktype => true
view_by :tags, view_by :tags,
:map => :map =>
"function(doc) { "function(doc) {
@ -430,6 +432,26 @@ describe CouchRest::Model do
end end
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 describe "a model with a compound key view" do
before(:all) do before(:all) do
written_at = Time.now - 24 * 3600 * 7 written_at = Time.now - 24 * 3600 * 7
@ -498,7 +520,7 @@ describe CouchRest::Model do
end end
it "should create a new design document on view access" do it "should create a new design document on view access" do
Article.view_by :updated_at Article.view_by :updated_at
Article.by_created_at Article.by_updated_at
newdocs = Article.database.documents :startkey => "_design/", :endkey => "_design/\u9999" newdocs = Article.database.documents :startkey => "_design/", :endkey => "_design/\u9999"
newdocs["rows"].length.should == @design_docs["rows"].length + 1 newdocs["rows"].length.should == @design_docs["rows"].length + 1
end end