Used stored design document if auto_update_design_doc is disabled
This commit is contained in:
parent
f2c16144b0
commit
39c60d77d2
|
@ -7,7 +7,11 @@ module CouchRest
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
|
|
||||||
def design_doc
|
def design_doc
|
||||||
@design_doc ||= ::CouchRest::Design.new(default_design_doc)
|
@design_doc ||= if auto_update_design_doc
|
||||||
|
::CouchRest::Design.new(default_design_doc)
|
||||||
|
else
|
||||||
|
stored_design_doc
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def design_doc_id
|
def design_doc_id
|
||||||
|
|
|
@ -162,39 +162,68 @@ describe "Design Documents" do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "when auto_update_design_doc false" do
|
describe "when auto_update_design_doc false" do
|
||||||
|
# We really do need a new class for each of example. If we try
|
||||||
before :all do
|
# to use the same class the examples interact with each in ways
|
||||||
Article.auto_update_design_doc = false
|
# that can hide failures because the design document gets cached
|
||||||
Article.save_design_doc!
|
# at the class level.
|
||||||
end
|
let(:model_class) {
|
||||||
|
class_name = "#{example.metadata[:full_description].gsub(/\s+/,'_').camelize}Model"
|
||||||
|
doc = CouchRest::Document.new("_id" => "_design/#{class_name}")
|
||||||
|
doc["language"] = "javascript"
|
||||||
|
doc["views"] = {"all" => {"map" =>
|
||||||
|
"function(doc) {
|
||||||
|
if (doc['type'] == 'Article') {
|
||||||
|
emit(doc['_id'],1);
|
||||||
|
}
|
||||||
|
}"},
|
||||||
|
"by_name" => {"map" =>
|
||||||
|
"function(doc) {
|
||||||
|
if ((doc['type'] == '#{class_name}') && (doc['name'] != null)) {
|
||||||
|
emit(doc['name'], null);
|
||||||
|
}",
|
||||||
|
"reduce" =>
|
||||||
|
"function(keys, values, rereduce) {
|
||||||
|
return sum(values);
|
||||||
|
}"}}
|
||||||
|
|
||||||
|
DB.save_doc doc
|
||||||
|
|
||||||
after :all do
|
eval <<-KLASS
|
||||||
Article.auto_update_design_doc = true
|
class ::#{class_name} < CouchRest::Model::Base
|
||||||
end
|
use_database DB
|
||||||
|
self.auto_update_design_doc = false
|
||||||
|
design do
|
||||||
|
view :by_name
|
||||||
|
end
|
||||||
|
property :name, String
|
||||||
|
end
|
||||||
|
KLASS
|
||||||
|
|
||||||
it "will not send a request for the saved design doc" do
|
class_name.constantize
|
||||||
Article.should_not_receive(:stored_design_doc)
|
}
|
||||||
Article.by_date
|
|
||||||
end
|
|
||||||
|
|
||||||
it "will not update stored design doc if view changed" do
|
it "will not update stored design doc if view changed" do
|
||||||
Article.by_date
|
model_class.by_name
|
||||||
orig = Article.stored_design_doc
|
orig = model_class.stored_design_doc
|
||||||
design = Article.design_doc
|
design = model_class.design_doc
|
||||||
view = design['views']['by_date']['map']
|
view = design['views']['by_name']['map']
|
||||||
design['views']['by_date']['map'] = view + ' '
|
design['views']['by_name']['map'] = view + ' '
|
||||||
Article.by_date
|
model_class.by_name
|
||||||
Article.stored_design_doc['_rev'].should eql(orig['_rev'])
|
model_class.stored_design_doc['_rev'].should eql(orig['_rev'])
|
||||||
end
|
end
|
||||||
|
|
||||||
it "will update stored design if forced" do
|
it "will update stored design if forced" do
|
||||||
Article.by_date
|
model_class.by_name
|
||||||
orig = Article.stored_design_doc
|
orig = model_class.stored_design_doc
|
||||||
design = Article.design_doc
|
design = model_class.design_doc
|
||||||
view = design['views']['by_date']['map']
|
view = design['views']['by_name']['map']
|
||||||
design['views']['by_date']['map'] = view + ' '
|
design['views']['by_name']['map'] = view + ' '
|
||||||
Article.save_design_doc!
|
model_class.save_design_doc!
|
||||||
Article.stored_design_doc['_rev'].should_not eql(orig['_rev'])
|
model_class.stored_design_doc['_rev'].should_not eql(orig['_rev'])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is able to use predefined views" do
|
||||||
|
model_class.by_name(key: "special").all
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue