Merge pull request #132 from pezra/improve-auto-update-design-doc-flag-suppport
Improve auto update design doc flag suppport
This commit is contained in:
commit
819ddb7643
|
@ -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 || ::CouchRest::Design.new(default_design_doc)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def design_doc_id
|
def design_doc_id
|
||||||
|
@ -75,16 +79,25 @@ module CouchRest
|
||||||
# If auto updates enabled, check checksum cache
|
# If auto updates enabled, check checksum cache
|
||||||
return design_doc if auto_update_design_doc && design_doc_cache_checksum(db) == checksum
|
return design_doc if auto_update_design_doc && design_doc_cache_checksum(db) == checksum
|
||||||
|
|
||||||
# Load up the stored doc (if present), update, and save
|
retries = 1
|
||||||
saved = stored_design_doc(db)
|
begin
|
||||||
if saved
|
# Load up the stored doc (if present), update, and save
|
||||||
if force || saved['couchrest-hash'] != checksum
|
saved = stored_design_doc(db)
|
||||||
saved.merge!(design_doc)
|
if saved
|
||||||
db.save_doc(saved)
|
if force || saved['couchrest-hash'] != checksum
|
||||||
|
saved.merge!(design_doc)
|
||||||
|
db.save_doc(saved)
|
||||||
|
@design_doc = saved # update memo to point to the document we actually saved
|
||||||
|
end
|
||||||
|
else
|
||||||
|
design_doc.delete('_rev') # This is a new document and so doesn't have a revision yet
|
||||||
|
db.save_doc(design_doc)
|
||||||
end
|
end
|
||||||
else
|
rescue RestClient::Conflict
|
||||||
db.save_doc(design_doc)
|
# if we get a conflict retry the operation...
|
||||||
design_doc.delete('_rev') # Prevent conflicts, never store rev as DB specific
|
raise if retries < 1
|
||||||
|
retries -= 1
|
||||||
|
retry
|
||||||
end
|
end
|
||||||
|
|
||||||
# Ensure checksum cached for next attempt if using auto updates
|
# Ensure checksum cached for next attempt if using auto updates
|
||||||
|
|
|
@ -58,10 +58,11 @@ module CouchRest
|
||||||
self.model = model
|
self.model = model
|
||||||
end
|
end
|
||||||
|
|
||||||
# Define a view and generate a method that will provide a new
|
# Generate a method that will provide a new View instance when
|
||||||
# View instance when requested.
|
# requested. This will also define the view in CouchDB unless
|
||||||
|
# auto_update_design_doc is disabled.
|
||||||
def view(name, opts = {})
|
def view(name, opts = {})
|
||||||
View.create(model, name, opts)
|
View.create(model, name, opts) if model.auto_update_design_doc
|
||||||
create_view_method(name)
|
create_view_method(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -72,9 +72,12 @@ module CouchRest
|
||||||
# <tt>spec/couchrest/more/extended_doc_spec.rb</tt>.
|
# <tt>spec/couchrest/more/extended_doc_spec.rb</tt>.
|
||||||
|
|
||||||
def view_by(*keys)
|
def view_by(*keys)
|
||||||
|
return unless auto_update_design_doc
|
||||||
|
|
||||||
opts = keys.pop if keys.last.is_a?(Hash)
|
opts = keys.pop if keys.last.is_a?(Hash)
|
||||||
opts ||= {}
|
opts ||= {}
|
||||||
ducktype = opts.delete(:ducktype)
|
ducktype = opts.delete(:ducktype)
|
||||||
|
|
||||||
unless ducktype || opts[:map]
|
unless ducktype || opts[:map]
|
||||||
opts[:guards] ||= []
|
opts[:guards] ||= []
|
||||||
opts[:guards].push "(doc['#{model_type_key}'] == '#{self.to_s}')"
|
opts[:guards].push "(doc['#{model_type_key}'] == '#{self.to_s}')"
|
||||||
|
|
|
@ -159,39 +159,68 @@ describe CouchRest::Model::DesignDoc 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
|
||||||
|
|
|
@ -83,7 +83,23 @@ describe CouchRest::Model::Designs do
|
||||||
@object.should_receive(:create_view_method).with('test')
|
@object.should_receive(:create_view_method).with('test')
|
||||||
@object.view('test')
|
@object.view('test')
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "for model with auto_update_design_doc disabled " do
|
||||||
|
class ::DesignModelAutoUpdateDesignDocDisabled < ::CouchRest::Model::Base
|
||||||
|
self.auto_update_design_doc = false
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#view" do
|
||||||
|
before :each do
|
||||||
|
@object = @klass.new(DesignModelAutoUpdateDesignDocDisabled)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "does not attempt to create view" do
|
||||||
|
CouchRest::Model::Designs::View.should_not_receive(:create)
|
||||||
|
@object.view('test')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#filter" do
|
describe "#filter" do
|
||||||
|
|
Loading…
Reference in a new issue