Archive for the 'coding' Category

Rails: Sake recipe: Rename Controller

Have you ever started writing an application to notice there is a typo in the controller name, or you want to name it something else without rewriting your code?

You could change your routes as so;

map.resources :tasks, :controller => "tasks"

But that’s messy. I’ve written a short sake task to rename all your files to the correct new name. It is your job however to rename the class/module titles. If you use testing, as you should, run the tests and you can catch most errors.

Install it,

sudo gem install sake
sake -i rename.sake

Then use it

sake rename:controller FROM=todo TO=tasks


The model task is causing a lot more troubles. So for that I will wait till I create a script/rename

0 Comments

Introducing: My Rails Plugins

I was waiting until I switch my blog until Rails only and then going to install Warehouse and list all my plugins there, however having to been forced at gunpoint (or at least metaphorical gunpoint) to try Git, I am starting to really appreciate the nuances that it brings over SVN. I won’t go into that now.

I’ve had many plugins lying around the place ready for consumption by Rails developers but now I shall start storing them online for everyone to use.

Clickable Error Messages

Currently a bit of a hack-job, was built before I even jumped on the REST boat and knew about @object.to_xml but essentially makes the fields in error_messages_for clickable.

Dashboard Location

With this plugin it takes 2 minutes (no, seriously) to add Basecamp-like urls to your project. Thanks goes to both David Heinemeier Hansson and Derek Haynes for their plugins.

Form Helper Fieldset

Simply wrap your inputs and labels in a fieldset block, passing a string aswell if you want a legend and it wraps it in fieldsets in the html for you.

Of course there are more to come as I can track down some of my plugins in the wild.

Get the plugins from GitHub here!

Any other downloadable work will go on my profile.

1 Comment

Stop With The Dreaded f blocks.

It happens in Rails’ generate scripts and it happens in a lot of plugins, but please, please, stop with the f blocks.

Too many times do I see:


form_for @bar do |f|

And even in the hpricot docs I see:


Hpricot(open(@url)) { |f| Hpricot f, :fixup_tags => true }

Why do I hate it so much?

The beauty of Ruby and the reason a lot of you use it is because it is pretty, easy to read and easy to write. It’s not Java. It’s not Ruby semantic, it’s just unreadable. F may stand for something, but to understand what it stands to, I have to read the earlier code.

8 Comments

[Host Review] Rails Machine

My final web host?

As with most people who work in the web industry I have an itching sensation to build tons of web applications from the many good ideas I have. My programming weapons of choice, Ruby on Rails and Merb are both a nightmare to deploy. Books even had to be written. Many people will simply not touch the beautiful language of Ruby for this reason alone.

Jamis built Capistrano in promise of easier deployent. The scope of the project was huge and due to it’s complexity and it being open-source, it was very hard to get working. The idea was right and in all theory it should have worked fine. But many people had troubles in one way or another. I was one of those people.

Originally I had a nice Slicehost server but proving to learn how to properly and securely set up a server got the better of me and I decided that it was best to get more of a provided solution. Of course, I wanted to go with Engine Yard but starting at $250 was a bit rich for my blood and I wasn’t sure how worth it, it was.

Not only does Capistrano work out of the box on RailsMachine, but they also have provided a gem to configure the deploy.rb configuration file and for extra tasks. It is incredibly easy to set up an application, or to make changes to one with Capistrano and RailsMachine.

RailsMachine have shown to be amazing hosts. The server setup was a little bit late outside of the given 24 hour setup period. I sent in an email at 24 hours past and at 25 hours past my server was set up and I was billed. The support is efficient, speedy and polite. I have asked quite a bit outside the official scope of support and they helped me along the way.

The plans that they offer are more recommendations, due to the fact that you can customise them very specifically (with price add-ons for better hardware/features) and I have heard that upgrading hardware takes less than an hour. Because of this, if your app is choking but you know that it doesn’t need a huge hardware update; you can slightly modify your server(s) without having to pay an arm and a leg for a system that you obviously do not need.

It would be nice to see a web panel but they hint that one is in the works (on their blog), so I will be watching out for that.

3 Comments

will_paginate sans ActiveRecord object

In one of my projects, I mix together two ActiveRecord objects for listing. There is two ways that I could have done this. A seperate model that enfolds my models or merging them with a concat. I did it this way as it only happened once so no need to create yet-another file.

After reading this post, I got a better idea of what to do. That code was OK but I refactored it into better, cleaner and more understandable code.


def list
foo = Foo.find :all
bar = Bar.find :all
foo_bar = foo.concat bar # merge the two arrays
foo_bar = foo_bar.sort_by { |foos_and_bars| foos_and_bars.created_at } # reorganize by created_at date
end

I then created a private method for pagination. I’ve stuck this in my local controller but the application-wide controller may be a better idea if you use it in different controllers.

Anyway, here be the code:

def paginate_array(array, per_page_param=25, page_number_param=0)
per_page_number = per_page_param.to_i == 0 ? 25 : per_page_param.to_i
page_number = page_number_param.nil? 0 : page_number_param.to_i
page_number = per_page_number * page_number

array_count = 0
array.collect { array_count += 1 } # Array does not have the count method so this works nicely.

WillPaginate::Collection.new(page_number, per_page_number, array_count).concat_array
end

6 Comments

Rails Tidbits: error_messages_for as a flash.

After not wanting to clutter my interface with the the default error_messages_for style I decided that I would display it as a flash message. I warn you though, it is not pretty.


errors = @folder.errors.to_a.collect { |key,value| key = key.capitalize + " " + value } # We have to do this because without it, there is no space and the key field is all lowercase.
flash[:error] = errors.to_sentence

0 Comments

error_messages_for Extended

I recently posted about my error_messages_for plugin. Since then I have turned it into a trouble ticket and also the extra’s added by Ryan McGeary.

Check out the new updates, on the svn.

0 Comments

Rails Plugins

Not only have I just set up a public HTTP SVN for my plugins (on Google Code as MediaTemple don’t do HTTP:// SVNs) …

I have created a new plugin that extends error_messages_for to give you clickable links to the form elements

The readme:

Clickable Error Messages
======================

Have a long form, or at least one that scrolls past the main page. Want to give your users more interactivity on the forms?

This is unobtrusive, no extra markup is required. If you remove it, your application will work as normal.

Example
=======

In your view:
<%= error_messages_for :user %>

Example goes here.

2 errors prohibited this user from being saved

There were problems with the following fields:

Copyright (c) 2007 Zach Inglis [www.zachinglis.com], released under the MIT license

Get it here: http://zachinglis-rails-plugins.googlecode.com/svn/trunk/svn/
I shall rename it to svn.zachinglis.com when (mt) get back to me.

2 Comments

Rails Hosting

Im currently about to release a butt-load of Rails applications onto my server but before I do, I have decided to evaluate the hosting solution I use.

I am currently on MediaTemple. Has anyone had experience using (mt)’s containers for production?

If I can’t use it decently, i’ll try one of these: (Reccomend them if you like ‘em)

6 Comments

Ruby on Rails » Helpers

I’ve started to archive the helpers that I use in Rails and thought i’d share them with the world.

You can find them here.

Although a small list, it’ll gradually get bigger.

Download them, use them in your work, modify them, share them, do whatever you want with them.

0 Comments