Permalink
Browse files

Moved require_tenant config option to an initializer.

  • Loading branch information...
ErwinM committed Feb 11, 2013
1 parent d83d72d commit fbd28aaea4e4ec6441e183eee6bfddd215359b5a
View
@@ -66,9 +66,9 @@ any code in this block will be scoped to the current tenant. All methods that se
**note:** If the current tenant is not set by one of these methods, Acts_as_tenant will be unable to apply the proper scope to your models. So make sure you use one of the two methods to tell acts_as_tenant about the current tenant.
-**Requiring tenant to be set**
+**Require tenant to be set always**
-If you want to require the tenant to be set at all times, include ```require_tenant``` in your ApplicationController. Acts_as_tenant will then raise an exeception if a query is made without a tenant available.
+If you want to require the tenant to be set at all times, you can configure acts_as_tenant to raise an error when a query is made without a tenant available. See below under configuarion options.
Scoping your models
-------------------
@@ -115,6 +115,19 @@ If you need to validate for uniqueness, chances are that you want to scope this
All options available to Rails' own `validates_uniqueness_of` are also available to this method.
+Configuration options
+---------------------
+An initializer can be created to control (currently one) option in ActsAsTenant. Defaults
+are shown below with sample overrides following. In `config/initializer/acts_as_tenant.rb`:
+
+```ruby
+ActsAsTenant.configure do |config|
+ config.require_tenant = false # true
+end
+```
+
+* `config.require_tenant` when set to true will raise an ActsAsTenant::NoTenant error whenever a query is made without a tenant set.
+
Note on testing
---------------
Whenever you set the `current_tenant` in your tests, either through integration tests or directly by calling `ActsAsTenant.current_tenant = some_tenant`, make sure to clean up the tenant after each test by calling `ActsAsTenant.current_tenant = nil`.
View
@@ -8,9 +8,9 @@
require "acts_as_tenant"
require "acts_as_tenant/version"
-require "acts_as_tenant/exceptions"
-require "acts_as_tenant/controller_extensions.rb"
-require "acts_as_tenant/model_extensions.rb"
+require "acts_as_tenant/configuration"
+require "acts_as_tenant/controller_extensions"
+require "acts_as_tenant/model_extensions"
#$LOAD_PATH.shift
@@ -19,5 +19,6 @@
ActionController::Base.extend ActsAsTenant::ControllerExtensions
end
-
+module ActsAsTenant
+end
@@ -0,0 +1,26 @@
+module ActsAsTenant
+ @@configuration = nil
+
+ def self.configure
+ @@configuration = Configuration.new
+
+ if block_given?
+ yield configuration
+ end
+
+ configuration
+ end
+
+ def self.configuration
+ @@configuration || configure
+ end
+
+ class Configuration
+ attr_accessor :require_tenant
+
+ def require_tenant
+ @require_tenant ||= false
+ end
+
+ end
+end
@@ -32,14 +32,6 @@ def self.with_tenant(tenant, &block)
self.current_tenant= old_tenant
return value
end
-
- def self.tenant_required?
- Thread.current[:tenant_required]
- end
-
- def self.require_tenant
- Thread.current[:tenant_required] = true
- end
module ModelExtensions
def self.included(base)
@@ -52,7 +44,7 @@ def acts_as_tenant(association = :account)
ActsAsTenant.set_tenant_klass(association)
default_scope lambda {
- if ActsAsTenant.tenant_required? && ActsAsTenant.current_tenant.nil?
+ if ActsAsTenant.configuration.require_tenant && ActsAsTenant.current_tenant.nil?
raise "No tenant found, while tenant_required is set to true [ActsAsTenant]"
end
where({ActsAsTenant.fkey => ActsAsTenant.current_tenant.id}) if ActsAsTenant.current_tenant
@@ -103,6 +95,7 @@ def validates_uniqueness_to_tenant(fields, args ={})
else
args[:scope] = tenant_id
end
+
validates_uniqueness_of(fields, args)
end
end
@@ -0,0 +1,28 @@
+require 'spec_helper'
+
+describe ActsAsTenant::Configuration do
+ describe 'no configuration given' do
+ before do
+ ActsAsTenant.configure
+ end
+
+ it 'provides defaults' do
+ ActsAsTenant.configuration.require_tenant.should_not be_true
+ end
+ end
+
+ describe 'with config block' do
+ after do
+ ActsAsTenant.configure
+ end
+
+ it 'stores config' do
+ ActsAsTenant.configure do |config|
+ config.require_tenant = true
+ end
+
+ ActsAsTenant.configuration.require_tenant.should be_true
+ end
+
+ end
+end
Oops, something went wrong.

0 comments on commit fbd28aa

Please sign in to comment.