Class: Alchemy::Node

Inherits:
BaseRecord
  • Object
show all
Defined in:
app/models/alchemy/node.rb

Constant Summary collapse

VALID_URL_REGEX =
/\A(\/|\D[a-z+\d.-]+:)/
SKIPPED_ATTRIBUTES_ON_COPY =
%w[id created_at updated_at creator_id updater_id lft rgt depth parent_id]

Constants included from SearchableResource

SearchableResource::SEARCHABLE_COLUMN_TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConfigMissing

#const_missing

Methods included from SearchableResource

#ransackable_associations, #ransackable_attributes, #ransackable_scopes, #ransortable_attributes

Class Method Details

.all_from_clipboard(clipboard) ⇒ Object



50
51
52
53
54
# File 'app/models/alchemy/node.rb', line 50

def all_from_clipboard(clipboard)
  return [] if clipboard.blank?

  where(id: clipboard.collect { |n| n["id"] })
end

.available_menu_namesObject



46
47
48
# File 'app/models/alchemy/node.rb', line 46

def available_menu_names
  read_definitions_file
end

.copy_and_paste(source, new_parent, new_name) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/alchemy/node.rb', line 56

def copy_and_paste(source, new_parent, new_name)
  # Prevent pasting a node as a child of itself or any of its descendants
  return if new_parent && source.is_or_is_ancestor_of?(new_parent)

  attributes = source.attributes.except(*SKIPPED_ATTRIBUTES_ON_COPY).merge(
    name: new_name,
    parent: new_parent,
    language: new_parent&.language || source.language
  )

  node = create!(attributes)

  # Copy all descendants
  source.children.each do |child|
    copy_and_paste(child, node, child.name)
  end

  node
end

.language_root_nodesObject

Returns all root nodes for current language



40
41
42
43
44
# File 'app/models/alchemy/node.rb', line 40

def language_root_nodes
  raise "No language found" if Current.language.nil?

  roots.where(language_id: Current.language.id)
end

Instance Method Details

#nameObject

Returns the name

Either the value is stored in the database or, if attached, the values comes from a page.



34
35
36
# File 'app/models/alchemy/node.rb', line 34

def name
  read_attribute(:name).presence || page&.name
end

#to_partial_pathObject



107
108
109
# File 'app/models/alchemy/node.rb', line 107

def to_partial_path
  "alchemy/menus/#{menu_type}/node"
end

#urlObject

Returns the url

Either the value is stored in the database, aka. an external url. Or, if attached, the values comes from a page.



103
104
105
# File 'app/models/alchemy/node.rb', line 103

def url
  page&.url_path || read_attribute(:url).presence
end