Switch to using view classes isntead of partials

This commit is contained in:
Ben Hollis 2012-10-13 14:28:08 -07:00 committed by Thomas Reynolds
parent b8eb932a73
commit 142abe027e
6 changed files with 71 additions and 30 deletions

View file

@ -0,0 +1 @@
alert("foo");

View file

@ -0,0 +1,14 @@
module Middleman
module MetaPages
# View class for a sitemap resource
class SitemapResource
def initialize(resource)
@resource = resource
end
def render
"<p>#{@resource.destination_path}</p>"
end
end
end
end

View file

@ -0,0 +1,48 @@
require 'middleman-core/meta_pages/sitemap_resource'
module Middleman
module MetaPages
# View class for a sitemap tree
class SitemapTree
def initialize
@children = {}
end
def add_resource(resource)
add_path(resource.path.split('/'), resource)
end
def render
content = ""
@children.keys.sort_by(&:downcase).each do |path_part|
content << "<details>"
content << "<summary>"
content << path_part
content << "</summary>"
subtree = @children[path_part]
content << subtree.render
content << "</details>"
end
content
end
protected
def add_path(path_parts, resource)
first_part = path_parts.first
if path_parts.size == 1
@children[first_part] = SitemapResource.new(resource)
else
@children[first_part] ||= SitemapTree.new
@children[first_part].add_path(path_parts[1..-1], resource)
end
end
def to_s
"Sitemap Tree"
end
end
end
end

View file

@ -1,12 +0,0 @@
<ul>
<% tree.each do |path_part, subtree| %>
<li>
<%= path_part %>
<% if subtree.respond_to? :path %>
Path!
<% else %>
<%= partial 'sitemap_tree.html.erb', :tree => subtree %>
<% end %>
</li>
<% end %>
</ul>

View file

@ -4,13 +4,14 @@
<meta charset="utf-8">
<title>Middleman Sitemap</title>
<script src="../assets/sitemap.js"></script>
</head>
<body>
<h1>Middleman Sitemap</h1>
<a href="../">More meta pages</a>
<%= partial 'sitemap_tree.html.erb', :tree => resource_tree %>
<%= sitemap_tree.render %>
</body>
</html>