Class: Anoubis::Tenant::GroupMenu

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

Overview

Model links Menu and Group. Describes group access to menu.

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

#access'not', ...

Returns group access to menu element.

  • ‘not’ — menu element doesn’t available for this group

  • ‘read’ — group has access to menu element only for read data

  • ‘write’ — group has access to menu element for read and write data

  • ‘disable’ — group hasn’t access to menu element.

Returns:

  • ('not', 'read', 'write', 'disable')

    group access to menu element.

    • ‘not’ — menu element doesn’t available for this group

    • ‘read’ — group has access to menu element only for read data

    • ‘write’ — group has access to menu element for read and write data

    • ‘disable’ — group hasn’t access to menu element



29
# File 'app/models/anoubis/tenant/group_menu.rb', line 29

enum access: { not: 0, read: 20, write: 40, disable: 60 }

#groupGroup

Returns reference to the Anoubis::Tenant::Group model.

Returns:



15
# File 'app/models/anoubis/tenant/group_menu.rb', line 15

belongs_to :group, :class_name => 'Anoubis::Tenant::Group'

Returns reference to the Menu model.

Returns:

  • (Menu)

    reference to the Menu model



20
# File 'app/models/anoubis/tenant/group_menu.rb', line 20

belongs_to :menu, :class_name => 'Anoubis::Tenant::Menu'

Instance Method Details

#after_create_group_menuObject

Is called after new link between menu and group was created. If new element has parent with link that doesn’t present in database then adds this link to database with #access defined as ‘read’.



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

def after_create_group_menu
  if self.menu.menu_id != nil
    Anoubis::Tenant::GroupMenu.find_or_create_by(menu_id: self.menu.menu_id, group_id: self.group_id) do |menu|
      menu.access = Anoubis::Tenant::GroupMenu.accesses[:read]
    end
  end
  self.after_modify_group_menu
end

#after_destroy_group_menuObject

Is called after link between menu and group had been deleted from database. It also deletes all child links.



66
67
68
69
70
71
72
73
# File 'app/models/anoubis/tenant/group_menu.rb', line 66

def after_destroy_group_menu
  Anoubis::Tenant::Menu.select(:id).where(menu_id: self.menu_id).each do |menu|
    Anoubis::Tenant::GroupMenu.where(menu_id: menu.id, group_id: self.group_id).each do |group_menu|
      group_menu.destroy
    end
  end
  self.after_modify_group_menu
end

#after_modify_group_menuObject

Deletes all user’s keys that belong this menu element in Redis database.



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

def after_modify_group_menu
  if self.redis
    self.redis.keys(self.redis_prefix + '*_'+self.menu.mode).each do |data|
      self.redis.del data
    end
  end
end

#before_update_group_menuObject

Is called before link between menu and group will be updated. Procedure prevents changing #menu and #group value.



58
59
60
61
62
# File 'app/models/anoubis/tenant/group_menu.rb', line 58

def before_update_group_menu
  self.menu_id = self.menu_id_was if self.menu_id_changed?
  self.group_id = self.group_id_was if self.group_id_changed?
  self.after_modify_group_menu
end

#before_validation_group_menuObject

Is called before validation when the link between menu and group is being created or updated. Procedure checks if group belongs a system that has access to this menu element. If #access doesn’t defined then #access sets to ‘read’



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

def before_validation_group_menu
  if !Anoubis::Tenant::SystemMenu.where(system_id: self.group.system_id, menu_id: self.menu_id).first
    errors.add(:base, I18n.t('anubis.group_menus.errors.no_access'))
    throw(:abort, __method__)
  end
  self.access = Anoubis::Tenant::GroupMenu.accesses[:read] if !self.access
end