Class: Anoubis::Tenant::Tenant
- Inherits:
-
Core::ApplicationRecord
- Object
- ActiveRecord::Base
- Core::ApplicationRecord
- Anoubis::Tenant::Tenant
- Defined in:
- app/models/anoubis/tenant/tenant.rb
Overview
Tenant model. Stores information about all portal’s tenants.
Constant Summary collapse
- VALID_TENANT_REGEX =
Tenant’s identifier consists of lowercase alphabetic symbols.
/[a-z]\z/i
Instance Attribute Summary collapse
-
#ident ⇒ String
The tenant’s identifier.
-
#state ⇒ 'standard', 'default'
The tenant’s status.
-
#title ⇒ String
The tenant’s title.
Attributes inherited from Core::ApplicationRecord
#can_delete, #can_edit, #can_new, #created_at, #current_user, #need_refresh, #redis, #sys_title, #updated_at
Instance Method Summary collapse
-
#after_create_tenant ⇒ Object
Is called after new tenant was created.
-
#after_update_tenant ⇒ Object
Is called after tenant had been updated.
-
#before_destroy_tenant ⇒ Object
Is called before tenant will be deleted from database.
-
#before_save_tenant ⇒ Object
Is called before tenant will be stored in database.
-
#before_validation_tenant_create ⇒ Object
Is called before validation when new tenant is being created.
-
#before_validation_tenant_update ⇒ Object
Is called before validation when tenant is being updated.
-
#update_users_login(id, ident) ⇒ Object
Changes every User#login value based on #ident value.
Methods inherited from Core::ApplicationRecord
#after_initialize_core_anubis_model, #can_destroy?, #current_locale, #current_locale=, #default_locale, #get_locale, #get_locale_field, get_where, #is_field_localized, #new_uuid, redis, #redis_prefix, redis_prefix, #set_locale_field
Instance Attribute Details
#ident ⇒ String
Returns the tenant’s identifier. Identifier consists of lowercase alphabetical symbols.
19 |
# File 'app/models/anoubis/tenant/tenant.rb', line 19 validates :ident, length: { minimum: 3, maximum: 10 }, uniqueness: true, format: { with: VALID_TENANT_REGEX } |
#state ⇒ 'standard', 'default'
Returns the tenant’s status.
-
‘default’ — tenant is marked as default (only one tenant may be default)
-
‘standard’ — tenant is marked as standard.
29 |
# File 'app/models/anoubis/tenant/tenant.rb', line 29 enum state: { standard: 0, default: 1 } |
#title ⇒ String
Returns the tenant’s title.
23 |
# File 'app/models/anoubis/tenant/tenant.rb', line 23 validates :title, length: { minimum: 5, maximum: 100 }, uniqueness: true |
Instance Method Details
#after_create_tenant ⇒ Object
Is called after new tenant was created. Adds access for this tenant to main system with id 1.
56 57 58 |
# File 'app/models/anoubis/tenant/tenant.rb', line 56 def after_create_tenant Anoubis::Tenant::TenantSystem.find_or_create_by(tenant_id: self.id, system_id: 1) if self.id != 1 end |
#after_update_tenant ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/anoubis/tenant/tenant.rb', line 65 def after_update_tenant if self.ident_was != self.ident self.update_users_login self.id, self.ident end # If current tenant had been set as 'default', then sets all other tenants as 'standard'. if self.state != self.state_was && self.state = 'default' Anoubis::Tenant::Tenant.where(state: Anoubis::Tenant::Tenant.states[:default]).update_all(state: Anoubis::Tenant::Tenant.states[:standard]) end end |
#before_destroy_tenant ⇒ Object
Is called before tenant will be deleted from database. Checks the ability to destroy a tenant.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/anoubis/tenant/tenant.rb', line 84 def before_destroy_tenant if self.id == 1 errors.add(:base, I18n.t('anubis.tenants.errors.cant_destroy')) throw(:abort, __method__) end if !can_destroy? errors.add(:base, I18n.t('anubis.tenants.errors.has_childs')) throw(:abort, __method__) end end |
#before_save_tenant ⇒ Object
Is called before tenant will be stored in database. Changes #ident value to lower case.
78 79 80 |
# File 'app/models/anoubis/tenant/tenant.rb', line 78 def before_save_tenant self.ident = self.ident.downcase if self.ident end |
#before_validation_tenant_create ⇒ Object
Is called before validation when new tenant is being created. If it’s a first tenant then it sets #ident value to ‘sys’
36 37 38 39 40 41 42 43 |
# File 'app/models/anoubis/tenant/tenant.rb', line 36 def before_validation_tenant_create if self.id if self.id == 1 self.ident = 'sys' return true end end end |
#before_validation_tenant_update ⇒ Object
Is called before validation when tenant is being updated. Prevents the changing #ident value.
47 48 49 50 51 52 |
# File 'app/models/anoubis/tenant/tenant.rb', line 47 def before_validation_tenant_update if self.id == 1 && self.ident != self.ident_was errors.add(:ident, I18n.t('anubis.tenants.errors.cant_change_ident')) throw(:abort, __method__) end end |
#update_users_login(id, ident) ⇒ Object
Changes every User#login value based on #ident value. Procedure makes direct update in database and doesn’t call any callbacks of User model.
101 102 103 104 105 106 |
# File 'app/models/anoubis/tenant/tenant.rb', line 101 def update_users_login (id, ident) query = " UPDATE users SET users.login = CONCAT(users.email, '.\#{ident}') WHERE users.tenant_id = \#{id}\n SQL\n Anoubis::Tenant::User.connection.execute query\nend\n" |