Class: Katalyst::Navigation::Menu

Inherits:
ApplicationRecord
  • Object
show all
Includes:
GarbageCollection
Defined in:
app/models/katalyst/navigation/menu.rb

Overview

A menu is a list of items (links, buttons, etc) with order and depth creating a tree structure. Items should be copy-on-write, as menus store their structure as copy-on-write versions using item ids.

Defined Under Namespace

Classes: Version

Instance Method Summary collapse

Methods included from GarbageCollection

#remove_stale_versions

Instance Method Details

#items_attributesObject

Required for testing items validation



75
76
77
# File 'app/models/katalyst/navigation/menu.rb', line 75

def items_attributes
  draft_version&.nodes&.as_json
end

#items_attributes=(attributes) ⇒ Object

Updates the current draft version with new structure. Attributes should be structural information about the items, e.g. ‘=> {id:, depth:` or `[depth:]`.

This method conforms to the behaviour of ‘accepts_nested_attributes_for` so that it can be used with rails form helpers.



84
85
86
# File 'app/models/katalyst/navigation/menu.rb', line 84

def items_attributes=(attributes)
  next_version.nodes = attributes
end

#publish!Object

Promotes the draft version to become the published version



63
64
65
66
# File 'app/models/katalyst/navigation/menu.rb', line 63

def publish!
  update!(published_version: draft_version)
  self
end

#revert!Object

Reverts the draft version to the current published version



69
70
71
72
# File 'app/models/katalyst/navigation/menu.rb', line 69

def revert!
  update!(draft_version: published_version)
  self
end

#stateObject

A menu is in draft mode if it has an unpublished draft or it has no published version.

Returns:

  • the current state of the menu, either ‘published` or `draft`



54
55
56
57
58
59
60
# File 'app/models/katalyst/navigation/menu.rb', line 54

def state
  if published_version_id && published_version_id == draft_version_id
    :published
  else
    :draft
  end
end