Class: Anoubis::Sso::Client::Menu

Inherits:
ApplicationRecord
  • Object
show all
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

Instance Method Summary collapse

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.).



18
# File 'app/models/anoubis/sso/client/menu.rb', line 18

validates :action, presence: true

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

Returns:

  • (Menu, nil)

    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

#modeString

Returns the controller path for menu element.

Returns:

  • (String)

    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_sizeInteger

Returns the default page size for table of data frame.

Returns:

  • (Integer)

    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_titleString

Returns the menu’s localized page title. Uses in frontend application.

Returns:

  • (String)

    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 }

#positionInteger

Returns the order position of menu element in current level.

Returns:

  • (Integer)

    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_titleString

Returns the menu’s localized short title. Uses in frontend application.

Returns:

  • (String)

    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.

Returns:

  • ('visible', 'hidden')

    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.

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.



77
# File 'app/models/anoubis/sso/client/menu.rb', line 77

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

#tabInteger

Returns the nesting level of menu element.

Returns:

  • (Integer)

    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 }

#titleString

Returns the menu’s localized title.

Returns:

  • (String)

    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_menuObject

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 after_destroy_sso_client_menu
  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 |menu|
    menu.destroy
  end
end

#before_create_sso_client_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.



90
91
92
93
94
95
# File 'app/models/anoubis/sso/client/menu.rb', line 90

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

  self.before_update_sso_client_menu
end

#before_destroy_sso_client_menuObject

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 before_destroy_sso_client_menu
  if !self.can_destroy?
    errors.add(:base, I18n.t('anubis.menus.errors.has_childs'))
    throw(:abort, __method__)
  end
end

#before_save_sso_client_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.



118
119
120
# File 'app/models/anoubis/sso/client/menu.rb', line 118

def before_save_sso_client_menu
  self.redis.del(self.redis_prefix + 'menu:' + self.mode) if self.redis
end

#before_update_sso_client_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.



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/anoubis/sso/client/menu.rb', line 101

def before_update_sso_client_menu
  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

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