How to do peer reviews with middleman-blog

As I was writing my last post, I asked for help from a friend to choose a title. Middleman allows for posts to be for the development environment only with the published: false option, but it will not build the post when you run middleman build. What I really needed was a way to build the post and have it served in a public environment, but avoid having it being listed anywhere: RSS feed, sitemap, index page, etc. After being bit by this and publishing the article with a completely bogus title, I thought I’d submit a pull request to middleman-blog for a public_draft option. The idea is pretty simple:

  • always build every post
  • in any non-development environments, reject public drafts from blog.articles

My pull request was not accepted because it’s debatable whether this should be maintained as part of the framework when it’s very simple to handle in your templates or in a template helper. My feelings are not hurt and I agree. Why should it be the burden of the project maintainers to support every feature request?

Because I still needed a way to have public drafts, I went ahead and added functionality through a middleman template helper and thought I’d share.

Template helper

To define custom template helpers, you have a few options. I’m going to use the helpers block option for the sake of this article, but I opted for the module option that I link to in another section if you want to include these in your project. You can read more about custom template helpers here.

I added a published helper that allows you to weed out public drafts out of any collection of articles:

helpers do
  def published(articles)
    if development?
      articles
    else
      articles.reject{|a| a.data.public_draft }
    end
  end
end

It’s then a matter of making sure that you filter every list of articles through the published helper. For example, my index.html template now uses something along the lines of this:

<% published(blog.articles).each do |article| %>
  <li><%= link_to article.title, article.url %></li>
<% end %>

Or if you were to list tag names and their number of articles:

<% blog.tags.each do |tag, articles| %>
  <li><%= tag %> (<%= published(articles).size %>)</li>
<% end %>

Sitemap gotcha

If you use sitemap.resources for anything, you’ll most likely want to weed out public drafts from those as well. I have a sitemap.xml similar to this which relies on sitemap.resources. I just made another helper named sitemap_resources to weed out public drafts and use that for my sitemap.xml instead:

helpers do
  def sitemap_resources
    sitemap.resources.reject{|p| p.data.public_draft }
  end
end

As a module for profits

In reality, it makes a lot more sense to make these helpers part of a module. I published my helpers/public_draft_helpers.rb module here in case this can be helpful for anyone else.

Conclusion

It turns out I’m pretty happy with the helper implementation. I’m especially happy that it made me realize that my pull request did not consider things like the list of articles for tags, or the sitemap generation.

comments powered by Disqus
Learn More

Subscribe via RSS