Class: Anoubis::Tenant::System

Inherits:
Core::ApplicationRecord show all
Defined in:
app/models/anoubis/tenant/system.rb

Overview

System model. Stores information about all portal’s systems.

Constant Summary collapse

VALID_SYSTEM_REGEX =

System’s identifier consists of lowercase alphabetic symbols.

/[a-z]\z/i

Instance Attribute Summary collapse

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

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

#identString

Returns the system’s identifier. Identifier consists of lowercase alphabetical symbols.

Returns:

  • (String)

    the system’s identifier. Identifier consists of lowercase alphabetical symbols.



19
# File 'app/models/anoubis/tenant/system.rb', line 19

validates :ident, length: { minimum: 3, maximum: 15 }, uniqueness: true, format: { with: VALID_SYSTEM_REGEX }

#titleString

Returns the system’s title. Title loads from Anoubis::Tenant::SystemLocale#title based on Core::ApplicationRecord#current_locale.

Returns:



124
125
126
# File 'app/models/anoubis/tenant/system.rb', line 124

def title
  self.model_locale.title if self.model_locale
end

Instance Method Details

#after_create_systemObject

Is called after new system was created. Creates administration group for new system.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/anoubis/tenant/system.rb', line 50

def after_create_system
  if self.id == 1
    data = Anoubis::Tenant::Group.create(id: 1)
    I18n.available_locales.each do |locale|
      I18n.locale = locale
      Anoubis::Tenant::GroupLocale.find_or_create_by(group_id: data.id, locale: Anoubis::Tenant::MenuLocale.locales[locale.to_s.to_sym]) do |system_locale|
        system_locale.title = I18n.t('anubis.install.admins_group')
      end
    end
  else
    data = Anoubis::Tenant::Group.find_or_create_by(ident: 'admin', system_id: self.id)
    I18n.available_locales.each do |locale|
      I18n.locale = locale
      Anoubis::Tenant::GroupLocale.find_or_create_by(group_id: data.id, locale: Anoubis::Tenant::MenuLocale.locales[locale.to_s.to_sym]) do |system_locale|
        system_locale.title = I18n.t('anubis.install.admins_group')
      end
    end
  end
end

#after_update_systemObject

Is called after system had been updated. If #ident value had been changed then procedure updates every Group#full_ident value.



79
80
81
82
83
# File 'app/models/anoubis/tenant/system.rb', line 79

def after_update_system
  if self.ident_was != self.ident
    update_groups_full_ident self.id, self.ident
  end
end

#before_destroy_systemObject

Is called before system will be deleted from database. Checks the ability to destroy a system. Delete all translations for system model from GroupLocale.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/anoubis/tenant/system.rb', line 88

def before_destroy_system
  if self.id == 1
    errors.add(:base, I18n.t('anubis.tenants.errors.cant_destroy'))
    throw(:abort, __method__)
  end

  Anoubis::Tenant::SystemLocale.where(system_id: self.id).each do |system_locale|
    system_locale.destroy
  end

  if !can_destroy?
    errors.add(:base, I18n.t('anubis.tenants.errors.has_childs'))
    throw(:abort, __method__)
  end
end

#before_save_systemObject

Is called before system will be stored in database. Changes #ident value to lower case.



72
73
74
# File 'app/models/anoubis/tenant/system.rb', line 72

def before_save_system
  self.ident = self.ident.downcase if self.ident
end

#before_validation_system_createObject

Is called before validation when new system is being created. If it’s a first system then it sets #ident value to ‘sys’



29
30
31
32
33
34
35
36
# File 'app/models/anoubis/tenant/system.rb', line 29

def before_validation_system_create
  if self.id
    if self.id == 1
      self.ident = 'sys'
      return true
    end
  end
end

#before_validation_system_updateObject

Is called before validation when system is being updated. Prevents the changing #ident value for system with id 1.



41
42
43
44
45
46
# File 'app/models/anoubis/tenant/system.rb', line 41

def before_validation_system_update
  if self.id == 1 && self.ident_was != self.ident
    errors.add(:ident, I18n.t('anubis.systems.errors.cant_change_ident'))
    throw(:abort, __method__)
  end
end

#model_localeSystemLocale

Returns model localization data from Anoubis::Tenant::SystemLocale.

Returns:



118
119
120
# File 'app/models/anoubis/tenant/system.rb', line 118

def model_locale
  @model_locale ||= self.system_locales.where(locale: Anoubis::Tenant::SystemLocale.locales[self.current_locale.to_sym]).first
end

#update_groups_full_ident(id, ident) ⇒ Object

Updates Group#full_ident when changed system’s identifier.

Parameters:

  • id (Integer)

    group unique identifier

  • ident (String)

    new system identifier



108
109
110
111
112
113
# File 'app/models/anoubis/tenant/system.rb', line 108

def update_groups_full_ident (id, ident)
  query = <<-SQL
          UPDATE groups SET groups.full_ident = CONCAT('#{ident}.', groups.ident) WHERE groups.system_id = #{id}
  SQL
  Anoubis::Tenant::Group.connection.execute query
end