From da3e524020c1ef06a31497d46c67c9f00bb0c6ff Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Sat, 30 Apr 2011 01:04:04 +0200 Subject: [PATCH] Fixing issues with proxying and new database code --- lib/couchrest/model/proxyable.rb | 10 +++++++--- spec/couchrest/proxyable_spec.rb | 33 +++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/lib/couchrest/model/proxyable.rb b/lib/couchrest/model/proxyable.rb index a3fa6cd..53a88da 100644 --- a/lib/couchrest/model/proxyable.rb +++ b/lib/couchrest/model/proxyable.rb @@ -42,8 +42,10 @@ module CouchRest # Define the name of a method to call to determine the name of # the database to use as a proxy. - def proxy_database_method=(name); @proxy_database_method = name; end - def proxy_database_method; @proxy_database_method; end + def proxy_database_method(name = nil) + @proxy_database_method = name if name + @proxy_database_method + end private @@ -51,7 +53,9 @@ module CouchRest # by overwriting it to provide a basic accessor. def overwrite_database_reader(model_name) class_eval <<-EOS, __FILE__, __LINE__ + 1 - def database; @database; end + def self.database + raise StandardError, "#{self.to_s} database must be accessed via '#{model_name}' proxy" + end EOS end diff --git a/spec/couchrest/proxyable_spec.rb b/spec/couchrest/proxyable_spec.rb index dfa2326..7e0c508 100644 --- a/spec/couchrest/proxyable_spec.rb +++ b/spec/couchrest/proxyable_spec.rb @@ -3,8 +3,9 @@ require File.expand_path("../../spec_helper", __FILE__) require File.join(FIXTURE_PATH, 'more', 'cat') class DummyProxyable < CouchRest::Model::Base - def proxy_database - 'db' # Do not use this! + proxy_database_method :db + def db + 'db' end end @@ -41,11 +42,11 @@ describe "Proxyable" do describe "class methods" do - before(:each) do - @class = DummyProxyable.clone - end describe ".proxy_owner_method" do + before(:each) do + @class = DummyProxyable.clone + end it "should provide proxy_owner_method accessors" do @class.should respond_to(:proxy_owner_method) @class.should respond_to(:proxy_owner_method=) @@ -56,7 +57,20 @@ describe "Proxyable" do end end + describe ".proxy_database_method" do + before do + @class = Class.new(CouchRest::Model::Base) + end + it "should be possible to set the proxy database method" do + @class.proxy_database_method :db + @class.proxy_database_method.should eql(:db) + end + end + describe ".proxy_for" do + before(:each) do + @class = DummyProxyable.clone + end it "should be provided" do @class.should respond_to(:proxy_for) @@ -91,6 +105,10 @@ describe "Proxyable" do end describe ".proxied_by" do + before do + @class = Class.new(CouchRest::Model::Base) + end + it "should be provided" do @class.should respond_to(:proxied_by) end @@ -118,6 +136,11 @@ describe "Proxyable" do @class.proxied_by(:department) lambda { @class.proxied_by(:company) }.should raise_error end + + it "should overwrite the database method to provide an error" do + @class.proxied_by(:company) + lambda { @class.database }.should raise_error(StandardError, /database must be accessed via/) + end end end