Skip to content

Instantly share code, notes, and snippets.

View mathie's full-sized avatar

Graeme Mathieson mathie

View GitHub Profile
class Node < ActiveRecord::Base
def parent
if parent_id = path.last
self.class.find(parent_id)
else
nil
end
end
end
class Node < ActiveRecord::Base
def ancestors
if path.present?
self.class.where(id: path)
else
self.class.none
end
end
end
class Node < ActiveRecord::Base
def siblings
self.class.where(path: path)
end
end
class Node < ActiveRecord::Base
def children
self.class.where(path: path + [id])
end
end
class Node < ActiveRecord::Base
def descendants
self.class.where(“:id = ANY(path)”, id: id)
end
end
class NodesController < ActionController::Base
def index
@nodes = Node.roots
end
end
<ul>
<%= render @nodes %>
</ul>
<li>
<%= link_to node.name, node %>
<% if node.children.present? %>
<ul>
<%= render node.children %>
</ul>
<% end %>
</li>
class NodesController < ActionController::Base
def index
@nodes = Node.roots.eager_load(:descendants)
end
end
@mathie
mathie / index.html
Created May 4, 2016 16:24
The simplest thing that could possibly work...
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>A Sneak Peek at The Internet</title>
</head>
<body>
<h1>A Sneak Peek at The Internet</h1>
<p>Now hosted on <a href=“https://aws.amazon.com/“>Amazon Web Services</a>!</p>
</body>
</html>