Subscribe to the MPC blog rss feed feed-icon-14x14
 

Sluggable Posts

Posted on 06:44AM on 08/11/2008
Tags: blog, slug

I was putting it off, but I decided tonight to implement slug urls in the blog. It's still in beta mode, so I feel its ok to go ahead and make the change.

So, now instead of seeing urls like: http://mattpayne.ca/blog/post/3, the urls will look like: http://mattpayne.ca/blog/post/this-is-a-post

I think it looks much nicer and also somewhat hides the data implementation. For all anyone knows, the posts could ve stored in an xml file on the server.

0 Comments (Show) (Comments are closed for this post)

Counting Post Tags

Posted on 05:16AM on 08/11/2008
Tags: ruby, blog

I decided to adopt the simplest possible approach to recording blog entry tags. Each post has a string property called tags, which simply corresponds to a text field in the MySQL database. Each tag in the string is simply separated by a space. I'm the only one creating posts, so why bother with something more complicated? At some point I may need to breakdown and create a normalized data model for it, but for the time being I'm happy. In creating this site, I wanted to be able to create a tag cloud with each tag having a count of the number of posts it was applied to. This proved a fun challenge. Here's the code:

class Tag

  attr_reader :tag, :count

  def initialize(tag, count)
    @tag, @count = tag, count
  end

end

class Post < Base  

 #Other code ...

  def self.all_tags
    hash = self.all.inject({}) do |h, post|
      unless post.tags.blank?
        tags = post.tags.split(" ")
        tags.each {|t| h.key?(t) ? h[t] += 1 : h[t] = 1}
      end
      h
    end
    #shuffle just randomizes the array of tags
    hash.inject([]){|arr, (k,v)| arr << Tag.new(k, v); arr}.shuffle
  end

  #Other code ...

end

Overall, I'm happy with this approach. We'll see how well it scales.

0 Comments (Show) (Comments are closed for this post)

Getting a Sinatra App Running with Passenger

Posted on 05:35AM on 08/07/2008
Tags: sinatra, passenger, blog

So, I finally managed to get this site running properly. It's now using Apache and Passenger to serve. It's quite easy to get Apache and Passenger setup. Just google it and follow the instructions. More difficult, however, is the setting up of the rackup file since Sinatra runs under Rack. For posterity, here is my working config.ru for this site (with sensitve stuff changed):

 #I keep Sinatra in a vendor folder under the app root - like Rails
  vendor_path = File.expand_path(File.join(File.dirname(__FILE__), "vendor", "sinatra", "lib"))
local_path = File.expand_path(File.dirname(__FILE__))
$:.unshift(local_path) unless $:.include?(local_path)
$:.unshift(vendor_path) unless $:.include?(vendor_path)

require 'rubygems'
require 'sinatra'

Sinatra::Application.default_options.merge!(
  :run => false,
  :env => :production,
  :raise_errors => true,
  :sessions => true,
  :app_file => '/home/public_html/app/the_app.rb',
  :root => "/home/public_html/app",
  :views => "/home/public_html/app//views"
)

log = File.new("sinatra.log", "a")
STDOUT.reopen(log)
STDERR.reopen(log)

require '/home/public_html/app/the_app'
run Sinatra.application

This also allows for logging and enables sessions. The important thing to note is the setting of absolute paths for the application root and the view directory. This took me some time Googling, so maybe it will help someone in the future.

0 Comments (Show) (Comments are closed for this post)

Specs

Posted on 09:24PM on 08/06/2008
Tags: bdd, specs, blog

I know that I should be practicing BDD, and normally I do, but for this site I'm writing specs after the fact. BDD/TDD are awesome concepts and can be useful design tools, but they are somewhat useless when you are creating something without any upfront specs. Frankly, I started this site just by playing - to see what I liked and what I didn't. So now that it's at the barebones stage and I feel comfortable that what is here won't change much, I'm going to start writing specs. Going forward, as I think of new features that I'd like to add, I'll likely go at their creation from a BSS perspective.

0 Comments (Show) (Comments are closed for this post)

Added Integration To GitHub

Posted on 09:20PM on 08/06/2008
Tags: github, integration, blog

I use GitHub to manage my source code. The guys at GitHub have even provided an API so that various bits of data can be read programmatically. I used this API to read my list of repositories and provide links to them. See the right side of the main blog page.

0 Comments (Show) (Comments are closed for this post)

More

1 2 3
Please note that I am currently unavailable for any large, long term work.