Rails Plugin - Jake's Rails Toolbox

Posted by admin 10/07/2007 at 13h56

hello all,

Ive recreated my old plugin and updated with some new features.

I created this plugin as a hodgepodge of different methods/hacks that i have found useful over the course of developing Rails Apps. I packaged it up as one plugin because most of the methods have nothing to do with each other, but i find them are very useful going from project to project.

Lets talk about them:

ActiveRecord Hacks:

1. Dynamic “nice_date”

Basically, i got tired of typing object.created_at.strftime(“%m-%d-%Y”) so i came up with the following method_missing hack for any Time attribute in an ActiveRecord object. Just prepend “nice_” in front of any Datetime attribute and you will get a nicely formatted date string. Plus some other goodies :)

Examples:

object.created_at                                          
# => Mon Dec 04 12:36:55 CST 2006
object.nice_created_at                                
# => "12-04-2006"  (mm-dd-YYYY)
object.nice_created_at(:euro)                 
# => "04-12-2006"  (dd-mm-YYYY)

plus, for you guys (and gals) that like using slashes instead of dashes:
object.nice_created_at(:slash)                 
# => "12/04/2006"  (mm/dd/YYYY)
object.nice_created_at(:euro_slash)     
# => "04/12/2006"  (dd/mm/YYYY)

and finally:
object.nice_created_at(:rss)                     
# => "Mon, 04 Dec 2006 12:36:55 -0600"  (rfc822)

NOTE
The attribute must be an instance of Time or a NoMethodError will be thrown

2. Dynamic SEO formatting.

Any method that returns a string can use this dynamic method. This method will create an SEO friendly string out of the attribute chosen (for use mainly in URLS). It will append the objects id (not the object_id, but the id key from the database) so that you can use params[:id] and not have to create new routes.

Example:

puts object.id                         
# => 1
object.title = " Rails is freaking awesome, didn't you know that?  " 
puts object.seo_title         
# => "1-rails-is-freaking-awesome-didnt-you-know-that" 

3. nice_name

This one is pretty simple. I found myself adding this same method to alot of my applications. Basically it looks for an attribute called “first_name” and one called “last_name” and combines them together with a space in between.

Example:

object.first_name    = "jake" 
object.last_name     = "varghese" 
object.nice_name  
# => "jake varghese" 

NOTE
If the method doesn’t have EITHER first_name or last_name, then a NoMethodError
will be thrown. It must have both attributes

4. create/new from XML

Create an ActiveRecord Object from XML. This method relies on the basics of AR’s current to_xml method (basically, it must be formatted the same).The differences between new_from_xml and create_from_xml are the same as the differences between AR.new and AR.create

If your XML string contains multiple records, it will parse out each record and create an object. The “multiple record” call returns an array like find(:all)

Example:

person = Person.new_from_xml(xml_string)
# => #<Project:0x2430758 @attributes={"prototype_url"=>nil, "company"=>"some value", 
"order_id"=>45564, "phone_number"=>555544455, "created_at"=>nil, 
"email_address"=>lkjlkjL@jfgg.com}, @new_record=true>

String Hacks:

1. SEO friendly.

This method is used in the AR seo hack. It takes a string and makes it seo friendly.

Example:


c.title = " Rails is freaking awesome, didn’t you know that? "
c.title.seo_friendly
# => “rails-is-freaking-awesome-didnt-you-know-that”

Float Hacks:

1. to_s override.

I really hate using sprintf, mainly because i always have to go online and look up the syntax. I figured i make that a little easier. Now you can print floats with different precision as easily as:

Example


4.123456.to_s(1) # => “4.1”
4.123456.to_s(3) # => “4.12”
4.123456.to_s(3) # => “4.123”
4.123456.to_s(4) # => “4.1235” (Note the auto rounding from 4.123456)
4.123456.to_s # => “4.123456”

Hash Hacks:

1. Method Missing hack to allow for easy hash referencing.

I can never remember whether the Hash i am playing with has symbols for keys or strings. I also dont like typing the brackets (not all text editors have the cool “auto close brace” feature). That’s why i came up with this method missing hack. Instead of explaining what it does, I’ll just show you.

Example:

hsh =  {"project"=>
        {
         "prototype_url"=>nil,
         "designer_id"=>2,
          "finished_at"=>nil,
          "phone_number"=>"512225555",
          "website"=>"http://www.ggg.com",
          "first_name"=>"test"
         }
        }
hsh.project                             
#=>  {"prototype_url"=>nil, "designer_id"=>2, "finished_at"=>nil, 
"phone_number"=>"512225555", "website"=>"http://www.ggg.com", 
"first_name"=>"test"}

hsh.project.prototype_url 
#=> nil
hsh.project.designer_id     
#=> 2
hsh.project.first_name         
#=> test

So instead of hsh[“project”] you can just write hsh.project

and that’s it. If you want to try it yourself, just run

script/plugin install http://jakes-rails-toolbox.googlecode.com/svn/trunk/jakes_toolbox 

You can also checkout the latest rev via SVN at:

svn checkout http://jakes-rails-toolbox.googlecode.com/svn/trunk/ vendor/plugins