<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Biodegradable Geek &#187; Snippets</title>
	<atom:link href="http://biodegradablegeek.com/category/snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://biodegradablegeek.com</link>
	<description></description>
	<lastBuildDate>Tue, 22 Jun 2010 21:52:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Calculate Your GPA Using this Bash Script</title>
		<link>http://biodegradablegeek.com/2009/05/calculate-your-gpa-using-this-bash-script/</link>
		<comments>http://biodegradablegeek.com/2009/05/calculate-your-gpa-using-this-bash-script/#comments</comments>
		<pubDate>Thu, 21 May 2009 10:18:18 +0000</pubDate>
		<dc:creator>Isam</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Code example]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[Scripts]]></category>

		<guid isPermaLink="false">http://biodegradablegeek.com/?p=433</guid>
		<description><![CDATA[This Bash script is used to calculate your Grade Point Average (GPA) on the command line. Usage might not be intuitive. Please see the usage function or just run the script without passing it any arguments.
The gval function should be edited to reflect your own region or university. It has been written and tested on [...]]]></description>
			<content:encoded><![CDATA[<p>This Bash script is used to calculate your Grade Point Average (GPA) on the command line. Usage might not be intuitive. Please see the usage function or just run the script without passing it any arguments.</p>
<p>The <strong>gval</strong> function should be edited to reflect your own region or university. It has been written and tested on Bash 3.2.48.</p>
<pre class="brush: bash;">
#!/bin/sh
#
# Bash GPA calculator
#
# Isam | r0cketjump@yahoo.com | biodegradablegeek.com
# 05/21/2009 - Just another 4 AM project

function usage {
  echo -e &quot;\nBASH GPA Calculator&quot;
  echo
  echo -e &quot;\tAccepts an even # of arguments in the form of C G C G C G ...&quot;
  echo -e &quot;\t (C = number of credits, G = grade for the course)&quot;
  echo
  echo -e &quot;\tExample: You got a B+ in a 4 credit course, &quot;
  echo -e &quot;\t         an A in a 3 credit course, etc..&quot;
  echo
  echo -e &quot;\tUSAGE: $0 4 B+ 3 A 3 F 3 B-&quot;
  echo
  echo &quot;Acceptable grades are A B C D F WU (eq to F)&quot;
  echo
}

function calc {
  echo `echo &quot;scale=3; $1&quot; | bc`
}

function gval {
  grade=`echo &quot;$1&quot; | tr [a-z] [A-Z]`
  case $grade in
    A+ ) echo '4.3';;
    A ) echo '4';;
    A- ) echo '3.7';;

    B+ ) echo '3.3';;
    B ) echo '3.00';;
    B- ) echo '2.7';;

    C+ ) echo '2.3';;
    C ) echo '2.0';;
    C- ) echo '1.7';;

    D+ ) echo '1.3';;
    D ) echo '1.0';;
    D- ) echo '0.7';;

    F ) echo '0';;
    WF ) echo '0';;
    WU ) echo '0';;
  esac
}

# check # of arguments. is it even?
let MOD=$#%2
if [ ! $MOD -eq 0 ]; then
  usage
  exit
elif [ $# -eq 0 ]; then
  usage
  exit
fi

args=($@)
n=${#args[@]}

points=0
credits=0

for ((i=0;i&lt;$n-1;i+=2)); do
  k=${i}

  creds=${args[$k]}
  cgrade=${args[$k+1]}

  # convert cgrade (C-) to a number
  grade=`gval $cgrade`
  pts=`calc $grade*$creds`

  echo &quot;$creds * $cgrade ($grade) = $pts&quot;

  points=`calc $points+$pts`
  credits=`calc $credits+$creds`
done

gpa=`calc $points/$credits`
echo &quot;------------&quot;
echo &quot;Total points  = $points&quot;
echo &quot;Total credits = $credits&quot;
echo &quot;------------&quot;
echo &quot;** GPA (pts/crd) = $gpa&quot;
echo &quot;------------&quot;
</pre>
<p>(Script uses <strong>bc</strong> as the calculator. Change that in the calc function if you need to.)</p>
<p>I&#8217;ll never get used to Bash&#8217;s ugly ass syntax.  &#8230; esac?</p>
]]></content:encoded>
			<wfw:commentRss>http://biodegradablegeek.com/2009/05/calculate-your-gpa-using-this-bash-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Maintain Static Sites with Git &amp; Jekyll</title>
		<link>http://biodegradablegeek.com/2009/03/how-to-maintain-static-sites-with-git-jekyll/</link>
		<comments>http://biodegradablegeek.com/2009/03/how-to-maintain-static-sites-with-git-jekyll/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 04:10:00 +0000</pubDate>
		<dc:creator>Isam</dc:creator>
				<category><![CDATA[Automation]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Productivity]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://biodegradablegeek.com/?p=371</guid>
		<description><![CDATA[Static sites in this context just means non-database driven sites. Your static site can be an elaborate PHP script or just a few markup and image files. For this I am using Jekyll &#8211; A neat Ruby gem that makes your static sites dynamic. It lets you create layouts and embed custom variables in your [...]]]></description>
			<content:encoded><![CDATA[<p>Static sites in this context just means non-database driven sites. Your static site can be an elaborate PHP script or just a few markup and image files. For this I am using <strong><a href="http://github.com/mojombo/jekyll/tree/master">Jekyll</a> &#8211; A neat Ruby gem that makes your static sites dynamic.</strong> It lets you create layouts and embed custom variables in your HTML (this is a &#8220;prototype&#8221; of the site). </p>
<p>Jekyll tackles all the nuisances involved in creating static pages (I used to add just enough PHP to make a layout). It works by running your prototype through some parsers and outputs plain static HTML/XML (RSS feeds) etc. It&#8217;s perfect for lightweight sites that would be impractical on Wordpress, like a few static pages of information, landing pages, portfolio/resume pages, and parked domains. </p>
<p>Git takes care of keeping your development (local) and production (remote) environments synced. Git might be a little confusing if you&#8217;re learning it with the mindset that it works like Subversion. </p>
<p><strong>I&#8217;ll update this post when the guide is done. For now, the following will assume you&#8217;re familiar with Jekyll (or at least have an empty file in the prototype directory) and git. This Bash script simplifies creating the remote git repository:</strong></p>
<p>** please read through the code and make sure you know what this does, and what you&#8217;re doing. As of now, this is bias towards my own Apache/vhost setup. It&#8217;s trivial to edit for your specific needs. <strong>You&#8217;re using this at your own risk</strong>.</p>
<p>(<a href="http://code.biodegradablegeek.com/repogen.sh" target="_blank">direct link &#8211; repogen.sh</a>)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #666666; font-style: italic;"># </span>
<span style="color: #666666; font-style: italic;"># 04/01/2009 | http://biodegradablegeek.com | GPL </span>
<span style="color: #666666; font-style: italic;"># </span>
<span style="color: #666666; font-style: italic;"># You should be in site (NOT public) root (be in same dir as public/ log/ etc)</span>
<span style="color: #666666; font-style: italic;"># proto/ is created and will house the jekyll prototype</span>
<span style="color: #666666; font-style: italic;"># public/ will be the generated static site</span>
<span style="color: #666666; font-style: italic;"># the public/ folder will be REMOVED and regenerated on every push</span>
<span style="color: #666666; font-style: italic;"># </span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Usage: ./repogen.sh domain.comn&quot;</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># optional. will make it easier to copy/paste cmd to clone repo </span>
<span style="color: #007800;">SSHURL</span>=<span style="color: #ff0000;">&quot;ssh.domain.com&quot;</span>
<span style="color: #007800;">URL</span>=<span style="color: #ff0000;">&quot;$1&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;** creating tmp repo&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> proto
<span style="color: #7a0874; font-weight: bold;">cd</span> proto
git init 
<span style="color: #c20cb9; font-weight: bold;">touch</span> INITIAL
git add INITIAL
git commit <span style="color: #660033;">-a</span> <span style="color: #660033;">-m</span> <span style="color: #ff0000;">&quot;Initial Commit&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;** creating bare repo&quot;</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> ..
git clone <span style="color: #660033;">--bare</span> proto proto.git
<span style="color: #c20cb9; font-weight: bold;">mv</span> proto proto.old
git clone proto.git
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> proto.old
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;** generating hook&quot;</span>
<span style="color: #007800;">HOOK</span>=proto.git<span style="color: #000000; font-weight: bold;">/</span>hooks<span style="color: #000000; font-weight: bold;">/</span>post-update
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #007800;">$HOOK</span> <span style="color: #000000; font-weight: bold;">/</span>tmp
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'#!/bin/sh'</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'# To enable this hook, make this file executable by &quot;chmod +x post-update&quot;.'</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'#exec git-update-server-info'</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">''</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">''</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'URL='</span><span style="color: #ff0000;">&quot;<span style="color: #007800;">$URL</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'PROTO=&quot;/home/$USER/www/$URL/proto&quot;'</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'PUBLIC=&quot;/home/$USER/www/$URL/public&quot;'</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span>  <span style="color: #ff0000;">''</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'export GIT_DIR=&quot;$PROTO/.git&quot;'</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'pushd $PROTO &gt; /dev/null'</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'git pull'</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'popd &gt; /dev/null'</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">''</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;echo -----------------------------&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;echo '** Pushing changes to '<span style="color: #007800;">$URL</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;echo '** Moving current public to /tmp'&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'mv &quot;$PUBLIC&quot; &quot;/tmp/'</span><span style="color: #007800;">$URL</span><span style="color: #ff0000;">'public-`date '</span>+<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #ff0000;">'`&quot;'</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'echo &quot;** Generating new public&quot;'</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'jekyll &quot;$PROTO&quot; &quot;$PUBLIC&quot;'</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$HOOK</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;** enabling hook&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">chmod</span> a+x <span style="color: #007800;">$HOOK</span> 
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;** clone repo on local machina. example:&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;git clone ssh://<span style="color: #007800;">$USER</span>@<span style="color: #007800;">$SSHURL</span>/~<span style="color: #007800;">$USER</span>/www/<span style="color: #007800;">$SSHURL</span>/proto.git&quot;</span></pre></div></div>

<p><strong>Usage</strong></p>
<p>Your site structure might be different. <strong>repogen.sh</strong> is made by pasting the above code in a new file, and then chmod a+x to make it executable. This should be done on the remote server.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> www<span style="color: #000000; font-weight: bold;">/</span>domain.com<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">ls</span>
public<span style="color: #000000; font-weight: bold;">/</span> private<span style="color: #000000; font-weight: bold;">/</span> log<span style="color: #000000; font-weight: bold;">/</span> cgi-bin<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
.<span style="color: #000000; font-weight: bold;">/</span>repogen.sh domain.com</pre></div></div>

<p>Now on your local machine, clone the new repo, move your files in, and push:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git clone <span style="color: #c20cb9; font-weight: bold;">ssh</span>:<span style="color: #000000; font-weight: bold;">//</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>username<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">@</span>ssh.domain.com<span style="color: #000000; font-weight: bold;">/</span>~<span style="color: #7a0874; font-weight: bold;">&#91;</span>username<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>domain.com<span style="color: #000000; font-weight: bold;">/</span>proto.git
<span style="color: #7a0874; font-weight: bold;">cd</span> proto<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #ff0000;">&quot;hello, world&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> index.htm
git add index.htm
git commit <span style="color: #660033;">-a</span> <span style="color: #660033;">-m</span> <span style="color: #ff0000;">'first local commit'</span>
git push</pre></div></div>

<p>After you push your changes, the post-update hook will delete the public/ directory (the root of the site). This dir and its contents are automatically generated and will get wiped out on EVERY push. Keep this in mind. All your changes and content should reside in proto/. </p>
<p>The proto/ repo will pull in the new changes, and then Jekyll will be invoked to generate the updated site in public/ from the prototype.</p>
<p>Should you need to edit it, the <strong>post-update hook</strong> is in the bare git repo (proto.git/hooks/)</p>
<p>Thanks to the authors in the posts below for sharing ideas. I first read this git method on dmiessler&#8217;s site. </p>
<p><strong>Resources:</strong><br />
<a href="http://dmiessler.com/blog/using-git-to-maintain-your-website">dmiessler.com &#8211; using git to maintain static pages</a><br />
<a href="http://toroid.org/ams/git-website-howto">toroid.org &#8211; using git to manage a web site</a><br />
<a href="http://github.com/mojombo/jekyll/tree/master">Jekyll @ GitHub</a><br />
<a href="http://media.pragprog.com/titles/tsgit/chap-005-extract.html">git info</a><br />
<a href="http://www.nardol.org/2009/2/19/git-basics-reversing-the-git-sucks-effect">more git info</a></p>
]]></content:encoded>
			<wfw:commentRss>http://biodegradablegeek.com/2009/03/how-to-maintain-static-sites-with-git-jekyll/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Refactoring Tip: Eliminating Model.find(params[:id]) Duplication</title>
		<link>http://biodegradablegeek.com/2008/12/refactoring-tip-eliminating-modelfindparamsid-duplication/</link>
		<comments>http://biodegradablegeek.com/2008/12/refactoring-tip-eliminating-modelfindparamsid-duplication/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 15:59:36 +0000</pubDate>
		<dc:creator>Isam</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[refactoring tips]]></category>

		<guid isPermaLink="false">http://biodegradablegeek.com/?p=293</guid>
		<description><![CDATA[In a controller, you&#8217;ll commonly have a method that requires you have an instance variable containing the object you&#8217;re working with. An example would be the show, edit, update, and destroy methods (REST).
To eliminate having find(params[:id]) in multiple methods, you can use before_filter, like this:

class Admin::PostsController &#60; Admin::ApplicationController
  before_filter :find_post, :only =&#62; &#91;:show, :edit, [...]]]></description>
			<content:encoded><![CDATA[<p>In a controller, you&#8217;ll commonly have a method that requires you have an instance variable containing the object you&#8217;re working with. An example would be the <strong>show</strong>, <strong>edit</strong>, <strong>update</strong>, and <strong>destroy</strong> methods (REST).</p>
<p>To eliminate having find(params[:id]) in multiple methods, you can use before_filter, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">Admin::PostsController</span> <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Admin::ApplicationController</span>
  <span style="color:#5A0A0A; font-weight:bold;">before_filter</span> <span style="color:#ff3333; font-weight:bold;">:find_post</span>, <span style="color:#ff3333; font-weight:bold;">:only</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:show</span>, <span style="color:#ff3333; font-weight:bold;">:edit</span>, <span style="color:#ff3333; font-weight:bold;">:update</span>, <span style="color:#ff3333; font-weight:bold;">:destroy</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  rescue_from<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::RecordNotFound</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#5A0A0A; font-weight:bold;">render</span> <span style="color:#ff3333; font-weight:bold;">:text</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;&lt;h2&gt;Post not found&lt;/h2&gt;&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> index
    <span style="color:#0066ff; font-weight:bold;">@posts</span> = Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#5A0A0A; font-weight:bold;">show</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#5A0A0A; font-weight:bold;">new</span>
    <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#5A0A0A; font-weight:bold;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> create
    <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#5A0A0A; font-weight:bold;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> edit
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#5A0A0A; font-weight:bold;">update</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#5A0A0A; font-weight:bold;">destroy</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
protected 
  <span style="color:#9966CC; font-weight:bold;">def</span> find_post<span style="color:#006600; font-weight:bold;">&#40;</span>id = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>(Thanks Jon)</p>
]]></content:encoded>
			<wfw:commentRss>http://biodegradablegeek.com/2008/12/refactoring-tip-eliminating-modelfindparamsid-duplication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Javascript to Populate Forms During Development</title>
		<link>http://biodegradablegeek.com/2008/12/using-javascript-to-populate-forms-during-development/</link>
		<comments>http://biodegradablegeek.com/2008/12/using-javascript-to-populate-forms-during-development/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 15:43:41 +0000</pubDate>
		<dc:creator>Isam</dc:creator>
				<category><![CDATA[Automation]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://biodegradablegeek.com/?p=291</guid>
		<description><![CDATA[During development, working with forms quickly gets annoying because you have to constantly fill in each field, sometimes with unique info. One way around this is to write a little Javascript code that just populates the fields. I use something like this on the bottom of the form. I had jQuery no-conflict mode on in [...]]]></description>
			<content:encoded><![CDATA[<p>During development, working with forms quickly gets annoying because you have to constantly fill in each field, sometimes with unique info. One way around this is to write a little Javascript code that just populates the fields. I use something like this on the bottom of the form. I had jQuery no-conflict mode on in this case. In your app you might be able to get away replacing _j() with $():</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">if</span> ENV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'RAILS_ENV'</span><span style="color:#006600; font-weight:bold;">&#93;</span>==<span style="color:#996600;">'development'</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span>
&lt;!-- Generate random field data --&gt;
&lt;script lang=&quot;text/javascript&quot;&gt;
  jQuery(function() {
    if (typeof(_j)=='undefined') _j = jQuery;
    function randstr() {
      return Math.floor(Math.random()*99999)
    }
&nbsp;
    _j('#name').val('Chuck Norris');
    _j('#company_name').val('Roundhouse LLC');
    _j('#email').val('moo'+randstr()+'@yawhoo.com');
    _j('#login').val('user'+randstr());
    _j('#password').val('admin');
    _j('#password_confirmation').val('admin');
  })
&lt;/script&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://biodegradablegeek.com/2008/12/using-javascript-to-populate-forms-during-development/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to POST Form Data Using Ruby</title>
		<link>http://biodegradablegeek.com/2008/04/how-to-post-form-data-using-ruby/</link>
		<comments>http://biodegradablegeek.com/2008/04/how-to-post-form-data-using-ruby/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 15:43:09 +0000</pubDate>
		<dc:creator>Isam</dc:creator>
				<category><![CDATA[Automation]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Scraping]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://biodegradablegeek.com/2008/04/24/how-to-post-form-data-using-ruby/</guid>
		<description><![CDATA[POSTing data on web forms is essential for writing tools and services that interact with resources already available on the web. You can grab information from your Gmail account, add a new thread to a forum from your own app, etc. 
The following is a brief example on how this can be done in Ruby [...]]]></description>
			<content:encoded><![CDATA[<p><strong>POST</strong>ing data on web forms is essential for writing tools and services that interact with resources already available on the web. You can grab information from your Gmail account, add a new thread to a forum from your own app, etc. </p>
<p>The following is a brief example on how this can be done in Ruby using <a href="http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html" target="_blank">Net::HTTP</a>and <a href="http://www.interlacken.com/webdbdev/ch05/formpost.asp" target="_blank">this POST form example</a>.</p>
<p>Looking at the source (interlacken.com/webdbdev/ch05/formpost.asp):</p>
<pre class="brush: xml;">
&lt;form method=&quot;POST&quot; action=&quot;formpost.asp&quot;&gt;
&lt;p&gt;&lt;input type=&quot;text&quot; name=&quot;box1″ size=&quot;20″ value=&quot;&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;button1″&gt;&lt;/p&gt;
&lt;/form&gt;
</pre>
<p>We see two attributes are sent to the formpost.asp script when the user hits the submit button: A textbox named <strong>box1</strong> and the value of the submit button, named <strong>Submit</strong>. If this form used a GET method, we would just fetch the URL postfixed with (for example) <strong>?box1=our+text+here</strong>. Fortunately, Ruby&#8217;s Net::HTTP makes posting data just as easy.</p>
<p>The Ruby code: </p>
<pre class="brush: ruby;">
#!/usr/bin/ruby

require &quot;uri&quot;
require &quot;net/http&quot;

params = {'box1′ =&gt; 'Nothing is less important than which fork you use. Etiquette is the science of living. It embraces everything. It is ethics. It is honor. -Emily Post',
'button1′ =&gt; 'Submit'
}
x = Net::HTTP.post_form(URI.parse('http://www.interlacken.com/webdbdev/ch05/formpost.asp'), params)
puts x.body

# Uncomment this if you want output in a file
# File.open('out.htm', 'w') { |f| f.write x.body }
</pre>
<p>Sending the value of button1 is optional in this case, but sometimes this value is checked in the server side script. One example is when the coder wants to find out if the form has been submitted &#8211; as opposed to it being the user&#8217;s first visit to the form &#8211; without creating a hidden attribute to send along w/ the other form fields. Besides, there&#8217;s no harm in sending a few more bytes.</p>
<p>If you&#8217;re curious about URI.parse, it simply makes the URI easier to work with by separating and classifying each of its attributes, effectively letting the methods in Net::HTTP do their sole job only, instead of having to analyze and parse the URL. More info on this in the <a href="http://www.ruby-doc.org/stdlib/libdoc/uri/rdoc/classes/URI.html#M009241" target="_blank">Ruby doc</a>.</p>
<p>Assuming no errors, running this example (<em>ruby postpost</em> or <em>chmod a+x postpost.rb; ./postpost.rb</em>) yields:</p>
<pre class="brush: xml;">
&lt;form method=&quot;POST&quot; action=&quot;formpost.asp&quot;&gt;
&lt;p&gt;&lt;input type=&quot;text&quot; name=&quot;box1″ size=&quot;20″ value=&quot;NOTHING IS LESS
IMPORTANT THAN WHICH FORK YOU USE. ETIQUETTE IS THE
SCIENCE OF LIVING. IT EMBRACES EVERYTHING. IT IS ETHICS.
IT IS HONOR. -EMILY POST&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;button1″&gt;&lt;/p&gt;
&lt;/form&gt;
</pre>
<p>In practice, you might want to use a more specialized library to handle what you&#8217;re doing. Be sure to check out <a href="http://mechanize.rubyforge.org/mechanize/" target="_blank">Mechanize</a> and <a href="http://github.com/adamwiggins/rest-client/tree/master" target="_blank">Rest-client</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://biodegradablegeek.com/2008/04/how-to-post-form-data-using-ruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

