Compare commits

...

11 Commits

Author SHA1 Message Date
Sam Lown 819ddb7643 Merge pull request #132 from pezra/improve-auto-update-design-doc-flag-suppport
Improve auto update design doc flag suppport
2011-12-02 07:15:27 -08:00
Peter Williams 447b11a397 Improved auto_update_design_doc handling. 2011-12-01 09:19:15 -07:00
Peter Williams 88bb413ec2 Merge remote branch 'refs/remotes/canonical/master' into design-mapper-more-auto-update-design-doc-aware 2011-11-29 08:22:30 -07:00
Peter Williams 39c60d77d2 Used stored design document if auto_update_design_doc is disabled 2011-11-28 16:47:13 -07:00
Peter Williams f2c16144b0 #view method works when auto_update_design_doc is disabled 2011-11-16 15:30:46 -07:00
Marcos Tapajos 1c695f58bf update readme 2011-08-21 02:02:56 -03:00
Marcos Tapajos 2f00599209 added rake to gemfile 2011-08-21 01:59:48 -03:00
Marcos Tapajós 29de79290f Merge pull request #107 from Burgestrand/base-respond_to
Add CouchRest::Model::Base.respond_to_missing? and respond_to?
2011-08-20 21:58:11 -07:00
Marcos Tapajós 44e09f6083 Merge pull request #108 from Burgestrand/fix-specs
Tell contextual validations specs which database to use
2011-08-20 21:55:08 -07:00
Kim Burgestrand 465c0681e2 Tell contextual validations specs which database to use 2011-07-31 04:44:35 +02:00
Kim Burgestrand 72e3ac37d6 Add CouchRest::Model::Base.respond_to_missing? and respond_to? 2011-07-31 04:40:31 +02:00
10 changed files with 139 additions and 40 deletions

View File

@ -30,6 +30,7 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<rspec>, "~> 2.6.0")
s.add_development_dependency(%q<json>, ["~> 1.5.1"])
s.add_development_dependency(%q<rack-test>, ">= 0.5.7")
s.add_development_dependency("rake", ">= 0.8.0")
# s.add_development_dependency("jruby-openssl", ">= 0.7.3")
end

View File

@ -1,5 +1,9 @@
# CouchRest Model Change History
## 1.1.3
* CouchRest::Model::Base.respond_to_missing? and respond_to? (Kim Burgestrand)
## 1.1.2 - 2011-07-23
* Minor fixes

View File

@ -82,6 +82,21 @@ module CouchRest
super
end
# compatbility for 1.8, it does not use respond_to_missing?
# thing is, when using it like this only, doing method(:find_by_view)
# will throw an error
def self.respond_to?(m, include_private = false)
super || respond_to_missing?(m, include_private)
end
# ruby 1.9 feature
# this allows ruby to know that the method is defined using
# method_missing, and as such, method(:find_by_view) will actually
# give a Method back, and not throw an error like in 1.8!
def self.respond_to_missing?(m, include_private = false)
has_view?(m) || has_view?(m.to_s[/^find_(by_.+)/, 1])
end
def to_key
new? ? nil : [id]
end

View File

@ -7,7 +7,11 @@ module CouchRest
module ClassMethods
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
def design_doc_id
@ -75,16 +79,25 @@ module CouchRest
# If auto updates enabled, check checksum cache
return design_doc if auto_update_design_doc && design_doc_cache_checksum(db) == checksum
# Load up the stored doc (if present), update, and save
saved = stored_design_doc(db)
if saved
if force || saved['couchrest-hash'] != checksum
saved.merge!(design_doc)
db.save_doc(saved)
retries = 1
begin
# Load up the stored doc (if present), update, and save
saved = stored_design_doc(db)
if 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
else
db.save_doc(design_doc)
design_doc.delete('_rev') # Prevent conflicts, never store rev as DB specific
rescue RestClient::Conflict
# if we get a conflict retry the operation...
raise if retries < 1
retries -= 1
retry
end
# Ensure checksum cached for next attempt if using auto updates

View File

@ -58,10 +58,11 @@ module CouchRest
self.model = model
end
# Define a view and generate a method that will provide a new
# View instance when requested.
# Generate a method that will provide a new View instance when
# requested. This will also define the view in CouchDB unless
# auto_update_design_doc is disabled.
def view(name, opts = {})
View.create(model, name, opts)
View.create(model, name, opts) if model.auto_update_design_doc
create_view_method(name)
end

View File

@ -72,9 +72,12 @@ module CouchRest
# <tt>spec/couchrest/more/extended_doc_spec.rb</tt>.
def view_by(*keys)
return unless auto_update_design_doc
opts = keys.pop if keys.last.is_a?(Hash)
opts ||= {}
ducktype = opts.delete(:ducktype)
unless ducktype || opts[:map]
opts[:guards] ||= []
opts[:guards].push "(doc['#{model_type_key}'] == '#{self.to_s}')"

View File

@ -85,11 +85,13 @@ end
# Following two fixture classes have __intentionally__ diffent syntax for setting the validation context
class WithContextualValidationOnCreate < CouchRest::Model::Base
use_database TEST_SERVER.default_database
property(:name, String)
validates(:name, :presence => {:on => :create})
end
class WithContextualValidationOnUpdate < CouchRest::Model::Base
use_database TEST_SERVER.default_database
property(:name, String)
validates(:name, :presence => true, :on => :update)
end

View File

@ -159,39 +159,68 @@ describe CouchRest::Model::DesignDoc do
end
describe "when auto_update_design_doc false" do
before :all do
Article.auto_update_design_doc = false
Article.save_design_doc!
end
# We really do need a new class for each of example. If we try
# to use the same class the examples interact with each in ways
# that can hide failures because the design document gets cached
# at the class level.
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
Article.auto_update_design_doc = true
end
eval <<-KLASS
class ::#{class_name} < CouchRest::Model::Base
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
Article.should_not_receive(:stored_design_doc)
Article.by_date
end
class_name.constantize
}
it "will not update stored design doc if view changed" do
Article.by_date
orig = Article.stored_design_doc
design = Article.design_doc
view = design['views']['by_date']['map']
design['views']['by_date']['map'] = view + ' '
Article.by_date
Article.stored_design_doc['_rev'].should eql(orig['_rev'])
model_class.by_name
orig = model_class.stored_design_doc
design = model_class.design_doc
view = design['views']['by_name']['map']
design['views']['by_name']['map'] = view + ' '
model_class.by_name
model_class.stored_design_doc['_rev'].should eql(orig['_rev'])
end
it "will update stored design if forced" do
Article.by_date
orig = Article.stored_design_doc
design = Article.design_doc
view = design['views']['by_date']['map']
design['views']['by_date']['map'] = view + ' '
Article.save_design_doc!
Article.stored_design_doc['_rev'].should_not eql(orig['_rev'])
model_class.by_name
orig = model_class.stored_design_doc
design = model_class.design_doc
view = design['views']['by_name']['map']
design['views']['by_name']['map'] = view + ' '
model_class.save_design_doc!
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

View File

@ -83,7 +83,23 @@ describe CouchRest::Model::Designs do
@object.should_receive(:create_view_method).with('test')
@object.view('test')
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
describe "#filter" do

View File

@ -173,7 +173,22 @@ describe CouchRest::Model::Views do
end
end
describe "#method_missing for find_by methods" do
before(:all) { reset_test_db! }
specify { Course.should respond_to :find_by_title_and_active }
specify { Course.should respond_to :by_title }
specify "#method should work in ruby 1.9, but not 1.8" do
if RUBY_VERSION >= "1.9"
Course.method(:find_by_title_and_active).should be_a Method
else
expect { Course.method(:find_by_title_and_active) }.to raise_error(NameError)
end
end
end
describe "a ducktype view" do
before(:all) do
reset_test_db!