From f27a69b323a82824c2d3c6e9201b3835c0a32d57 Mon Sep 17 00:00:00 2001 From: James Bracy Date: Mon, 29 Dec 2008 00:35:00 -0500 Subject: added the ability to change the "couchrest-type" field --- lib/couchrest/core/model.rb | 39 +++++++++++++++++++++++++++--------- spec/couchrest/core/model_spec.rb | 18 +++++++++++++++- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb index 0fccb27..3720f28 100644 --- a/lib/couchrest/core/model.rb +++ b/lib/couchrest/core/model.rb @@ -27,7 +27,7 @@ module CouchRest # view_by :tags, # :map => # "function(doc) { - # if (doc['couchrest-type'] == 'Article' && doc.tags) { + # if (doc['couchrest_type'] == 'Article' && doc.tags) { # doc.tags.forEach(function(tag){ # emit(tag, 1); # }); @@ -76,7 +76,7 @@ module CouchRest apply_defaults cast_keys unless self['_id'] && self['_rev'] - self['couchrest-type'] = self.class.to_s + self[inheritance_field] = self.class.to_s end end @@ -90,6 +90,7 @@ module CouchRest class_inheritable_accessor :design_doc class_inheritable_accessor :design_doc_slug_cache class_inheritable_accessor :design_doc_fresh + class_inheritable_accessor :inheritance_field class << self # override the CouchRest::Model-wide default_database @@ -101,6 +102,24 @@ module CouchRest def database self.class_database || CouchRest::Model.default_database end + + # override the default inheritance field ('couchrest_type') + # Example: + # + # class Post < CouchRest::Model + # + # set_inheritance_field "document_type" + # + # end + # + def set_inheritance_field(field) + @inheritance_field = field + end + alias :inheritance_field= :set_inheritance_field + + def inheritance_field + @inheritance_field ||= "couchrest_type".freeze + end # Load a document from the database by id def get id @@ -108,9 +127,9 @@ module CouchRest new(doc) end - # Load all documents that have the "couchrest-type" field equal to the - # name of the current class. Take the standard set of - # CouchRest::Database#view options. + # Load all documents that have the inheritance field (default + # "couchrest_type") field equal to the name of the current class. + # Take the standard set of CouchRest::Database#view options. def all opts = {}, &block self.design_doc ||= Design.new(default_design_doc) unless design_doc_fresh @@ -119,7 +138,7 @@ module CouchRest view :all, opts, &block end - # Load the first document that have the "couchrest-type" field equal to + # Load the first document that have the inheritance field equal to # the name of the current class. # # ==== Returns @@ -263,7 +282,7 @@ module CouchRest # view_by :tags, # :map => # "function(doc) { - # if (doc['couchrest-type'] == 'Post' && doc.tags) { + # if (doc['couchrest_type'] == 'Post' && doc.tags) { # doc.tags.forEach(function(tag){ # emit(doc.tag, 1); # }); @@ -279,7 +298,7 @@ module CouchRest # function: # # function(doc) { - # if (doc['couchrest-type'] == 'Post' && doc.date) { + # if (doc['couchrest_type'] == 'Post' && doc.date) { # emit(doc.date, null); # } # } @@ -314,7 +333,7 @@ module CouchRest ducktype = opts.delete(:ducktype) unless ducktype || opts[:map] opts[:guards] ||= [] - opts[:guards].push "(doc['couchrest-type'] == '#{self.to_s}')" + opts[:guards].push "(doc['#{inheritance_field}'] == '#{self.to_s}')" end keys.push opts self.design_doc.view_by(*keys) @@ -422,7 +441,7 @@ module CouchRest "views" => { 'all' => { 'map' => "function(doc) { - if (doc['couchrest-type'] == '#{self.to_s}') { + if (doc['#{inheritance_field}'] == '#{self.to_s}') { emit(null,null); } }" diff --git a/spec/couchrest/core/model_spec.rb b/spec/couchrest/core/model_spec.rb index 466b850..93cf24b 100644 --- a/spec/couchrest/core/model_spec.rb +++ b/spec/couchrest/core/model_spec.rb @@ -13,6 +13,10 @@ class BasicWithValidation < CouchRest::Model end end +class BasicWithoutDefaults < CouchRest::Model + set_inheritance_field "type" +end + class WithTemplateAndUniqueID < CouchRest::Model unique_id do |model| model['important-field'] @@ -54,7 +58,7 @@ class Article < CouchRest::Model view_by :tags, :map => "function(doc) { - if (doc['couchrest-type'] == 'Article' && doc.tags) { + if (doc['couchrest_type'] == 'Article' && doc.tags) { doc.tags.forEach(function(tag){ emit(tag, 1); }); @@ -114,6 +118,16 @@ describe CouchRest::Model do Basic.database.info['db_name'].should == 'couchrest-test' end + it 'should use the default inheritance field' do + Basic.inheritance_field.should == "couchrest_type" + end + + it 'should override the default inheritance field' do + @obj = BasicWithoutDefaults.new + @obj.save.should == true + @obj['type'].should == BasicWithoutDefaults.to_s + end + it "should override the default db" do Article.database.info['db_name'].should == 'couchrest-model-test' end @@ -337,7 +351,7 @@ describe CouchRest::Model do end it "should set the type" do - @obj['couchrest-type'].should == 'Basic' + @obj['couchrest_type'].should == 'Basic' end end -- 1.6.0