Nifty Methods

Nifty Methods: cycle(first_value, *values)

Recently I was working on an app and needed to "cycle" through some values. Previously, the code was using each_with_index and using that index to figure out where the "odd" or "even" class was applied to the table rows. Since I needed to change the code to use map I could no longer use each_with_index effectively.

First my thinking was to use modulo to loop through and add the classes, but that seemed to be complete overkill for my situation.

I then turned to google to find something simpler. It seems Rails already has a dead simple solution. It amazes me every time I find something that I don't feel is necessary to write from scratch already in existence.

The method is cycle(first_value, *values). This can be used for many things, but the most obvious is cycling through "even" and "odd" classes, but can also be used to more advanced arrays. The Rails API uses cycling through colors as an example. The method also can accept a Hash to create a named cycle.

If you're interested in reading more about this nifty little method, you can find more details in the Rails API.

Now I've Got Pretty Permalinks and Much More

Note: This blog post referred to an older version of this blog that I had built from scratch. I've attempted to update links so they don't 404, but know that this post is not currently accurate.

I'm off to a pretty good start with my yearly goals. It may not look like it but I've made a lot of changes under the hood to this blog. I was finding my ability to find myself on google pretty low so I added a sitemap — and it's not just any sitemap, it auto-updates using sweepers. In the next post I'll go over how to build the sitemap specifically.

A couple other new features are "pretty permalinks" and an RSS Feed. For the permalinks I used the Stringex gem. The gem allows you to more easily control your permalinks and comes with a nifty little method that allows you to create the URL's for already existing posts. I then made sure I added rewrite rules to my nginx config so that the old /posts/1 urls still functioned. Nginx rewrite rules are a little different from Apache as you can see in my exmaple below:

rewrite ^/posts/1(/)?$ /posts/hello-world permanent;

Lastly, I updated my 404, 422, and 500 pages to actually look like my site. Yipee!. I should have done that earlier, but when I built I wanted to get it live before the New Year.

Next set of changes will involve adding pages to the cms, including a contact form, ability to add images, and perhaps comments. Thanks for reading!

Categories: tips nifty-methods gems

Nifty Rails Methods: link_to_if, link_to_unless

I have for some time been wanting to make a "definitive list of awesome rails methods". There are so many great built in methods that I didn't realize existed when I was trying to build something from scratch. Sometimes it's annoying to find out that thing you're trying to build already exists - but if you haven't put 500 hours into it, it's usually a relief.

So today the featured definitve list of awesome rails methods are link_to_if and link_to_unless.

The nice thing about this method is it's great for changing links based on if a user is logged in, if the page is current, etc. Instead of writing an if else statement you can DRY it up by using link_to_if or link_to_unless. I found it fun to play around with and very useful as well. I personally used it for a complicated if else to mark a category for a prodcut as current. There is also an link_to_unless_current which I didn't end up using because it wouldn't mark both the current category and parent category as active, link_to_if worked much better for that.

Below is an example from the Rails API

  link_to_if(@current_user.nil?, "Login", { :controller => "sessions", :action => "new" }) do
     link_to(@current_user.login, { :controller => "accounts", :action => "show", :id => @current_user })

More about these methods can be found on the Rails API.