Class: Menu

Inherits:
Forest::ApplicationRecord show all
Includes:
Sluggable
Defined in:
app/models/menu.rb

Constant Summary collapse

CACHE_KEY =
'forest_menus'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Forest::ApplicationRecord

cache_key, cache_key_name, csv_columns, #expire_cache_key, expire_cache_key, statusable?, #statusable?, to_csv_template, #to_friendly_param, #to_label, #to_select2_response, #to_select2_selection, versionable, #versionable

Class Method Details

.expire_cache!Object



14
15
16
# File 'app/models/menu.rb', line 14

def self.expire_cache!
  Rails.cache.delete CACHE_KEY
end

.for(slug) ⇒ Object



10
11
12
# File 'app/models/menu.rb', line 10

def self.for(slug)
  self.menus.select { |menu| menu.slug == slug.to_s }.first
end

.resource_descriptionObject



18
19
20
# File 'app/models/menu.rb', line 18

def self.resource_description
  "Menus are where you edit the navigation structure of your website. Menu items are composed of custom links or associations with pages."
end

Instance Method Details

#nokogiriObject



26
27
28
29
30
31
32
33
34
# File 'app/models/menu.rb', line 26

def nokogiri
  @nokogiri ||= begin
    fragment = Nokogiri::HTML.fragment('<ul></ul>')
    structure_as_json.each do |menu_item|
      create_nokogiri_children(menu_item, fragment.at_css('ul'))
    end
    fragment
  end
end

#renderObject



36
37
38
# File 'app/models/menu.rb', line 36

def render
  nokogiri.to_html.html_safe
end

#structure_as_jsonObject



22
23
24
# File 'app/models/menu.rb', line 22

def structure_as_json
  JSON.parse(structure.presence || '[]')
end