Site Redesign

NOV

15

2008

0 comments

My site has received a much-needed facelift. The original "design" was something I just slapped together when I was first building the site. It was really an afterthought because I was only concerned with the functionality of the site, and not so much with how it looked.

Of course, this design is not something that I'm capable of. Luckily Andrew, one of the designers at Plexus, needed someone to code his personal site. So we worked out a deal to trade code for design. I think he did a great job. The only drawback was that he wanted me to put a gaudy link to his site in my footer. Sorry.

Tagged: plexus, redesign

Stop! Haml Time!

NOV

23

2008

0 comments

Ok, first of all I have to apologize for the title of this post, but I couldn't resist the pun.

Anyway, as the title suggests, I've recently converted all of my erb pages on this site to haml. Haml is a DSL templating language uses to generate well-formed XHTML. The documentation site claims that you can become familiar with the syntax in about 20 minutes. I think, as long as you already know XHTML, you can pick it up in about 10 minutes. The concept is pretty simple. Each tag should be on its own line, and everything contained in that tag should be indented below it. If a tag doesn't have any other tags contained in it, then the content can be on the same line as the tag. Also, div is the default tag, so you can just pass an id or class name to output a div.

            %p This is a paragraph tag.
            %ul
              %li First list item
              %li Second list item
            #id_name Content in div
            .class_name Content in div
            

Will convert to (with beautiful formatting):

            <p>This is a paragraph tag.</p>
            <ul>
              <li>First list item</li>
              <li>Second list item</li>
            </ul>
            <div id="id_name">
              Content in div
            </div>
            <div class="class_name">
              Content in div
            </div>
            

It's really easy to include rails evaluation and output blocks too.

            - @posts.each do |post|
              %p
                Written by
                = post.author
              %p= post.date
            

Notice that there is no need for end when a block is done. When haml sees that the indentation has ended, it automatically ends the block.

You can also pass any attributes to an HTML tag via a ruby hash.

            %img{:src => "/images/picture.png", :alt => "Picture", :title => "Picture"}
            

I think the coolest thing about Haml is how it can assign a class and unique id to an element simply by passing an ActiveRecord object within square brackets.

            %div[@post]
            

Will output:

            <div class="post" id="post_18"><div>
            

If you'd like to learn more, you can take a look at the online documentation, or you can look at the code for my site on Github.

Tagged: haml, xhtml, ruby

Helpful Helpers

NOV

30

2008

1 comment

Here is a list of helper methods that we use a lot at Plexus to make our jobs easier. A few have been referenced in previous posts, but I thought I'd list them all here in one place.

Strip text for pretty URLs

You can use this method to replace all non-alphanumeric characters to dashes in a title or name when you're including them in the URL.

This is the route I use for my posts on this blog:

            m.post_details ':year/:month/:day/:title', :action => "by_date",
              :requirements => { :year => /(19|20)\d\d/, :month => /[01]?\d/, :day => /[0-3]?\d/},
              :day => nil, :month => nil, :title => nil
            

And here is the helper to strip out characters in the title when creating the URL:

            # Replace all non-alphanumeric characters with dashes
            def strip_chars(string='')
              string.gsub(/\s+/,'-').gsub(/[^a-z0-9\-]+/i, '')
            end
            

You can call the helper when you generate the route.

            post_details_path(:year => @post.date.year, :month => @post.date.month, :day => @post.date.day, :title => strip_chars(@post.title))
            

Generate a random password of n length

I've actually got a whole other post for just this helper, but decided to include it in this list.

            def generate_password(length=6)
              chars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ23456789'
              password = ''
              length.times { |i| password << chars[rand(chars.length)] }
              password
            end
            

It will generate a 6 digit password by default, but you can specify the desired length.

            generate_password
            => "Q6Kfze"
            
            generate_password(10)
            => "ZSJcmtRH5q"
            

Built-in rails helpers

These following helpers are built into rails, but you may not know about them.

Array#to_sentence

            ["this", "that", "the other"].to_sentence
            => "this, that, and the other"
            

number helpers

            number_to_currency(123456789)
            => "$123,456,789.00"
            
            number_to_human_size(123456789)
            => "117.7 MB"
            
            number_to_phone(1234567890, :area_code => true)
            => "(123) 456-7890"
            

There are a ton of string inflector helpers

            "under_scored".dasherize
            => "under-scored"
            
            "names_and_titles".humanize
            => "Names and titles"
            
            "lower case title".titleize
            => "Lower Case Title"
            

Tagged: helpers, rails, ruby


© 2008 Travis Roberts. All rights reserved.