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



73
74
75
# File 'app/models/katalyst/navigation/menu.rb', line 73

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.



82
83
84
# File 'app/models/katalyst/navigation/menu.rb', line 82

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

#publish!Object

Promotes the draft version to become the published version



61
62
63
64
# File 'app/models/katalyst/navigation/menu.rb', line 61

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

#revert!Object

Reverts the draft version to the current published version



67
68
69
70
# File 'app/models/katalyst/navigation/menu.rb', line 67

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



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

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