URL-based Breadcrumbs

February 23rd, 2007

UPDATE: This post has been moved over to Ruby Snips


iTunes Rating Menu Item?

February 22nd, 2007

Does anyone know if this exists?

I’m looking for a quick way to rate the currently playing track in iTunes without going deep into a menu or switching programs. Basically what I want looks like this:

Any ideas?


Google Sitemaps are an effective way to let Google know which pages of your website are available for indexing. Though they go to great lengths to point out that submitting a sitemap is only a suggestion, and not a command, Google does use your sitemap to provide insights into how your website is being indexed and found by web searchers.

Here’s how I added a Google Sitemap for Moral Metric in two shakes of a lamb’s tail.

First, I made sure that my HomeController was defined as a resource in my routes.rb file:


map.resource :home

My HomeController is very sparse – it only has one public method: index. I wanted to have my application return XML when Google asked for XML, so rather than adding a new method, I used Rails’ respond_to blocks:

1
2
3
4
5
6
7
def index
  respond_to do |format|
    @things = Thing.find(:all)
    format.html {}
    format.xml {render :partial => "sitemap", :layout => false}
  end
end

Easy enough. Then I created an RXML file in app/views/home/sitemap.rxml. Here I constructed my sitemap code according to Google’s Sitemap Protocol, which is very simple. The file looks something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
xml.instruct!
xml.urlset "xmlns" => "http://www.google.com/schemas/sitemap/0.84" do

  # Provide your site's homepage
  xml.url do
    xml.loc         "http://www.moralmetric.com/"
    xml.lastmod     w3c_date @articles.last.updated_at
    xml.changefreq  "hourly"
    xml.priority    "1.0"
  end

  # You can hand-specify important sub-sections of your site
  xml.url do
    xml.loc         "http://www.moralmetric.com/things"
    xml.lastmod     w3c_date @things.last.updated_at
    xml.changefreq  "daily"
    xml.priority    "0.9"
  end

  # You can iterate over items in your site, providing a link to each page
  @things.each do |thing|
    xml.url do
      xml.loc         thing_url(thing)
      xml.lastmod     w3c_date thing.updated_at
      xml.changefreq  "daily"
    end
  end

The w3c_date helper method is a convenience method from Codeism, to whom I also owe thanks for the inspiration for this technique.

That’s it! Now, going to /home.xml will pull up your newly minted XML format Google sitemap.

You could also ping Google each time something new is posted. See here.

Resources:


Ruby Block Goodness

February 14th, 2007

I uncover something new in Ruby nearly every day. Here’s a neat trick I learned today:

Suppose you have a User model, and that model has several methods that determine a user’s rights. Maybe something like User#can_delete?(object) and User#can_create_pages?.

Now suppose that in your view, you want to display a certain link only if a user is logged in AND if that user has rights to do that thing. So the delete link would look like this:

<%= link_to("delete", page_url(@page), :method => :delete) 
    if User.current_user and User.current_user.can_delete?(@page) %>

Yuk. Wordy. If you don’t check for User.current_user first, ActionView will raise an error because you’ll be requesting the can_delete? method from a nil object. So every time you want to display the delete link, you have to remember to process multiple conditions.

There is a better way.

In my ApplicationHelper, I defined a method called user?.


  def user?(*args, &block)
    return false unless User.current_user
    return true if args.first.blank?
    return User.current_user.send(*args, &block)
  end
  alias_method :user?, :user

Now we can rewrite that link code.

<%= link_to("delete", page_url(@page), :method => :delete) 
    if user(:can_delete?, @page) %>

Nice, eh?

We can do things like
user? #=> true | false
user(:can_create_pages?) #=> true | false
user(:can_edit?, @page) #=> true | false

The trick is in the *args variable. We pass the name of the method we’re calling as the first argument, and then any number of additional arguments, which will then be passed to the method specified in the first argument. Simple and clean.

[update] just fixed a couple of formatting issues.


Rapid Rails Setup

February 8th, 2007

If you create Rails projects regularly, or even occasionally, you probably find yourself repeating yourself. Importing the same plugins, uploading to the Subversion repository, etc.

No more…

Robert Evans has created an awesome shell script to automate all this drudgery. So stop repeating yourself and head over to Rapid Rails setup.