Class: Anoubis::Tenant::Menu

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

Overview

Menu model. Stores information about all menu elements of the portal. Menu model defines the dependence between controller and user access.

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 ApplicationRecord

#before_create_tenant_anoubis_model, #before_update_tenant_anoubis_model, get_where

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

#actionString

Returns the default action of menu element (‘data’, ‘menu’, etc.).

Returns:

  • (String)

    the default action of menu element (‘data’, ‘menu’, etc.).



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

validates :action, presence: true

Returns the parent menu for element menu (if exists).

Returns:

  • (Menu, nil)

    the parent menu for element menu (if exists).



36
# File 'app/models/anoubis/tenant/menu.rb', line 36

belongs_to :menu, class_name: 'Anoubis::Tenant::Menu', optional: true

#modeString

Returns the controller path for menu element.

Returns:

  • (String)

    the controller path for menu element.



16
# File 'app/models/anoubis/tenant/menu.rb', line 16

validates :mode, presence: true, uniqueness: true

#page_sizeInteger

Returns the default page size for table of data frame.

Returns:

  • (Integer)

    the default page size for table of data frame.



32
# File 'app/models/anoubis/tenant/menu.rb', line 32

validates :page_size, numericality: { only_integer: true, greater_than_or_equal_to: 0 }

#page_titleString

Returns the menu’s page title. Page title loads from Anoubis::Tenant::MenuLocale#page_title based on Core::ApplicationRecord#current_locale.

Returns:



147
148
149
# File 'app/models/anoubis/tenant/menu.rb', line 147

def page_title
  self.model_locale.page_title if self.model_locale
end

#positionInteger

Returns the order position of menu element in current level.

Returns:

  • (Integer)

    the order position of menu element in current level.



28
# File 'app/models/anoubis/tenant/menu.rb', line 28

validates :position, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }

#short_titleString

Returns the menu’s short title. Short title loads from Anoubis::Tenant::MenuLocale#short_title based on Core::ApplicationRecord#current_locale.

Returns:



153
154
155
# File 'app/models/anoubis/tenant/menu.rb', line 153

def short_title
  self.model_locale.short_title if self.model_locale
end

#state'visible', 'hidden'

Returns the visibility of menu element. Attribute is used in fronted application.

  • ‘visible’ — element is visible.

  • ‘hidden’ — element is hidden.

Returns:

  • ('visible', 'hidden')

    the visibility of menu element. Attribute is used in fronted application.

    • ‘visible’ — element is visible.

    • ‘hidden’ — element is hidden.



53
# File 'app/models/anoubis/tenant/menu.rb', line 53

enum state: { visible: 0, hidden: 1 }

#status'enabled', 'disabled'

Returns the status of menu element.

  • ‘enabled’ — element is enabled and is used by the system.

  • ‘disabled’ — element is disabled and isn’t used by the system.

Returns:

  • ('enabled', 'disabled')

    the status of menu element.

    • ‘enabled’ — element is enabled and is used by the system.

    • ‘disabled’ — element is disabled and isn’t used by the system.



47
# File 'app/models/anoubis/tenant/menu.rb', line 47

enum status: { enabled: 0, disabled: 1 }

#tabInteger

Returns the nesting level of menu element.

Returns:

  • (Integer)

    the nesting level of menu element



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

validates :tab, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }

#titleString

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

Returns:



141
142
143
# File 'app/models/anoubis/tenant/menu.rb', line 141

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

Instance Method Details

#after_destroy_menuObject

Is called after menu was deleted from database. Procedure recalculates position of other menu elements.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/anoubis/tenant/menu.rb', line 114

def after_destroy_menu
  query = <<-SQL
          UPDATE menus
          SET menus.position = menus.position - 1
          WHERE menus.tab = #{self.tab} AND menus.position > #{self.position}
  SQL
  Anoubis::Tenant::Menu.connection.execute query
  #i = self.position
  #Anoubis::Tenant::Menu.where(menu_id: self.menu_id, position: (self.position+1..Float::INFINITY)).find_each do |menu|
  #        menu.position = i
  #        menu.save
  #        i += 1
  #      end
  Anoubis::Tenant::Menu.where(menu_id: self.id).find_each do |menu|
    menu.destroy
  end
end

#before_create_menuObject

Is called before menu will be created in database. Sets #position as last #position + 1 on current #tab. After this calls #before_update_menu for additional modification.



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

def before_create_menu
  data = Anoubis::Tenant::Menu.where(menu_id: self.menu_id).maximum(:position)
  self.position = if data then data + 1 else 0 end

  self.before_update_menu
end

#before_destroy_menuObject

Is called before menu will be deleted from database. Checks the ability to destroy a menu. Delete all translations for menu model from Anoubis::Tenant::MenuLocale.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/anoubis/tenant/menu.rb', line 93

def before_destroy_menu
  Anoubis::Tenant::MenuLocale.where(menu_id: self.id).each do |menu_locale|
    menu_locale.destroy
  end

  if !can_destroy?
    errors.add(:base, I18n.t('anubis.menus.errors.has_childs'))
    throw(:abort, __method__)
  end
  #childs = !self.menus.empty?
  #childs = !self.group_menus.empty? if !childs
  #childs = !self.system_menus.empty? if !childs

  #return if !childs

  #errors.add(:base, I18n.t('menus.errors.has_childs'))
  #throw(:abort, __method__)
end

#before_save_menuObject

Is called right before menu will be stored in database (after #before_create_menu and #before_update_menu). Deletes cache data for this menu in Redis database.



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

def before_save_menu
  self.redis.del(self.redis_prefix + 'menu_' + self.mode) if self.redis
end

#before_update_menuObject

Is called before menu will be stored in database. Sets #mode and #action in lowercase. If #page_size doesn’t defined then sets it to 20. If defined parent menu element then sets #tab based on #tab of parent menu element + 1.



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/anoubis/tenant/menu.rb', line 69

def before_update_menu
  self.mode = mode.downcase
  self.action = self.action.downcase
  self.page_size = 20 if !self.page_size
  self.page_size = self.page_size.to_i

  parent_menu = Anoubis::Tenant::Menu.where(id: self.menu_id).first
  if parent_menu
    self.tab = parent_menu.tab + 1
  else
    self.tab = 0
  end
end

#model_localeMenuLocale

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

Returns:



135
136
137
# File 'app/models/anoubis/tenant/menu.rb', line 135

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