Class: Anoubis::Sso::Client::Menu
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Anoubis::Sso::Client::Menu
- Defined in:
- app/models/anoubis/sso/client/menu.rb
Constant Summary collapse
- VALID_IDENT_REGEX =
/\A[a-z_\/0-9]*\z/i
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 localized page title.
-
#position ⇒ Integer
The order position of menu element in current level.
-
#short_title ⇒ String
The menu’s localized 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 localized title.
Instance Method Summary collapse
-
#after_destroy_sso_client_menu ⇒ Object
Is called after menu was deleted from database.
-
#before_create_sso_client_menu ⇒ Object
Is called before menu will be created in database.
-
#before_destroy_sso_client_menu ⇒ Object
Is called before menu will be deleted from database.
-
#before_save_sso_client_menu ⇒ Object
Is called right before menu will be stored in database (after #before_create_menu and #before_update_menu).
-
#before_update_sso_client_menu ⇒ Object
Is called before menu will be stored in database.
Instance Attribute Details
#action ⇒ String
Returns the default action of menu element (‘data’, ‘menu’, etc.).
18 |
# File 'app/models/anoubis/sso/client/menu.rb', line 18 validates :action, presence: true |
#menu ⇒ Menu?
Returns the parent menu for element menu (if exists).
34 |
# File 'app/models/anoubis/sso/client/menu.rb', line 34 belongs_to :menu, class_name: 'Anoubis::Sso::Client::Menu', optional: true |
#mode ⇒ String
Returns the controller path for menu element.
14 |
# File 'app/models/anoubis/sso/client/menu.rb', line 14 validates :mode, length: { minimum: 3, maximum: 100 }, uniqueness: { case_sensitive: false }, format: { with: VALID_IDENT_REGEX } |
#page_size ⇒ Integer
Returns the default page size for table of data frame.
30 |
# File 'app/models/anoubis/sso/client/menu.rb', line 30 validates :page_size, numericality: { only_integer: true, greater_than_or_equal_to: 0 } |
#page_title ⇒ String
Returns the menu’s localized page title. Uses in frontend application.
51 |
# File 'app/models/anoubis/sso/client/menu.rb', line 51 validates :page_title, presence: true, length: { minimum: 3, maximum: 200 } |
#position ⇒ Integer
Returns the order position of menu element in current level.
26 |
# File 'app/models/anoubis/sso/client/menu.rb', line 26 validates :position, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 } |
#short_title ⇒ String
Returns the menu’s localized short title. Uses in frontend application.
63 |
# File 'app/models/anoubis/sso/client/menu.rb', line 63 validates :short_title, length: { maximum: 200 } |
#state ⇒ 'visible', 'hidden'
Returns the visibility of menu element. Attribute is used in fronted application.
-
‘visible’ — element is visible.
-
‘hidden’ — element is hidden.
83 |
# File 'app/models/anoubis/sso/client/menu.rb', line 83 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.
77 |
# File 'app/models/anoubis/sso/client/menu.rb', line 77 enum status: { enabled: 0, disabled: 1 } |
#tab ⇒ Integer
Returns the nesting level of menu element.
22 |
# File 'app/models/anoubis/sso/client/menu.rb', line 22 validates :tab, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 } |
#title ⇒ String
Returns the menu’s localized title.
39 |
# File 'app/models/anoubis/sso/client/menu.rb', line 39 validates :title, presence: true, length: { maximum: 100 } |
Instance Method Details
#after_destroy_sso_client_menu ⇒ Object
Is called after menu was deleted from database. Procedure recalculates position of other menu elements.
134 135 136 137 138 139 140 141 142 143 144 |
# File 'app/models/anoubis/sso/client/menu.rb', line 134 def query = <<-SQL UPDATE menus SET menus.position = menus.position - 1 WHERE menus.tab = #{self.tab} AND menus.position > #{self.position} SQL Anoubis::Sso::Client::Menu.connection.execute query Anoubis::Sso::Client::Menu.where(menu_id: self.id).find_each do || .destroy end end |
#before_create_sso_client_menu ⇒ Object
90 91 92 93 94 95 |
# File 'app/models/anoubis/sso/client/menu.rb', line 90 def data = Anoubis::Sso::Client::Menu.where(menu_id: self.).maximum(:position) self.position = if data then data + 1 else 0 end self. end |
#before_destroy_sso_client_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 MenuLocale.
125 126 127 128 129 130 |
# File 'app/models/anoubis/sso/client/menu.rb', line 125 def if !self.can_destroy? errors.add(:base, I18n.t('anubis.menus.errors.has_childs')) throw(:abort, __method__) end end |
#before_save_sso_client_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.
118 119 120 |
# File 'app/models/anoubis/sso/client/menu.rb', line 118 def self.redis.del(self.redis_prefix + 'menu:' + self.mode) if self.redis end |
#before_update_sso_client_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.
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/models/anoubis/sso/client/menu.rb', line 101 def self.mode = mode.downcase self.action = self.action.downcase self.page_size = 20 if self.page_size == 0 self.page_size = self.page_size.to_i = Anoubis::Sso::Client::Menu.where(id: self.).first if self.tab = .tab + 1 else self.tab = 0 end end |