Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor MultiTenant to MultiTenantModel.

  • Loading branch information...
commit 91f5a60096a48885e802aad39c18731e376a15b9 1 parent 17ece38
@mconnell authored
View
2  init.rb
@@ -1 +1 @@
-ActiveRecord::Base.send :include, MultiTenant
+ActiveRecord::Base.extend MultiTenantModel
View
43 lib/multi_tenant.rb
@@ -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
37 lib/multi_tenant_model.rb
@@ -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
View
2  test/multi_tenant_test.rb → test/multi_tenant_model_test.rb
@@ -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
4 test/test_helper.rb
@@ -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'
Please sign in to comment.
Something went wrong with that request. Please try again.