[edge Rails] the rails command now supports templates

Setting up a new Rails project is pretty boring. Installing plugins, gems, adding initializers, etc. always the same monotonous work. - not fun. 

That’s why there have been quite a lot of starter apps. Most of them are git repositories with blank Rails apps bundled with plugins and extensions. But those are unflexible and the cloning of different repos somehow feels not right. - it’s better but still not fun. 

I’ve also tried to avoid the boring setup work with several tools. My first very, very hackish attempt was kickrails. A stupid ruby script that preceduraly runs all the build commands for me. - not fun either.

But then came RG. An awesome über cool project developed by Jeremy McAnally. RG allows you to kickstart your Rails app using templates written in ruby. - fun! ;) One command and your done. RG runs the rails command and setups all the stuff to get you started based on templates.

I’ve created a fork, added some more helpers and used it for several projects. It turned out great. 

Anyway what I wanted to tell is: RG just got added to Rails core. This means the rails command now supports templates for building your inital apps. Running rails -m/–template my_super_cool.template not only generates the default rails skeleton for you but also applies the template which installs plugins, gems, extensions, initializes a git repository, etc.  - pretty awesome - fun! 

I’m as excited as I’ve been when I’ve ran the rails command for the first time. 

How do you setup your rails app? Now compare that to:
RG template
Isn’t rails great?!

 

[edge Rails] :only/:except options for map.resources

Today a nice option for the routes was commited to the Rails edge.

map.resources now allows the option :only and :except to specify which routes should be created. Both options accept :none, :all, one action name or an array of action names.
For example:

map.resources :user, :only => :create
# GET /users fails
# only POST /users is available
 
map.resources :comments, :except => [:update, :destroy]
# GET /comments etc. works
# PUT /comments/1  fails
# DELETE /comments/1 fails

If you have a lot of resources in your routes this allows you to cut memory consumption.

[edge Rails] Inflector.parameterize for easy slug generation

David has just commited an parameterize method for easy slug generation. This means it strips out all special characters so that it can be savely used in URLs. It replaces anything but a-z and 0-9 with a “-” (you can pass a custom seperator).
Example to generate nice URLs for your models:

def to_param
  "#{id}-#{name.parameterize}"
end
"$%hello I'm a sentence with & a löt òf SPECIAL chars+".parameterize #=> -hello-i-m-a-sentence-with-a-lt-f-special-chars-

My find_by_param plugin, that helps you a lot with working with nice URLs, uses a custom encoding method to do this. Perhaps I will change that some time soon. ;)

Update:
The comments on the commit pointed to two really nice projects:
1. stringex - which is a bit of a overkill. *tries to solve everything* but creates really aweseome slugs by translating special chars ($ to dollar, etc.)
2. slugalizer - a ruby slugalizer which ses ActiveSupport for platform-consistent normalization. It also does nice formatting like: Åh, räksmörgåsar! => ah-raksmorgasar”

Update2
This feature was extended earlier today. Not nice conversions like Malmö = malmo or Garçons = garcons are supported.

[edge Rails] shallow nesting of routes

Ok, finally. This is the first post of a series about some new exciting edge rails features. ;)

Here at Railslove most of our projects are living on the edge (thanks to braid!) ;) and we try to keep close track of what’s happening in the rails master branch.

Today I’ve found a commit that is actually a few days old but this will clean up a lot of my nested routes.

Imagine your application has a User who has_many :posts which again has_many :comments. Your routes would look something like:

map.resources :users do |user|
  user.resources :posts do |post|
    post.resources :comments
  end
end

This defines the following helpers and URLs

users_url #=> /users/
user_posts_url #=> /users/1/posts/
user_post_comments_url #=> /users/1/posts/10/comments

however only the full nested routes are available.
/posts/10 or /comments/10 are not available and you need to declare those seperately:

map.resources :posts do |post|
  post.resources :comments
end
map.resources :comments

This commit now allows you to add a :shallow => true option which does this automatically for you. This is great and shortens the routes.rb a lot.
The example above would then just look like:

map.resources :users, :shallow => true do |user|
  user.resources :posts, :shallow => true do |post|
    post.resources :comments
  end
end

and post_url, comment_url, post_comments_url,… get also defined.

very nice!

Have a look at the commit message and source code for more information.

Update:
Georg of SalesKing.eu fame pointed me to Ryan’s great post about the :shallow option.

Technorati Profile