Module: Multitenant::IsMultitenant::ClassMethods
- Defined in:
- lib/is_multitenant.rb
Instance Method Summary collapse
-
#is_multitenant(options = {}) ⇒ Object
When invoked from an ActiveRecord model class,
is_multitenant
specifies that the model’s database operations always be scoped to the application request’s current tenant. - #is_multitenant? ⇒ Boolean
Instance Method Details
#is_multitenant(options = {}) ⇒ Object
When invoked from an ActiveRecord model class, is_multitenant
specifies that the model’s database operations always be scoped to the application request’s current tenant. Usage is as follows:
class Contact < ActiveRecord::Base
is_multitenant :with_attribute => :account_id
-OR-
is_multitenant :class => :account
-OR-
is_multitenant :class => :account, :with_attribute => :account_key
end
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/is_multitenant.rb', line 72 def is_multitenant( = {}) raise 'Cannot use is_multitenant more than once per model class' if is_multitenant? cattr_accessor :tenant_class_name cattr_accessor :tenant_attribute self.tenant_attribute = [:with_attribute] raise ArgumentError, "Must specify :class and/or :with_attribute" if [:class].blank? && self.tenant_attribute.blank? class_name = self.tenant_attribute.to_s.ends_with?('_id') ? self.tenant_attribute.to_s.sub('_id','').underscore.to_sym : nil self.tenant_class_name = ([:class] || class_name).to_s.classify self.tenant_attribute = self.tenant_class_name.foreign_key.to_sym if self.tenant_attribute.nil? extend IsMultitenant::SingletonMethods include IsMultitenant::InstanceMethods inject_scope :multitenant, :for_current_tenant, :apply_to => :all scope :for_current_tenant, ->() { where(tenant_condition) } scope :for_tenant, ->(tenant_id) { where(tenant_condition(tenant_id)) } validate :associations_have_same_tenant before_validation :force_current_tenant_id before_save :force_current_tenant_id define_tenant_id_writer(tenant_attribute) end |
#is_multitenant? ⇒ Boolean
100 101 102 |
# File 'lib/is_multitenant.rb', line 100 def is_multitenant? false end |