Permalink
Browse files

Refactor MultiTenant to MultiTenantModel.

  • Loading branch information...
mconnell committed Jun 19, 2010
1 parent 17ece38 commit 91f5a60096a48885e802aad39c18731e376a15b9
Showing with 41 additions and 47 deletions.
  1. +1 −1 init.rb
  2. +0 −43 lib/multi_tenant.rb
  3. +37 −0 lib/multi_tenant_model.rb
  4. +1 −1 test/{multi_tenant_test.rb → multi_tenant_model_test.rb}
  5. +2 −2 test/test_helper.rb
View
@@ -1 +1 @@
-ActiveRecord::Base.send :include, MultiTenant
+ActiveRecord::Base.extend MultiTenantModel
View
@@ -1,43 +0,0 @@
-module MultiTenant
- def self.included(base)
- base.extend(ClassMethods)
- end
-
- module ClassMethods
- def multi_tenant_model
- self.class_eval do
- cattr_accessor :current
- end
- end
-
- def belongs_to_tenant(tenant = :account)
- self.class_eval do
- cattr_accessor :tenant, :tenant_class
- end
-
- self.tenant ||= tenant
- self.tenant_class ||= tenant.to_s.capitalize.constantize
-
- self.class_eval do
- belongs_to tenant
-
- def self.unscoped
- if tenant_class.current
- super.apply_finder_options(:conditions => { "#{tenant}_id" => tenant_class.current.id })
- else
- super
- end
- end
-
- def initialize(attributes = nil)
- if tenant_class.current
- new_attributes = {"#{tenant}_id" => tenant_class.current.id }
- new_attributes.merge!(attributes) if attributes.is_a?(Hash)
- attributes = new_attributes
- end
- super(attributes)
- end
- end
- end
- end
-end
View
@@ -0,0 +1,37 @@
+module MultiTenantModel
+ def multi_tenant_model
+ self.class_eval do
+ cattr_accessor :current
+ end
+ end
+
+ def belongs_to_tenant(tenant = :account)
+ self.class_eval do
+ cattr_accessor :tenant, :tenant_class
+ end
+
+ self.tenant ||= tenant
+ self.tenant_class ||= tenant.to_s.capitalize.constantize
+
+ self.class_eval do
+ belongs_to tenant
+
+ def self.unscoped
+ if tenant_class.current
+ super.apply_finder_options(:conditions => { "#{tenant}_id" => tenant_class.current.id })
+ else
+ super
+ end
+ end
+
+ def initialize(attributes = nil)
+ if tenant_class.current
+ new_attributes = {"#{tenant}_id" => tenant_class.current.id }
+ new_attributes.merge!(attributes) if attributes.is_a?(Hash)
+ attributes = new_attributes
+ end
+ super(attributes)
+ end
+ end
+ end
+end
@@ -1,6 +1,6 @@
require 'test_helper'
-class MultiTenantTest < ActiveSupport::TestCase
+class MultiTenantModelTest < ActiveSupport::TestCase
def setup
acme_account = Account.create(:name => 'Acme Housing Ltd')
View
@@ -10,8 +10,8 @@
)
require 'db/load_schema'
-require 'multi_tenant'
-ActiveRecord::Base.send :include, MultiTenant
+require 'multi_tenant_model'
+ActiveRecord::Base.extend MultiTenantModel
require 'models/account'
require 'models/property'

0 comments on commit 91f5a60

Please sign in to comment.