Class: Alchemy::Node

Inherits:
BaseRecord
  • Object
show all
Includes:
RelatableResource
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 RelatableResource

RelatableResource::RELATED_INGREDIENTS_SUBQUERY

Constants included from SearchableResource

SearchableResource::SEARCHABLE_COLUMN_TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RelatableResource

#deletable?

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



122
123
124
125
126
# File 'app/models/alchemy/node.rb', line 122

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

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

.available_menu_names ⇒ Object



118
119
120
# File 'app/models/alchemy/node.rb', line 118

def available_menu_names
  read_definitions_file
end

.copy_and_paste(source, new_parent, new_name) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/alchemy/node.rb', line 128

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_nodes ⇒ Object

Returns all root nodes for current language



112
113
114
115
116
# File 'app/models/alchemy/node.rb', line 112

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

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

Instance Method Details

#name ⇒ Object

Returns the name

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



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

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

#to_partial_path ⇒ Object



179
180
181
# File 'app/models/alchemy/node.rb', line 179

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

#url ⇒ Object

Returns the url

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



175
176
177
# File 'app/models/alchemy/node.rb', line 175

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