diff --git a/lib/couchrest/mixins/views.rb b/lib/couchrest/mixins/views.rb index 902f064..994d271 100644 --- a/lib/couchrest/mixins/views.rb +++ b/lib/couchrest/mixins/views.rb @@ -7,7 +7,11 @@ module CouchRest end module ClassMethods - attr_accessor :design_doc_slug_cache, :design_doc_fresh + attr_accessor :design_doc, :design_doc_slug_cache, :design_doc_fresh + + def design_doc + @design_doc ||= Design.new(default_design_doc) + end # Define a CouchDB view. The name of the view will be the concatenation # of by and the keys joined by _and_ @@ -77,7 +81,6 @@ module CouchRest # spec/core/model_spec.rb. def view_by(*keys) - self.design_doc ||= Design.new(default_design_doc) opts = keys.pop if keys.last.is_a?(Hash) opts ||= {} ducktype = opts.delete(:ducktype) diff --git a/spec/couchrest/more/extended_doc_subclass_spec.rb b/spec/couchrest/more/extended_doc_subclass_spec.rb index a864d7b..ef31a7a 100644 --- a/spec/couchrest/more/extended_doc_subclass_spec.rb +++ b/spec/couchrest/more/extended_doc_subclass_spec.rb @@ -14,7 +14,10 @@ class DesignBusinessCard < BusinessCard property :bg_color, :default => '#eee' end -class OnlineCourse < Course; end +class OnlineCourse < Course + property :url + view_by :url +end class Animal < CouchRest::ExtendedDocument use_database TEST_SERVER.default_database @@ -71,12 +74,25 @@ describe "Subclassing an ExtendedDocument" do OnlineCourse.design_doc_slug.should =~ /^OnlineCourse/ end - it "should have a it's own design_doc_fresh" do + it "should have its own design_doc_fresh" do Animal.refresh_design_doc Dog.design_doc_fresh.should_not == true Dog.refresh_design_doc Dog.design_doc_fresh.should == true end + it "should not add views to the parent's design_doc" do + Course.design_doc['views'].keys.should_not include('by_url') + end + + it "should not add the parent's views to its design doc" do + Course.refresh_design_doc + OnlineCourse.refresh_design_doc + OnlineCourse.design_doc['views'].keys.should_not include('by_title') + end + + it "should have an all view with a guard clause for couchrest-type == subclass name in the map function" do + OnlineCourse.design_doc['views']['all']['map'].should =~ /if \(doc\['couchrest-type'\] == 'OnlineCourse'\)/ + end end