Goodbye Wordpress, Hello Jekyll
Article posted by Joel Moss on 09 Dec 2009   |  

A few months ago, I ditched my install of Wordpress and rebuilt this blog from the ground up using nothing but good old, raw HTML, with a little sprinkling of magic. At the time, I didn't really talk about it much, so in this post I will be talking about how my blog is built, maintained and published using that little sprinkling of magic that is called Jekyll and Codaset Pages.

Goodbye Wordpress

Now don't get me wrong here, but Wordpress is great at what it does. It's probably the best blog engine available, and the fact that it has a huge community around it, makes it somewhat of a rock star in the open source community. But... what it tries to do is everything! It tries to answer all your blogging conundrums, which unfortunately actually places some restrictions on what you can or cannot do with your blog. Yes I can install a plugin, or even write one myself, but have you ever written a Wordpress plugin? Don't!

What I am trying to say, is that blogging with Wordpress or any other automated blogging engine is not fun anymore. I know the point of a blogging engine is to allow you to publish without the need to write code, but you know what? I like writing code! I like having full control over what my blog does and what it looks like. And that is why I ditched Wordpress.

Hello Jekyll

What I didn't want to do was to write my own blogging engine. In fact, I don't want to use a blogging engine at all, at least not in the traditional sense. I just want a simple, unrestricted method of blogging. So I turned to Jekyll. Jekyll is a blog aware, static site generator written in Ruby, and in use by Codaset and Github for their pages feature. It lets you write raw HTML, or your template language of choice (Textile, Markdown), and even lets you create layouts. It then takes these templates and layouts and spits out a complete website or blog in a completely static form.

And Hello Codaset Pages

Codaset Pages are a brilliantly simple way to host any static content, and works perfectly with Jekyll. Just create a new project under your Codaset account, then every time you 'git push' to it, your site or blog is instantly updated. So I have a project called 'joelmoss_pages', which can be seen at http://joelmoss.codaset.com. And that is this very blog.

You can see exactly how my blog is structured and have access to all the source code at Codaset, so I won't go into too much detail. Just look at the source yourself. But what I will quickly do is show you how I wrote this blog post.

You can find the actual source file for this blog post right here. Nothing special right? Wrong! The top four lines contain what we call 'YAML front matter', and they tell Jekyll which layout to use and the pages title. You can find the layout here and can see on line 66 where we include this blog post file.

Now I just load up my command line and cd to my 'joelmoss_pages' working directory, and then 'git push'. Codaset will then run Jekyll against my new blog post when I push it to the server. Which means that my blog post is published almost as soon as I push to my Git repository. There's just something so bloody wicked about that!

And that, my friends is it! I have full control over what my blog post looks like, as I can edit the html directly and with very little effort. And I can very easily create any other pages of content just as quickly. I don't have to mess about with the restrictions of a blog engine such as Wordpress, and there are no version upgrades to mess around with. I also don't have to look at the messiest, spaghettiest codebase in the open source community, every time I want to add some sort of plugin.


Quick and easy emailing in Ruby with Pony
Article posted by Joel Moss on 01 Dec 2009   |  

I needed to send an email to all the users of Codaset, and wanted a quick and easy way to do so. So I just wrote a quick Rake task that will loop through all the users in the Codaset database, and send each one an email. However, when I was looking at how to send the emails, there didn't seem to be an easy way to do so without having to write several lines of code just to send one email. What I needed was something similar to PHP's mail() function which lets me send an email with one line of code:

mail('you@example.com', 'My Subject', $message);

I don't need to use an SMTP server to send it, as it works just fine with sendmail.

Unfortunately, Ruby and Rails built in mail classes don't offer such simplicity, which is very rare. But then I found Pony, which is a Ruby gem that mimics PHP's mail function. Now I can can send an email in Ruby with one short line of code, and no configuration needed:

Pony.mail(:to => 'you@example.com', :from => 'me@example.com', :subject => 'hi', :body => 'Hello there.')

Easy peasy!

For those of you who are interested, here is my rake task in its entirety:

require "pony"
require "erb"

desc "Send an email to all users"
task :email_users => :environment do
  raise "No template provided. Please set TEMPLATE=file_name" if ENV['TEMPLATE'].blank?

  template  = File.read ENV['TEMPLATE']
  subject   = ENV['SUBJECT'] || "Hello from Codaset"
  
  if ENV['TEST']
    name = 'Joel Test'
    Pony.mail :to => 'joel@developwithstyle.com', :from => "Codaset <help@codaset.com>", :subject => subject, :body => ERB.new(template).result(binding)
    puts "Email test ('#{subject}') sent to joel@developwithstyle.com"
  else
    User.find_each do |user|
      unless user.email.blank?
        name = user.title
        Pony.mail :to => user.email, :from => "Codaset <help@codaset.com>", :subject => subject, :body => ERB.new(template).result(binding)
        puts "Email ('#{subject}') sent to #{user.email}"
      end
    end
  end
end

This simply loops through each user in the database, and sends them an email. The body of the email is built using a templated file, that uses ERB. Which means I can use it as a normal view template. I run it like this:

rake email_users RAILS_ENV=production TEMPLATE=my_email_template.txt SUBJECT='Look at my email i sent ya!'

Concerned with Rails
Article posted by Joel Moss on 13 Oct 2009   |  

Concerns is a simple Rails plugin that provides you with a simple way to organise your Controllers, Models and Mailers, and split them into smaller chunks of logic. It is especially useful when you have lengthly models, and get fed up with having to scroll through several hundred lines of code.

How does it work?

So let's say we have a Post model (doesn't everyone?!) which is getting a bit lengthy, and frankly not very nice to look at. With the Concerns plugin, we can split it up into nice little chunks. Because we have lots of validations, let's start by pulling them out and placing them within a concern file.

Let's get going then...

First of all, install the plugin:

script/plugin install git://codaset.com/joelmoss/rails-concerns.git

Then, create a new directory in your app/models drectory and call it "post", which is the same name as your model.

Within this new directory, create a new file at app/models/post/validations.rb. Now all this should do is reopen your Post model, and define your validations like this:

class Post < ActiveRecord::Base
  validates_presence_of :title, :body
  validates_uniqueness_of :title, :scope => :project_id, :case_sensitive => false
  validates_exclusion_of :title, :in => %w(edit new blog delete destroy create update post posts)
  validates_inclusion_of :markup_language, :in => %w( markdown textile wikitext ), :allow_nil => true
end

It's just like writing your model again.

Now within your main Post model; right at the top, we simply call:

concerned_with :validations

Multiple concerns can be called like so:

concerned_with :validations, :class_methods

And we're done!

You can do this as many times as you wish, and with as many concerns as you want. And it works with models, controllers and mailers. Need help?

Grab and/or fork the code from the Codaset project


Codaset Opens up its Beta to Everyone.
Article posted by Joel Moss on 06 Oct 2009   |  

This is a reprint of an announcement made a few minutes ago at Codaset.com...

I am very pleased to announce that Codaset has ended it's private beta phase, and is now open to all. The beta tag is still in place, so the site is effectively in public beta. This means that no invite is needed to register and create an account. Anyone can create projects, and anyone can take part in any activity on the site.

Along with this announcement, also brings with it a few bug fixes, and minor improvements. Nothing huge, but you should find that browsing the site in Safari is much improved, and several styling bugs have been fixed. I also added URL slugs to milestones, which means your Milestones look prettier and are more familiar to browse.

So I am hoping that with this announcement and release, we will start to see an increase in the number of registered users, and active users. And of course, Codaset is one step closer to a final release. So if there are any features you want to see in the final release; and if there is not already a ticket for it, please create one now. If there is a ticket already created for your requested feature, please vote it up. Voting for tickets is the best way for me to see what you want.

You can see Codaset's tickets at http://codaset.com/tickets



Recent Comments