Class: Anoubis::Tenant::Group

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

Overview

Group model. Stores information about usser’s groups

Constant Summary collapse

VALID_IDENT_REGEX =

Group’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

#full_identString

Returns the calculated group’s identification. This identification based on System#ident and #ident.

Returns:

  • (String)

    the calculated group’s identification. This identification based on System#ident and #ident



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

#identString

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

Returns:

  • (String)

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



17
# File 'app/models/anoubis/tenant/group.rb', line 17

validates :ident, length: { minimum: 3, maximum: 50 }, uniqueness: { scope: [:system_id], case_sensitive: false }, format: { with: VALID_IDENT_REGEX }

#systemSystem

Returns reference to system that owns this group.

Returns:

  • (System)

    reference to system that owns this group



24
# File 'app/models/anoubis/tenant/group.rb', line 24

belongs_to :system, class_name: 'Anoubis::Tenant::System'

#titleString

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

Returns:



92
93
94
# File 'app/models/anoubis/tenant/group.rb', line 92

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

Instance Method Details

#before_destroy_groupObject

Is called before group will be deleted from database. Checks the ability to destroy a group. Prevents deleting group with id 1. Also destroys all associated links to User and Menu



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/anoubis/tenant/group.rb', line 62

def before_destroy_group
  # Can't destroy admin group of main system
  if self.ident == 'admin' && self.system_id == 1
    errors.add(:base, I18n.t('anubis.groups.errors.cant_destroy_admin_group'))
    throw(:abort, __method__)
  end

  Anoubis::Tenant::GroupLocale.where(group_id: self.id).each do |group_locale|
    group_locale.destroy
  end

  unless user_groups.empty?
    errors.add(:base, I18n.t('anubis.groups.errors.cant_destroy_group_with_users'))
    throw(:abort, __method__)
  end

  # Before destroy group delete all associated links to users and menus
  Anoubis::Tenant::GroupMenu.where(group_id: self.id).delete_all
  #Anoubis::Tenant::UserGroup.where(group_id: self.id).delete_all
end

#before_save_groupObject

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



55
56
57
# File 'app/models/anoubis/tenant/group.rb', line 55

def before_save_group
  self.full_ident = self.system.ident+'.'+self.ident
end

#before_validation_group_createObject

Is called before validation when new group is being created. Sets #ident value as ‘admin’ and #system value to main system with id 1 if this is a first group.



33
34
35
36
37
38
39
40
41
# File 'app/models/anoubis/tenant/group.rb', line 33

def before_validation_group_create
  if self.id
    if self.id == 1
      self.ident = 'admin'
      self.system_id = 1
      return true
    end
  end
end

#before_validation_group_updateObject

Is called before validation when group is being updated. Prevents changing #ident value when #ident value is ‘admin’



46
47
48
49
50
51
# File 'app/models/anoubis/tenant/group.rb', line 46

def before_validation_group_update
  if self.ident != self.ident_was && self.ident_was == 'admin'
    errors.add(:ident, I18n.t('tims.groups.errors.cant_change_admin_ident'))
    throw(:abort, __method__)
  end
end

#model_localeGroupLocale

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

Returns:



86
87
88
# File 'app/models/anoubis/tenant/group.rb', line 86

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