Class: Anoubis::Tenant::Menu
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- Core::ApplicationRecord
- ApplicationRecord
- Anoubis::Tenant::Menu
- 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
-
#action ⇒ String
The default action of menu element (‘data’, ‘menu’, etc.).
-
#menu ⇒ Menu?
The parent menu for element menu (if exists).
-
#mode ⇒ String
The controller path for menu element.
-
#page_size ⇒ Integer
The default page size for table of data frame.
-
#page_title ⇒ String
The menu’s page title.
-
#position ⇒ Integer
The order position of menu element in current level.
-
#short_title ⇒ String
The menu’s short title.
-
#state ⇒ 'visible', 'hidden'
The visibility of menu element.
-
#status ⇒ 'enabled', 'disabled'
The status of menu element.
-
#tab ⇒ Integer
The nesting level of menu element.
-
#title ⇒ String
The menu’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_destroy_menu ⇒ Object
Is called after menu was deleted from database.
-
#before_create_menu ⇒ Object
Is called before menu will be created in database.
-
#before_destroy_menu ⇒ Object
Is called before menu will be deleted from database.
-
#before_save_menu ⇒ Object
Is called right before menu will be stored in database (after #before_create_menu and #before_update_menu).
-
#before_update_menu ⇒ Object
Is called before menu will be stored in database.
-
#model_locale ⇒ MenuLocale
Returns model localization data from MenuLocale.
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
#action ⇒ String
Returns the default action of menu element (‘data’, ‘menu’, etc.).
20 |
# File 'app/models/anoubis/tenant/menu.rb', line 20 validates :action, presence: true |
#menu ⇒ Menu?
Returns 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 |
#mode ⇒ String
Returns the controller path for menu element.
16 |
# File 'app/models/anoubis/tenant/menu.rb', line 16 validates :mode, presence: true, uniqueness: true |
#page_size ⇒ Integer
Returns 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_title ⇒ String
Returns the menu’s page title. Page title loads from Anoubis::Tenant::MenuLocale#page_title based on Core::ApplicationRecord#current_locale.
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 |
#position ⇒ Integer
Returns 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_title ⇒ String
Returns the menu’s short title. Short title loads from Anoubis::Tenant::MenuLocale#short_title based on Core::ApplicationRecord#current_locale.
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.
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.
47 |
# File 'app/models/anoubis/tenant/menu.rb', line 47 enum status: { enabled: 0, disabled: 1 } |
#tab ⇒ Integer
Returns 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 } |
#title ⇒ String
Returns the menu’s title. Title loads from Anoubis::Tenant::MenuLocale#title based on Core::ApplicationRecord#current_locale.
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_menu ⇒ Object
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 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 || .destroy end end |
#before_create_menu ⇒ Object
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 data = Anoubis::Tenant::Menu.where(menu_id: self.).maximum(:position) self.position = if data then data + 1 else 0 end self. end |
#before_destroy_menu ⇒ Object
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 Anoubis::Tenant::MenuLocale.where(menu_id: self.id).each do || .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_menu ⇒ Object
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 self.redis.del(self.redis_prefix + 'menu_' + self.mode) if self.redis end |
#before_update_menu ⇒ Object
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 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 = Anoubis::Tenant::Menu.where(id: self.).first if self.tab = .tab + 1 else self.tab = 0 end end |
#model_locale ⇒ MenuLocale
Returns model localization data from Anoubis::Tenant::MenuLocale.
135 136 137 |
# File 'app/models/anoubis/tenant/menu.rb', line 135 def model_locale @model_locale ||= self..where(locale: Anoubis::Tenant::MenuLocale.locales[self.current_locale.to_sym]).first end |