Module: Multitenant::IsMultitenant::InstanceMethods
- Defined in:
- lib/is_multitenant.rb
Overview
All instances of is_multitenant models have access to the following methods.
Class Method Summary collapse
Instance Method Summary collapse
- #associations_have_same_tenant ⇒ Object
- #force_current_tenant_id ⇒ Object
- #set_tenant_id(tenant_id = nil) ⇒ Object
Class Method Details
.included(base) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/is_multitenant.rb', line 141 def self.included(base) protected def set_tenant_id(tenant_id = nil) write_attribute(self.class.tenant_attribute, tenant_id || self.class.current_tenant_id) end def force_current_tenant_id set_tenant_id unless self.class.without_multitenant_scope? end def associations_have_same_tenant self.class.reflect_on_all_associations(:belongs_to).each do |assoc| if assoc.klass.is_multitenant? && assoc.class_name != self.tenant_class_name value = send(assoc.foreign_key) errors.add assoc.foreign_key, "multitenant association #{assoc.name} has different tenant" unless value.nil? || assoc.klass.where(:id => value).exists? end end end end |
Instance Method Details
#associations_have_same_tenant ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/is_multitenant.rb', line 153 def associations_have_same_tenant self.class.reflect_on_all_associations(:belongs_to).each do |assoc| if assoc.klass.is_multitenant? && assoc.class_name != self.tenant_class_name value = send(assoc.foreign_key) errors.add assoc.foreign_key, "multitenant association #{assoc.name} has different tenant" unless value.nil? || assoc.klass.where(:id => value).exists? end end end |
#force_current_tenant_id ⇒ Object
149 150 151 |
# File 'lib/is_multitenant.rb', line 149 def force_current_tenant_id set_tenant_id unless self.class.without_multitenant_scope? end |
#set_tenant_id(tenant_id = nil) ⇒ Object
145 146 147 |
# File 'lib/is_multitenant.rb', line 145 def set_tenant_id(tenant_id = nil) write_attribute(self.class.tenant_attribute, tenant_id || self.class.current_tenant_id) end |