<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Flvorful Bloggage: Tag hacks</title>
    <link>http://blog.flvorful.com/articles/tag/hacks</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Rails Plugin -  Jake's Rails Toolbox</title>
      <description>&lt;p&gt;hello all,&lt;/p&gt;

&lt;p&gt;Ive recreated my old plugin and updated with some new features.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Lets talk about them:&lt;/p&gt;

&lt;h3&gt;ActiveRecord Hacks: &lt;/h3&gt;

&lt;h4&gt;1. Dynamic &amp;#8220;nice_date&amp;#8221;&lt;/h4&gt;

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

    &lt;p&gt;&lt;strong&gt;Examples:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;
object.created_at                                          
# =&amp;gt; Mon Dec 04 12:36:55 CST 2006
object.nice_created_at                                
# =&amp;gt; "12-04-2006"  (mm-dd-YYYY)
object.nice_created_at(:euro)                 
# =&amp;gt; "04-12-2006"  (dd-mm-YYYY)

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

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

&lt;/pre&gt;    
&lt;p&gt;&lt;strong&gt;&lt;span class="caps"&gt;&lt;span class="caps"&gt;NOTE&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;
 The attribute must be an instance of Time or a NoMethodError will be thrown&lt;/p&gt;&lt;/p&gt;

&lt;h4&gt;2. Dynamic &lt;span class="caps"&gt;&lt;span class="caps"&gt;SEO&lt;/span&gt;&lt;/span&gt; formatting.&lt;/h4&gt;
&lt;p&gt;Any method that returns a string can use this dynamic method. This method will create an &lt;span class="caps"&gt;&lt;span class="caps"&gt;SEO&lt;/span&gt;&lt;/span&gt; friendly string out of the attribute chosen (for use mainly in &lt;span class="caps"&gt;&lt;span class="caps"&gt;URLS&lt;/span&gt;&lt;/span&gt;).  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.&lt;/p&gt;

    &lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;
puts object.id                         
# =&amp;gt; 1
object.title = " Rails is freaking awesome, didn't you know that?  " 
puts object.seo_title         
# =&amp;gt; "1-rails-is-freaking-awesome-didnt-you-know-that" 
&lt;/pre&gt;&lt;/p&gt;

&lt;h4&gt;3. nice_name&lt;/h4&gt;
&lt;p&gt;This one is pretty simple.  I found myself adding this same method to alot of my applications.  Basically it looks for an attribute called &amp;#8220;first_name&amp;#8221; and one called &amp;#8220;last_name&amp;#8221; and combines them together with a space in between.&lt;/p&gt;

    &lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;    &lt;/p&gt;
&lt;pre&gt;
object.first_name    = "jake" 
object.last_name     = "varghese" 
object.nice_name  
# =&amp;gt; "jake varghese" 
&lt;/pre&gt;
    &lt;p&gt;&lt;strong&gt;&lt;span class="caps"&gt;&lt;span class="caps"&gt;NOTE&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/strong&gt;
    If the method doesn&amp;#8217;t have &lt;span class="caps"&gt;&lt;span class="caps"&gt;EITHER&lt;/span&gt;&lt;/span&gt; first_name or last_name, then a NoMethodError 
    will be thrown.  It must have both attributes&lt;/p&gt;&lt;/p&gt;

&lt;h4&gt;4. create/new from &lt;span class="caps"&gt;&lt;span class="caps"&gt;XML&lt;/span&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;p&gt;Create an ActiveRecord Object from &lt;span class="caps"&gt;&lt;span class="caps"&gt;XML&lt;/span&gt;&lt;/span&gt;.  This method relies on the basics of AR&amp;#8217;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&lt;/p&gt;

&lt;p&gt;If your &lt;span class="caps"&gt;&lt;span class="caps"&gt;XML&lt;/span&gt;&lt;/span&gt; string contains multiple records, it will parse out each record and create an object.  The &amp;#8220;multiple record&amp;#8221; call returns an array like find(:all)

&lt;/p&gt;

    &lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;
person = Person.new_from_xml(xml_string)
# =&amp;gt; #&amp;lt;Project:0x2430758 @attributes={"prototype_url"=&amp;gt;nil, "company"=&amp;gt;"some value", 
"order_id"=&amp;gt;45564, "phone_number"=&amp;gt;555544455, "created_at"=&amp;gt;nil, 
"email_address"=&amp;gt;lkjlkjL@jfgg.com}, @new_record=true&amp;gt;

&lt;/pre&gt;&lt;/p&gt;

&lt;h3&gt;String Hacks:&lt;/h3&gt;
&lt;h4&gt;1.  &lt;span class="caps"&gt;&lt;span class="caps"&gt;SEO&lt;/span&gt;&lt;/span&gt; friendly.&lt;/h4&gt;
&lt;p&gt;This method is used in the AR seo hack.  It takes a string and makes it seo friendly.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;
&lt;pre&gt;
c.title = " Rails is freaking awesome, didn't you know that?  " 
c.title.seo_friendly        
# =&amp;gt; "rails-is-freaking-awesome-didnt-you-know-that" 
&lt;/pre&gt;&lt;/p&gt;

&lt;h3&gt;Float Hacks:&lt;/h3&gt;
&lt;h4&gt;1. to_s override.&lt;/h4&gt;
&lt;p&gt;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:&lt;/p&gt;
&lt;strong&gt;Example&lt;/strong&gt;
&lt;pre&gt;
4.123456.to_s(1)    # =&amp;gt; "4.1" 
4.123456.to_s(3)    # =&amp;gt; "4.12" 
4.123456.to_s(3)    # =&amp;gt; "4.123" 
4.123456.to_s(4)    # =&amp;gt; "4.1235" (Note the auto rounding from 4.123456)
4.123456.to_s       # =&amp;gt; "4.123456" 

&lt;/pre&gt;    

&lt;h3&gt;Hash Hacks:&lt;/h3&gt;
&lt;h4&gt;1.  Method Missing hack to allow for easy hash referencing.&lt;/h4&gt;
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 &amp;#8220;auto close brace&amp;#8221; feature).  That&amp;#8217;s why i came up with this method missing hack.  Instead of explaining what it does, I&amp;#8217;ll just show you.

    &lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;
hsh =  {"project"=&amp;gt;
                {
                    "prototype_url"=&amp;gt;nil,
            "designer_id"=&amp;gt;2,
          "finished_at"=&amp;gt;nil,
          "phone_number"=&amp;gt;"512225555",
          "website"=&amp;gt;"http://www.ggg.com",
          "first_name"=&amp;gt;"test" 
         }
        }
hsh.project                             
#=&amp;gt;  {"prototype_url"=&amp;gt;nil, "designer_id"=&amp;gt;2, "finished_at"=&amp;gt;nil, 
"phone_number"=&amp;gt;"512225555", "website"=&amp;gt;"http://www.ggg.com", 
"first_name"=&amp;gt;"test"}

hsh.project.prototype_url 
#=&amp;gt; nil
hsh.project.designer_id     
#=&amp;gt; 2
hsh.project.first_name         
#=&amp;gt; test

&lt;/pre&gt;
&lt;p&gt;So instead of hsh[&amp;#8220;project&amp;#8221;] you can just write hsh.project&lt;/p&gt;&lt;/p&gt;

&lt;p&gt;and that&amp;#8217;s it.  If you want to try it yourself, just run&lt;/p&gt;
&lt;pre&gt;script/plugin install http://jakes-rails-toolbox.googlecode.com/svn/trunk/jakes_toolbox &lt;/pre&gt;

&lt;p&gt;You can also checkout the latest rev via &lt;span class="caps"&gt;&lt;span class="caps"&gt;SVN&lt;/span&gt;&lt;/span&gt; at: &lt;/p&gt;

&lt;pre&gt;svn checkout http://jakes-rails-toolbox.googlecode.com/svn/trunk/ vendor/plugins &lt;/pre&gt;</description>
      <pubDate>Tue, 10 Jul 2007 13:56:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:d5135407-e1dc-49fd-a664-b2de70fc7316</guid>
      <author>jake</author>
      <link>http://blog.flvorful.com/articles/2007/07/10/rails-plugin-jakes-rails-toolbox</link>
      <category>Rails</category>
      <category>rails</category>
      <category>plugins</category>
      <category>hacks</category>
      <trackback:ping>http://blog.flvorful.com/articles/trackback/7</trackback:ping>
    </item>
  </channel>
</rss>
