Class: ArMenu
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- ArMenu
- Defined in:
- app/models/ar_menu.rb
Overview
Schema information
Table name: ArMenu : AgileRail menu system
Default menu system for AgileRails. Model recursively embeds ArMenuItem documents which (theoretically) results in infinite level of sub menus. In practice reasonable maximum level of 4 is advised.
Class Method Summary collapse
-
.all_pages_for_site(parms) ⇒ Object
Return all pages belonging to site ready for select input field.
-
.choices_for_menu(site) ⇒ Object
Will return list of menu items of specified menu.
-
.choices_for_menu_as_tree(site_id = nil) ⇒ Object
Will return menu structure for menus belonging to the site.
-
.do_sub_menu(menus, parent, ids) ⇒ Object
Subroutine of choices_for_menu_as_tree.
-
.menu_item_link_update(record) ⇒ Object
Will update link value of selected menu_item.
Instance Method Summary collapse
-
#cache_delete ⇒ Object
Clear menu cache.
Class Method Details
.all_pages_for_site(parms) ⇒ Object
Return all pages belonging to site ready for select input field. Used by ar_menu* forms, for selecting page which will be linked by menu option.
Parameters: [site] Site document.
135 136 137 138 139 140 141 142 |
# File 'app/models/ar_menu.rb', line 135 def self.all_pages_for_site(parms) = parms[:ids].split(';').first # get menu id from params[:ids] site_id = ArMenu.only(:site_id).find().ar_site_id site = ArSite.only(:page_class, :id).find(site_id) # find site pages = site.page_class.constantize # pages collection name pages.only(:subject, :id).where(ar_site_id: site.id, active: true).order(:subject) .map { |page| [ page.subject, page.id] } end |
.choices_for_menu(site) ⇒ Object
Will return list of menu items of specified menu. Used in ArPage document for selecting top level selected menu.
Called from AgileApplicationHelper :agile_choices_for_menu: method.
Parameters: [Site] ArSite document. Site for which menu belongs to. If site is not specified all current menus in ar_menus collection will be returned.
Returns: Array. Of choices prepared for select input field.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/ar_menu.rb', line 65 def self.(site) rez = [] = (site..blank? ? all : where(name: site.)).to_a .each do || rez << [.name, nil] ..where(active: true).order(:order).each do || rez << ['-- ' + .caption, ._id] end end rez end |
.choices_for_menu_as_tree(site_id = nil) ⇒ Object
Will return menu structure for menus belonging to the site.
Parameters: [Site] ArSite document. Site for which menu belongs to. If site is not specified all current menus in collection will be returned.
Returns: Array. Of choices prepared for tree:select input field.
101 102 103 104 105 106 107 108 109 110 111 |
# File 'app/models/ar_menu.rb', line 101 def self.(site_id = nil) return [] if site_id == nil id = site_id.class == Integer ? site_id : site_id.id where(ar_site_id: [nil, id], active: true).order(:name).inject([]) do |r, | r << [.name, .id, nil, 0] = ArMenuItem.where(ar_menu_id: .id, parent_id: 0).order(:order).to_a r += (, .id, .id.to_s) if .size > 0 r end end |
.do_sub_menu(menus, parent, ids) ⇒ Object
Subroutine of choices_for_menu_as_tree
80 81 82 83 84 85 86 87 88 89 |
# File 'app/models/ar_menu.rb', line 80 def self.(, parent, ids) #:nodoc: result = [] .each do |item| long_id = "#{ids};#{item.id}" result << [item.caption, long_id, parent, item.order] = ArMenuItem.where(parent_id: item.id).order(:order).to_a result += (, long_id, long_id) if .size > 0 end result end |
.menu_item_link_update(record) ⇒ Object
Will update link value of selected menu_item
Parameters: [record] Array. Data of saved document.
119 120 121 122 123 124 125 126 |
# File 'app/models/ar_menu.rb', line 119 def self.(record) return if record.try(:menu_id).blank? # not set ar = record..split(';') = ArMenuItem.find(ar.last.to_i) .page_id = record.id .save end |
Instance Method Details
#cache_delete ⇒ Object
Clear menu cache
48 49 50 |
# File 'app/models/ar_menu.rb', line 48 def cache_delete Agile.cache_clear(name) end |