<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3" -->
<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/"
	>

<channel>
	<title>Jason Martin's Software and Design</title>
	<link>http://www.jason-knight-martin.com/v2</link>
	<description>Just another WordPress weblog</description>
	<pubDate>Fri, 25 Apr 2008 12:36:23 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3</generator>
	<language>en</language>
			<item>
		<title>CSV Parser in Ruby</title>
		<link>http://www.jason-knight-martin.com/v2/?p=9</link>
		<comments>http://www.jason-knight-martin.com/v2/?p=9#comments</comments>
		<pubDate>Mon, 17 Mar 2008 03:31:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Frameworks]]></category>

		<category><![CDATA[Languages]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[Ruby On Rails]]></category>

		<category><![CDATA[Work]]></category>

		<category><![CDATA[csv]]></category>

		<category><![CDATA[parser]]></category>

		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.jason-knight-martin.com/v2/?p=9</guid>
		<description><![CDATA[So, I have written like a million versions of these in different languages, then I found out that Ruby has an in built CSV parser, so I said cool. I read through the Pickaxe, got the info, plugged it in and boom, no worky, I hate it when things no worky. So I spent the [...]]]></description>
			<content:encoded><![CDATA[<p>So, I have written like a million versions of these in different languages, then I found out that Ruby has an in built CSV parser, so I said cool. I read through the Pickaxe, got the info, plugged it in and boom, no worky, I hate it when things no worky. So I spent the next hour on google looking for how to fix it, people seem to not be having the problems I am having, that is, it just doesn&#8217;t work&#8230;weird, but fuck it, I have written enough of them to do it my damn self, all I wanted was to parse a csv and maintain the column headers so that I could dynamically create activerecord objects in my rails app and not have to worryabout enforcing column order, or parsing it in the controller. It took me about 20 minutes to write, and yes it shows it, heres the kicker, it works.</p>
<p>That is, like out of the box, plug and play, whatever, it works for me, so maybe it will work for you, that is, unless you want to stick with the poorly documented and wholly mystical ruby version, you might be better off, but I like doing things myself, that way, I completely understand how it works, why, and if I want to make a change I can.</p>
<p>Anyway, so here it is:</p>
<p>Using a csv like this for the example:</p>

<div class="wp_syntax"><div class="code"><pre>&quot;Song Name&quot;,&quot;Artist&quot;,&quot;Album&quot;,&quot;Duration&quot;
&quot;Girls And Boys&quot;,&quot;Good Charlotte&quot;,&quot;The Young and the Hopeless&quot;,03:05
&quot;Meet Virginia&quot;,&quot;Train&quot;,&quot;Unknown&quot;,03:59
&quot;Sacrifice&quot;,&quot;Elton John&quot;,&quot;Unknown&quot;,05:06</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> Kcsv
	<span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>file, options<span style="color:#006600; font-weight:bold;">&#41;</span>
		<span style="color:#0066ff; font-weight:bold;">@file</span> = file
		<span style="color:#0066ff; font-weight:bold;">@options</span> = options
		options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:header</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#0000FF; font-weight:bold;">true</span> ? <span style="color:#0066ff; font-weight:bold;">@headers</span> = parse_header_line : <span style="color:#0066ff; font-weight:bold;">@headers</span> = <span style="color:#0000FF; font-weight:bold;">false</span>
		<span style="color:#0066ff; font-weight:bold;">@children</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
		parse
	<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
	<span style="color:#9966CC; font-weight:bold;">def</span> parse
		i = <span style="color:#006666;">0</span>
		<span style="color:#0066ff; font-weight:bold;">@file</span>.<span style="color:#9900CC;">rewind</span>
&nbsp;
		<span style="color:#0066ff; font-weight:bold;">@file</span>.<span style="color:#9900CC;">each_line</span> <span style="color:#9966CC; font-weight:bold;">do</span> |line|
			<span style="color:#008000; font-style:italic;">#looping through each line of the file</span>
			<span style="color:#9966CC; font-weight:bold;">if</span> i &gt; <span style="color:#006666;">0</span> <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#008000; font-style:italic;"># we are past the first line</span>
				x = <span style="color:#006666;">0</span> <span style="color:#008000; font-style:italic;">#we set the index for our position in the headers.</span>
				row = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;">#the row</span>
				<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">not</span> line.<span style="color:#9966CC; font-weight:bold;">include</span>? <span style="color:#996600;">','</span> <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#008000; font-style:italic;">#Here we check to see if it is a single column</span>
					row<span style="color:#006600; font-weight:bold;">&#91;</span>@headers<span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = clean<span style="color:#006600; font-weight:bold;">&#40;</span>line<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">not</span> <span style="color:#0066ff; font-weight:bold;">@headers</span><span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>?
					row<span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span> = clean<span style="color:#006600; font-weight:bold;">&#40;</span>line<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@headers</span><span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>?
				<span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#008000; font-style:italic;">#we have multiple columns</span>
					line.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">','</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |column|
						<span style="color:#008000; font-style:italic;">#now we need to know if we have headers</span>
						<span style="color:#9966CC; font-weight:bold;">if</span> !@headers.<span style="color:#0000FF; font-weight:bold;">nil</span>? <span style="color:#9966CC; font-weight:bold;">then</span>
								row<span style="color:#006600; font-weight:bold;">&#91;</span>@headers<span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = clean<span style="color:#006600; font-weight:bold;">&#40;</span>column<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">not</span> <span style="color:#0066ff; font-weight:bold;">@headers</span><span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>?
						<span style="color:#9966CC; font-weight:bold;">else</span>
							row<span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span> = clean<span style="color:#006600; font-weight:bold;">&#40;</span>column<span style="color:#006600; font-weight:bold;">&#41;</span>
						<span style="color:#9966CC; font-weight:bold;">end</span>
						x = x + <span style="color:#006666;">1</span>
					<span style="color:#9966CC; font-weight:bold;">end</span>
				<span style="color:#9966CC; font-weight:bold;">end</span>
				<span style="color:#0066ff; font-weight:bold;">@children</span> &lt;&lt; row
			<span style="color:#9966CC; font-weight:bold;">else</span>
				i = i + <span style="color:#006666;">1</span> <span style="color:#008000; font-style:italic;">#just there to skipp the first line</span>
			<span style="color:#9966CC; font-weight:bold;">end</span>
		<span style="color:#9966CC; font-weight:bold;">end</span>
		<span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">true</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
	<span style="color:#9966CC; font-weight:bold;">def</span> to_a
		<span style="color:#0066ff; font-weight:bold;">@children</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
	protected
	<span style="color:#9966CC; font-weight:bold;">def</span> clean<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">string</span><span style="color:#006600; font-weight:bold;">&#41;</span>
		<span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#CC0066; font-weight:bold;">string</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'&quot;'</span>,<span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">strip</span>
	<span style="color:#9966CC; font-weight:bold;">end</span>
	<span style="color:#9966CC; font-weight:bold;">def</span> parse_header_line
		headers = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
		accepted_headers = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;ebay_id&quot;</span>,<span style="color:#996600;">&quot;image&quot;</span>,<span style="color:#996600;">&quot;price&quot;</span>,<span style="color:#996600;">&quot;quantity&quot;</span>,<span style="color:#996600;">&quot;title&quot;</span>,<span style="color:#996600;">&quot;desc&quot;</span>,<span style="color:#996600;">&quot;id&quot;</span>,<span style="color:#996600;">&quot;action&quot;</span>,<span style="color:#996600;">&quot;is_live&quot;</span>,<span style="color:#996600;">&quot;is_type&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
		<span style="color:#0066ff; font-weight:bold;">@file</span>.<span style="color:#9900CC;">rewind</span>
		<span style="color:#0066ff; font-weight:bold;">@file</span>.<span style="color:#9900CC;">each_line</span> <span style="color:#9966CC; font-weight:bold;">do</span> |line|
			x = <span style="color:#006666;">0</span>
			<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">not</span> line.<span style="color:#9966CC; font-weight:bold;">include</span>? <span style="color:#996600;">','</span> <span style="color:#9966CC; font-weight:bold;">then</span>
				headers &lt;&lt; <span style="color:#006600; font-weight:bold;">&#91;</span>clean<span style="color:#006600; font-weight:bold;">&#40;</span>line<span style="color:#006600; font-weight:bold;">&#41;</span>,x<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">if</span> accepted_headers.<span style="color:#9966CC; font-weight:bold;">include</span>? clean<span style="color:#006600; font-weight:bold;">&#40;</span>line<span style="color:#006600; font-weight:bold;">&#41;</span>
				headers &lt;&lt; <span style="color:#006600; font-weight:bold;">&#91;</span>x,x<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">not</span> accepted_headers.<span style="color:#9966CC; font-weight:bold;">include</span>? clean<span style="color:#006600; font-weight:bold;">&#40;</span>line<span style="color:#006600; font-weight:bold;">&#41;</span>
			<span style="color:#9966CC; font-weight:bold;">else</span>
				line.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">','</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |col|
					headers &lt;&lt; <span style="color:#006600; font-weight:bold;">&#91;</span>clean<span style="color:#006600; font-weight:bold;">&#40;</span>col<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">strip</span>,x<span style="color:#006600; font-weight:bold;">&#93;</span>
					x = x + <span style="color:#006666;">1</span>
				<span style="color:#9966CC; font-weight:bold;">end</span>
			<span style="color:#9966CC; font-weight:bold;">end</span>
			<span style="color:#9966CC; font-weight:bold;">break</span>
		<span style="color:#9966CC; font-weight:bold;">end</span>
		<span style="color:#0000FF; font-weight:bold;">return</span> headers
	<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;">#this is just to test that it works as expected, returning and array of arrays with to_a</span>
csv = Kcsv.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'songs.csv'</span>,<span style="color:#996600;">'r'</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#ff3333; font-weight:bold;">:header</span> =&amp;gt; <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
csv.<span style="color:#9900CC;">to_a</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |row|
	<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{row[&quot;</span>Song Name<span style="color:#996600;">&quot;]} by #{row[&quot;</span>Artist<span style="color:#996600;">&quot;]} from #{row[&quot;</span>Album<span style="color:#996600;">&quot;]} -- #{row[&quot;</span>Duration<span style="color:#996600;">&quot;]}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Anyway, there it is, have some fun. There maybe a better, or faster way, however, this is working, so I am happy enough.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jason-knight-martin.com/v2/?feed=rss2&amp;p=9</wfw:commentRss>
		</item>
		<item>
		<title>Ebay File Exchange .csv Gotcha Fixed with gsub</title>
		<link>http://www.jason-knight-martin.com/v2/?p=8</link>
		<comments>http://www.jason-knight-martin.com/v2/?p=8#comments</comments>
		<pubDate>Wed, 16 Jan 2008 16:21:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Misc]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[Ruby On Rails]]></category>

		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://www.jason-knight-martin.com/v2/?p=8</guid>
		<description><![CDATA[Hey All
Been awhile, I know. While working on one of my projects, it came up that we would need to import thousands of ebay items into the shop, and be able to export them using the ebay file exchange, which dumps out a .csv file. It seems .csv parsers are the most common task I [...]]]></description>
			<content:encoded><![CDATA[<p>Hey All</p>
<p>Been awhile, I know. While working on one of my projects, it came up that we would need to import thousands of ebay items into the shop, and be able to export them using the ebay file exchange, which dumps out a .csv file. It seems .csv parsers are the most common task I perform, I have written like a million for rails and php over the years, so I slapped something together quickly to do it and oops, I didn&#8217;t notice that ebay lists the item&#8217;s price with a comma. Ack, Comma Separated File, and there is a comma in one of the values, it&#8217;s easy to forget, but can really mess you up. Well, since I was using rails which means ruby, this is not such a problem, I can fix that easy enough with some regex.</p>
<p>To make a long story short, and since the price field was and will only forever be the offending field, I got away clean with a simple quick fix:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">fields = line.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span>/£<span style="color:#006600; font-weight:bold;">&#40;</span>\d+<span style="color:#006600; font-weight:bold;">&#41;</span>,<span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">&#41;</span>/,<span style="color:#996600;">&quot;<span style="color:#000099;">\\</span>1<span style="color:#000099;">\\</span>2&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">','</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Got rid of that offending , and the £ sign to boot since we save currency amounts as floats, the £ sign is independent of that, and we use number_to_currency for that. Simple I know, but man, what an effing pain sometimes.</p>
<p>Well, gotta get back to relearning Rails, with the release of 2.0, there&#8217;s so much new stuff that it&#8217;s driving me nuts. Personally, I don&#8217;t see 2.0 as an improvement, except for the caching stuff, and the sexy migrations, those are cool&#8230;but that&#8217;s it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jason-knight-martin.com/v2/?feed=rss2&amp;p=8</wfw:commentRss>
		</item>
		<item>
		<title>Scriptaculous Inline Edit in a php script to create mp3 playlists</title>
		<link>http://www.jason-knight-martin.com/v2/?p=7</link>
		<comments>http://www.jason-knight-martin.com/v2/?p=7#comments</comments>
		<pubDate>Wed, 24 Oct 2007 15:49:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Daily Life]]></category>

		<category><![CDATA[For Your Info]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[Languages]]></category>

		<category><![CDATA[Misc]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.jason-knight-martin.com/v2/?p=7</guid>
		<description><![CDATA[Well,
I got tired of linux mp3 players and their inability to manage mp3s, so I broke down
and started doing what I have been planning for along time, creating a mp3 playlist
creator to run on my local server. 
I may release it as a script, as so far, it&#8217;s pretty useful, but one thing
that came up [...]]]></description>
			<content:encoded><![CDATA[<p>Well,</p>
<p>I got tired of linux mp3 players and their inability to manage mp3s, so I broke down<br />
and started doing what I have been planning for along time, creating a mp3 playlist<br />
creator to run on my local server. </p>
<p>I may release it as a script, as so far, it&#8217;s pretty useful, but one thing<br />
that came up was, some of the mp3 filenames were not exactly clear. Like Track 13.mp3.<br />
I firmly believe in the mp3 naming standard of Artist ft. Others - Song Name - Alblum.mp3.</p>
<p>In order to do this with the script I got a fantabulous idea, let&#8217;s use scriptaculous to edit<br />
them in place, making it feel a bit more like an application.</p>
<p>I load all the files into an array, choosing the chunk I am going to list. Note, if you<br />
have thousands of mp3s, do not try to create thousands of inline edits, it will crash, seriously<br />
I tried it.</p>
<p>Instead, I paginate my files so I can surf through them easily, checking off the ones I want<br />
to add to my playlist, then the script moves them to the specified folder.</p>

<div class="wp_syntax"><div class="code"><pre class="php">  &lt;script src=<span style="color: #ff0000;">&quot;javascripts/prototype.js&quot;</span>&gt;&lt;/script&gt;
  &lt;script src=<span style="color: #ff0000;">&quot;javascripts/scriptaculous.js?load=effects,controls&quot;</span>&gt;&lt;/script&gt;
  &lt;script type=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> language=<span style="color: #ff0000;">&quot;javascript&quot;</span>&gt;
    <span style="color: #808080; font-style: italic;">// &lt;![CDATA[</span>
      Event.observe<span style="color: #66cc66;">&#40;</span>window, <span style="color: #ff0000;">'load'</span> , <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">&lt;?php</span> 
        <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span> = <span style="color: #0000ff;">$start</span>; <span style="color: #0000ff;">$i</span> &lt;= <span style="color: #0000ff;">$limit</span>;<span style="color: #0000ff;">$i</span>++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">?&gt;</span>
            <span style="color: #000000; font-weight: bold;">new</span> Ajax.InPlaceEditor<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'song_&lt;?php echo $i;?&gt;'</span>, <span style="color: #ff0000;">'rename.php?action=rename&amp;dir=&lt;?php echo $_GET['</span><span style="color: #000066;">dir</span><span style="color: #ff0000;">'];?&gt;'</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #000000; font-weight: bold;">&lt;?php</span>
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">?&gt;</span>
      <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// ]]&gt;</span>
  <span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></pre></div></div>

<p>There we go, now, I just loop through the array of songs, and place things in a paragraph<br />
with the necessary id:</p>

<div class="wp_syntax"><div class="code"><pre class="php">  &lt;table&gt;
    	<span style="color: #000000; font-weight: bold;">&lt;?php</span> 
        <span style="color: #808080; font-style: italic;">//This is the equivalent of say for instance cycle. It can</span>
        <span style="color: #808080; font-style: italic;">//be done a million different ways, this is one of them.</span>
        <span style="color: #808080; font-style: italic;">//It allows for an alternating background color.</span>
        <span style="color: #0000ff;">$x</span> = <span style="color: #cc66cc;">0</span>;
        <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$i</span> = <span style="color: #0000ff;">$start</span>; <span style="color: #0000ff;">$i</span> &lt;= <span style="color: #0000ff;">$limit</span>;<span style="color: #0000ff;">$i</span>++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
          <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$x</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">0</span>:
              <span style="color: #0000ff;">$x</span> = <span style="color: #cc66cc;">1</span>;
              <span style="color: #0000ff;">$style</span> = <span style="color: #ff0000;">'table-odd'</span>;
              <span style="color: #b1b100;">break</span>;
            <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span>:
              <span style="color: #0000ff;">$x</span> = <span style="color: #cc66cc;">0</span>;
              <span style="color: #0000ff;">$style</span> = <span style="color: #ff0000;">'table-even'</span>;
              <span style="color: #b1b100;">break</span>;
          <span style="color: #66cc66;">&#125;</span>
          <span style="color: #000000; font-weight: bold;">?&gt;</span>
            &lt;tr&gt;
              &lt;td valign=<span style="color: #ff0000;">&quot;top&quot;</span> colspan=<span style="color: #ff0000;">&quot;3&quot;</span>&gt;
              &lt;a name=<span style="color: #ff0000;">&quot;&lt;?php echo $i;?&gt;&quot;</span> /&gt;
                &lt;table <span style="color: #000000; font-weight: bold;">class</span>=<span style="color: #ff0000;">&quot;&lt;?php echo $style;?&gt;&quot;</span> width=<span style="color: #ff0000;">&quot;100%&quot;</span>&gt;
                  &lt;tr&gt;
                    &lt;td valign=<span style="color: #ff0000;">&quot;top&quot;</span> width=<span style="color: #ff0000;">&quot;3%&quot;</span>&gt;&lt;?php <span style="color: #000066;">echo</span> <span style="color: #0000ff;">$i</span>;?&gt;&lt;/td&gt;
                    &lt;td valign=<span style="color: #ff0000;">&quot;top&quot;</span> width=<span style="color: #ff0000;">&quot;2%&quot;</span>&gt;&lt;input type=<span style="color: #ff0000;">&quot;checkbox&quot;</span> name=<span style="color: #ff0000;">&quot;song_nums[]&quot;</span> value=<span style="color: #ff0000;">&quot;&lt;?php echo $i;?&gt;&quot;</span>&gt; &lt;/td&gt;
                    &lt;td valign=<span style="color: #ff0000;">&quot;top&quot;</span> width=<span style="color: #ff0000;">&quot;45%&quot;</span>&gt;
                      &lt;p id=<span style="color: #ff0000;">&quot;song_&lt;?php echo $i;?&gt;&quot;</span>&gt;&lt;?php <span style="color: #000066;">echo</span> <span style="color: #0000ff;">$songs</span><span style="color: #66cc66;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #66cc66;">&#93;</span>;?&gt;&lt;/p&gt;&lt;/td&gt;
                    &lt;td valign=<span style="color: #ff0000;">&quot;top&quot;</span>&gt;&lt;a href=<span style="color: #ff0000;">&quot;delete.php?dir=&lt;?php echo $_GET['dir'];?&gt;&amp;file=&lt;?php echo urlencode($songs[$i]);?&gt;&amp;anchor=&lt;?php echo $i;?&gt;&quot;</span>&gt;Delete&lt;/a&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                &lt;/table&gt;
              &lt;/td&gt;
            &lt;/tr&gt;
      <span style="color: #000000; font-weight: bold;">&lt;?php</span>
        <span style="color: #66cc66;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;/table&gt;</pre></div></div>

<p>If someone wants me to post the whole script when it&#8217;s done, I will.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jason-knight-martin.com/v2/?feed=rss2&amp;p=7</wfw:commentRss>
		</item>
		<item>
		<title>Rails: Caching and Autocomplete with Scriptaculous</title>
		<link>http://www.jason-knight-martin.com/v2/?p=6</link>
		<comments>http://www.jason-knight-martin.com/v2/?p=6#comments</comments>
		<pubDate>Sun, 30 Sep 2007 16:27:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Frameworks]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[Languages]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[Ruby On Rails]]></category>

		<guid isPermaLink="false">http://www.jason-knight-martin.com/v2/?p=6</guid>
		<description><![CDATA[I have seen some really great autocompleter tutorials around, but they all require constant database access, which is no good for me.
I have a table with customer&#8217;s info in it, there will never be more than a 100 or so, and I don&#8217;t add new ones that often.
I have a search method that lets me [...]]]></description>
			<content:encoded><![CDATA[<p>I have seen some really great autocompleter tutorials around, but they all require constant database access, which is no good for me.</p>
<p>I have a table with customer&#8217;s info in it, there will never be more than a 100 or so, and I don&#8217;t add new ones that often.</p>
<p>I have a search method that lets me pull up any customers details simply by typing all or part of his/her name, I want to add an autocompleter to this field, here is what I did:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#008000; font-style:italic;">#helpers/application_helper.rb</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> autocompleter<span style="color:#006600; font-weight:bold;">&#40;</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:div</span><span style="color:#006600; font-weight:bold;">&#93;</span> ||= args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:field</span><span style="color:#006600; font-weight:bold;">&#93;</span> + <span style="color:#996600;">'_div'</span>
    args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:class</span><span style="color:#006600; font-weight:bold;">&#93;</span> ||= <span style="color:#996600;">'auto_complete'</span>
    scriptaculous = <span style="color:#996600;">&quot;Event.observe(window, 'load', 
                    function() {new Autocompleter.Local('#{args[:field]}', 
                    '#{args[:div]}', #{args[:values]});});&quot;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#996600;">&quot;&lt;script&gt;<span style="color:#000099;">\n</span>#{scriptaculous}<span style="color:#000099;">\n</span>&lt;/script&gt;<span style="color:#000099;">\n</span>
            &lt;div id=<span style="color:#000099;">\&quot;</span>#{args[:div]}<span style="color:#000099;">\&quot;</span> class=<span style="color:#000099;">\&quot;</span>#{args[:class]}<span style="color:#000099;">\&quot;</span>&gt;&lt;/div&gt;&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#008000; font-style:italic;">#views/customers/list.rhtml</span>
  &lt;% cache <span style="color:#996600;">&quot;customer_autocomplete_list&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span> %&gt;
  &lt;script&gt;
    var customer_names = <span style="color:#006600; font-weight:bold;">&#91;</span>
    &lt;% Customer.<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:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |customer| %&gt;
      <span style="color:#996600;">'&lt;%= customer.name %&gt;'</span>,
    &lt;% <span style="color:#9966CC; font-weight:bold;">end</span> %&gt;
    <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#93;</span>;
  &lt;/script&gt;
  &lt;% <span style="color:#9966CC; font-weight:bold;">end</span> %&gt;
  <span style="color:#008000; font-style:italic;">#or, alternatively more ruby-like, you could also turn this into a helper...:</span>
  &lt;% cache <span style="color:#996600;">&quot;customer_autocomplete_list&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span> 
    values = Customer.<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:#9900CC;">map</span> <span style="color:#9966CC; font-weight:bold;">do</span> |c|
     <span style="color:#996600;">&quot;'#{c.name}'&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  %&gt;
  &lt;script&gt;
    var customer_names = <span style="color:#006600; font-weight:bold;">&#91;</span>
      &lt;%= values.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">','</span><span style="color:#006600; font-weight:bold;">&#41;</span> %&gt;
    <span style="color:#006600; font-weight:bold;">&#93;</span>;
  &lt;/script&gt;
  &lt;% <span style="color:#9966CC; font-weight:bold;">end</span> %&gt; 
  <span style="color:#008000; font-style:italic;">#Then, I create the form</span>
  &lt;% form_tag <span style="color:#ff3333; font-weight:bold;">:action</span> =&gt; <span style="color:#996600;">'search'</span> <span style="color:#9966CC; font-weight:bold;">do</span> %&gt;
    &lt;input type=<span style="color:#996600;">&quot;text&quot;</span> name=<span style="color:#996600;">&quot;search&quot;</span> value=<span style="color:#996600;">&quot;&quot;</span> id=<span style="color:#996600;">&quot;search_field&quot;</span>&gt; 
    &lt;%= autocompleter <span style="color:#ff3333; font-weight:bold;">:field</span> =&gt; <span style="color:#996600;">'search_field'</span>, 
                      <span style="color:#ff3333; font-weight:bold;">:div</span> =&gt; <span style="color:#996600;">'search_names'</span>, 
                      <span style="color:#ff3333; font-weight:bold;">:values</span> =&gt; <span style="color:#996600;">'customer_names'</span>, 
                      <span style="color:#ff3333; font-weight:bold;">:class</span> =&gt; <span style="color:#996600;">'auto_complete'</span> %&gt;
    &lt;%= submit_tag <span style="color:#996600;">'Search'</span> %&gt;
  &lt;% <span style="color:#9966CC; font-weight:bold;">end</span> %&gt;</pre></div></div>

<p>Explanation:</p>
<p>First I create a js helper method in the application_helper.rb file, it&#8217;s generic, so<br />
I can resuse it for anything I like, then in the view, I create a cached js fragment called<br />
customer_autocomplete_list, then I create the form and call the helper method.</p>
<p>In my controller, when I add a new customer, I can simply use:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">def</span> create
    <span style="color:#0066ff; font-weight:bold;">@customer</span> = Customer.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:customer</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    expire_fragment <span style="color:#996600;">&quot;customer_autocomplete_list&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And it will be regenerated the next time the page is loaded.</p>
<p>Now even with 1000 names, this should still be a viable solution, hell,<br />
for 10000 names, you are still better off loading a fragment than generating<br />
that list from live queries, or on each load.</p>
<p>Anyway, for my small project it works, it&#8217;s great, and it makes the client happy<br />
to have one of those gravy effects.</p>
<p>Just to be thorough, here is the auto_complete css:</p>

<div class="wp_syntax"><div class="code"><pre class="css">  div<span style="color: #6666ff;">.auto_complete</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">width</span>: <span style="color: #933;">350px</span>;
    <span style="color: #000000; font-weight: bold;">background</span>: <span style="color: #cc00cc;">#fff</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  div<span style="color: #6666ff;">.auto_complete</span> ul <span style="color: #66cc66;">&#123;</span>
    border<span style="color: #3333ff;">:<span style="color: #933;">1px</span></span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#<span style="color: #933;">888</span></span>;
    margin<span style="color: #3333ff;">:<span style="color: #933;">0</span></span>;
    padding<span style="color: #3333ff;">:<span style="color: #933;">0</span></span>;
    width<span style="color: #3333ff;">:<span style="color: #933;">100</span></span>%;
    list-style-type<span style="color: #3333ff;">:none</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  div<span style="color: #6666ff;">.auto_complete</span> ul li <span style="color: #66cc66;">&#123;</span>
    margin<span style="color: #3333ff;">:<span style="color: #933;">0</span></span>;
    padding<span style="color: #3333ff;">:<span style="color: #933;">3px</span></span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  div<span style="color: #6666ff;">.auto_complete</span> ul li<span style="color: #6666ff;">.selected</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span>: <span style="color: #cc00cc;">#ffb</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  div<span style="color: #6666ff;">.auto_complete</span> ul strong<span style="color: #6666ff;">.highlight</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">800</span></span>;
    margin<span style="color: #3333ff;">:<span style="color: #933;">0</span></span>;
    padding<span style="color: #3333ff;">:<span style="color: #933;">0</span></span>;
  <span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Earlier on, I said you could convert it into a helper, that is the mapping thing, well, I decided to come back and add that<br />
in so you can see how it is done:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#008000; font-style:italic;">#in application_helper.rb</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> single_field_map<span style="color:#006600; font-weight:bold;">&#40;</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:left</span><span style="color:#006600; font-weight:bold;">&#93;</span> ||= <span style="color:#996600;">&quot;'&quot;</span>
    args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:right</span><span style="color:#006600; font-weight:bold;">&#93;</span> ||= <span style="color:#996600;">&quot;'&quot;</span>
    values = args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:collection</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span> <span style="color:#9966CC; font-weight:bold;">do</span> |obj|
      <span style="color:#996600;">&quot;#{args[:left]}#{obj.send(args[:field])}#{args[:right]}&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> values
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#008000; font-style:italic;">#views/customers/list.rhtml</span>
  &lt;% cache <span style="color:#996600;">&quot;customer_autocomplete_list&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span> %&gt;
  &lt;script&gt;
    var customer_names = <span style="color:#006600; font-weight:bold;">&#91;</span>
      &lt;%= single_field_map<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:field</span> =&gt; <span style="color:#996600;">'name'</span>,:collection =&gt; Customer.<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:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">','</span><span style="color:#006600; font-weight:bold;">&#41;</span> %&gt;
    <span style="color:#006600; font-weight:bold;">&#93;</span>;
  &lt;/script&gt;
  &lt;% <span style="color:#9966CC; font-weight:bold;">end</span> %&gt;</pre></div></div>

<p>Now, that&#8217;s more concise, of course, that is in addition to the first helper and view code&#8230;just an addition<br />
and a replacement, more ruby like. </p>
<p>It&#8217;s hard for me to not think like a PHP programmer, I am getting there, but remember,<br />
working code is better than working theory. In Arnis we have a saying, &#8220;If it works, it&#8217;s Arnis.&#8221;<br />
In a fight, if you win, you&#8217;re right, in coding, if your software works like you planned it, then<br />
it&#8217;s right.</p>
<p>I have tested all of this code to make sure it works, however, I don&#8217;t guarantee anything.</p>
<p>The golden rule of code copying is, if you don&#8217;t understand it, don&#8217;t copy it. Figure it out first.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jason-knight-martin.com/v2/?feed=rss2&amp;p=6</wfw:commentRss>
		</item>
		<item>
		<title>Current Project 29/09/2007 Rails and eBay Trading API</title>
		<link>http://www.jason-knight-martin.com/v2/?p=5</link>
		<comments>http://www.jason-knight-martin.com/v2/?p=5#comments</comments>
		<pubDate>Sun, 30 Sep 2007 02:09:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Misc]]></category>

		<category><![CDATA[Portfolio]]></category>

		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://www.jason-knight-martin.com/v2/?p=5</guid>
		<description><![CDATA[




A nice screenshot from my current project, as always, I am cursed with creating intranet like apps, which aren&#8217;t publicly accessible. This one is from a project integrating a Rails database management system with eBay using their trading API. The whole thing is built from scratch in Rails.
So far it features automatic thumbnail generation and [...]]]></description>
			<content:encoded><![CDATA[<table align="left">
<tr>
<td><a href="http://www.jason-knight-martin.com/v2/?attachment_id=4" rel="attachment wp-att-4" title="Show Stamp"><img src="http://www.jason-knight-martin.com/v2/wp-content/uploads/2007/09/mark_bloxham_show.thumbnail.png" alt="Show Stamp" border="0" /></a></td>
</tr>
</table>
<p>A nice screenshot from my current project, as always, I am cursed with creating intranet like apps, which aren&#8217;t publicly accessible. This one is from a project integrating a Rails database management system with eBay using their trading API. The whole thing is built from scratch in Rails.</p>
<p>So far it features automatic thumbnail generation and image sorting into directories, c39 and EAN barcode generation, and a connection to eBay that allows stock to be automatically listed.</p>
<p>When it&#8217;s finished, I might just create a publicly accessible version just for future client to peruse.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jason-knight-martin.com/v2/?feed=rss2&amp;p=5</wfw:commentRss>
		</item>
		<item>
		<title>Rails: Helper Tutorial Now with 20% more Scriptaculous.</title>
		<link>http://www.jason-knight-martin.com/v2/?p=3</link>
		<comments>http://www.jason-knight-martin.com/v2/?p=3#comments</comments>
		<pubDate>Sat, 29 Sep 2007 23:37:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Frameworks]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[Languages]]></category>

		<category><![CDATA[Misc]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[Ruby On Rails]]></category>

		<guid isPermaLink="false">http://www.jason-knight-martin.com/v2/?p=3</guid>
		<description><![CDATA[I am no rails expert, in fact, I had a bit of a problem with helpers, had to look into it and eventually, ask some questions and get answer on the Rails group before I could fix the issue, I thought I would give back to the community on what I learned.
Helpers Rule.
That&#8217;s right, helpers [...]]]></description>
			<content:encoded><![CDATA[<p>I am no rails expert, in fact, I had a bit of a problem with helpers, had to look into it and eventually, ask some questions and get answer on the Rails group before I could fix the issue, I thought I would give back to the community on what I learned.</p>
<p>Helpers Rule.</p>
<p>That&#8217;s right, helpers are you friend, there isn&#8217;t much to say else, except they are dead useful ideas. Helpers save you loads of time and keep your code DRY, as opposed to wet <img src='http://www.jason-knight-martin.com/v2/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> As you may not know, DRY stands for Don&#8217;t Repeat Yourself, and along with Convention over Configuration, it&#8217;s the core mantra of every rails programmer. How do they work? Simple enough, they are basically functions held in a centralized location, either in a controllers helper, or in the main Application helper, based on where you will need them.</p>
<p>If you only need them to be accessible from on controller and it&#8217;s helpers, then you put them in the ControllerName_helper.rb file that you either create, or was created for you with scaffold.</p>
<p>If you need something that is accessible from any view/controller, then you will be putting it in helpers/application_helper.rb</p>
<p>Let&#8217;s take an example. Let&#8217;s say you have a central image location for thumbnails and one for large images on another server, instead of hard coding that path into every view, you can create a helper that does it for you.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> my_image_path<span style="color:#006600; font-weight:bold;">&#40;</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>  
  args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:version</span><span style="color:#006600; font-weight:bold;">&#93;</span> ||= <span style="color:#996600;">''</span>
  return_value = <span style="color:#996600;">''</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:version</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">'thumb'</span> <span style="color:#9966CC; font-weight:bold;">then</span>
    return_value = <span style="color:#996600;">'http://images.myhost.com/thumbnails/'</span>
  <span style="color:#9966CC; font-weight:bold;">elsif</span> args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:version</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">'large'</span> <span style="color:#9966CC; font-weight:bold;">then</span>
    return_value = <span style="color:#996600;">'http://images.myhost.com/large_images/'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> return_value
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Then you just pass it in a view like this:</p>
<p><%= my_image_path :version => &#8216;thumb&#8217; %></p>
<p>Pretty cool huh?</p>
<p>You&#8217;ll notice something, in this example, I could have just done it this way:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">def</span> my_image_path<span style="color:#006600; font-weight:bold;">&#40;</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>  
  args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:version</span><span style="color:#006600; font-weight:bold;">&#93;</span> ||= <span style="color:#996600;">''</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:version</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">'thumb'</span> <span style="color:#9966CC; font-weight:bold;">then</span>
    <span style="color:#996600;">'http://images.myhost.com/thumbnails/'</span>
  <span style="color:#9966CC; font-weight:bold;">elsif</span> args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:version</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">'large'</span> <span style="color:#9966CC; font-weight:bold;">then</span>
    <span style="color:#996600;">'http://images.myhost.com/large_images/'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>You can do that, however, as I learned the hard way, it can have unexpected results. I will tell you what wasn&#8217;t told to me,<br />
just because you can do it, doesn&#8217;t mean you should, you should always explicitly return something!</p>
<p>Of course the above example isn&#8217;t so powerful, but, with this one, you can see the greatness of the helper.</p>
<p>I have a website that has products separated into categories of an infinite depth, that means, it can have as many subcategories<br />
as it needs or wants. You can add products to multiple categories, so I need to generate a list box. Because of the<br />
various different extra goodies, I need to be able to display this list box in several different views, for instance<br />
the new and edit pages, but also a move, and list page so that multiple items can be moved into a selected category at once.</p>
<p>Now, I could write that code in every single view, but when I need to make a change, I have to edit like 5 different files, that&#8217;s<br />
no good, and all of a sudden, my code is no longer DRY, enter Helpers to the rescue.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">def</span> category_list_options<span style="color:#006600; font-weight:bold;">&#40;</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:category_id</span><span style="color:#006600; font-weight:bold;">&#93;</span>  ||= <span style="color:#006666;">0</span>
    args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:spacer</span><span style="color:#006600; font-weight:bold;">&#93;</span> ||= <span style="color:#996600;">'&amp;nbsp;'</span>
    <span style="color:#0066ff; font-weight:bold;">@categories</span> = Category.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>,:conditions =&gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'category_id = ?'</span>,args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:category_id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    ret = <span style="color:#996600;">''</span>
    <span style="color:#9966CC; font-weight:bold;">for</span> category <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#0066ff; font-weight:bold;">@categories</span> <span style="color:#9966CC; font-weight:bold;">do</span>
     ret = ret + <span style="color:#996600;">'&lt;option value=&quot;'</span> + category.<span style="color:#9900CC;">id</span>.<span style="color:#9900CC;">to_s</span> + <span style="color:#996600;">'&quot;&gt;'</span> + args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:spacer</span><span style="color:#006600; font-weight:bold;">&#93;</span> + <span style="color:#996600;">' '</span> + category.<span style="color:#9900CC;">title</span> + <span style="color:#996600;">'&lt;/option&gt;'</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> category.<span style="color:#9900CC;">categories</span>.<span style="color:#9900CC;">length</span> &gt; <span style="color:#006666;">0</span> <span style="color:#9966CC; font-weight:bold;">then</span>
       ret = ret + category_list_options<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:category_id</span> =&gt; category.<span style="color:#9900CC;">id</span>,:spacer =&gt; args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:spacer</span><span style="color:#006600; font-weight:bold;">&#93;</span> + args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:spacer</span><span style="color:#006600; font-weight:bold;">&#93;</span><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>
    <span style="color:#0000FF; font-weight:bold;">return</span> ret
   <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Then I just call it in a view with:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">  &lt;%= category_list_options <span style="color:#ff3333; font-weight:bold;">:spacer</span> =&gt; <span style="color:#996600;">'&amp;nbsp;'</span> %&gt;</pre></div></div>

<p>Now of course, I can expand that, for instance, I can rewrite it to show them as table rows, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">def</span> category_list_table<span style="color:#006600; font-weight:bold;">&#40;</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:category_id</span><span style="color:#006600; font-weight:bold;">&#93;</span>  ||= <span style="color:#006666;">0</span>
    args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:spacer</span><span style="color:#006600; font-weight:bold;">&#93;</span> ||= <span style="color:#996600;">'&amp;nbsp;'</span>
    <span style="color:#0066ff; font-weight:bold;">@categories</span> = Category.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>,:conditions =&gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'category_id = ?'</span>,args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:category_id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    ret = <span style="color:#996600;">''</span>
    <span style="color:#9966CC; font-weight:bold;">for</span> category <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#0066ff; font-weight:bold;">@categories</span> <span style="color:#9966CC; font-weight:bold;">do</span>
     ret = ret + <span style="color:#996600;">&quot;&lt;tr&gt;&lt;td&gt;#{args[:spacer]} #{category.title}&lt;/td&gt;&lt;/tr&gt;<span style="color:#000099;">\n</span>&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> category.<span style="color:#9900CC;">categories</span>.<span style="color:#9900CC;">length</span> &gt; <span style="color:#006666;">0</span> <span style="color:#9966CC; font-weight:bold;">then</span>
       ret = ret + category_list_table<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:category_id</span> =&gt; category.<span style="color:#9900CC;">id</span>,:spacer =&gt; args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:spacer</span><span style="color:#006600; font-weight:bold;">&#93;</span> + args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:spacer</span><span style="color:#006600; font-weight:bold;">&#93;</span><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>
    <span style="color:#0000FF; font-weight:bold;">return</span> ret
   <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I can call the whole tree like so:</p>
<pre>
  <%= category_list_table :spacer => &#8216;&nbsp;&#8217; %>
</pre>
<p>Or, when customers are browsing a certain category, I can grab all the ones down that chain like so:</p>
<pre>
  <%= category_list_table :category_id => current_category.id,:spacer => &#8216;&nbsp;&#8217; %>
</pre>
<p>This all assumes that you have a model that belongs to itself, with a foreign key in the table.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">class</span> Category &lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
    has_many <span style="color:#ff3333; font-weight:bold;">:categories</span>
    belongs_to <span style="color:#ff3333; font-weight:bold;">:category</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>From then on, whenever we create a category, we set main categories as having a :category_id of 0, that way<br />
we know this category belongs to no category, everything else is a subcategory <img src='http://www.jason-knight-martin.com/v2/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>So you see, helpers really really rule. And this isn&#8217;t the half of it, I am sure I will be back to add more<br />
code samples and ideas when I find new uses for helpers&#8230;</p>
<p>Ohh, I forgot&#8230;</p>
<p>One other cool factor, is javascript helpers, writing js is a total bitch. On a Pizza Shop&#8217;s website, which is basicallly<br />
an online menu, I needed to have a js popup window to show the product and ingredients. I just added a little helper in<br />
application_helper.rb like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">def</span> javascript_popup_window<span style="color:#006600; font-weight:bold;">&#40;</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:height</span><span style="color:#006600; font-weight:bold;">&#93;</span> ||= <span style="color:#996600;">'600'</span>
    args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:width</span><span style="color:#006600; font-weight:bold;">&#93;</span> ||= <span style="color:#996600;">'300'</span>
    args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:name</span><span style="color:#006600; font-weight:bold;">&#93;</span> ||= <span style="color:#996600;">'javascript_window'</span>
    args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:toolbar</span><span style="color:#006600; font-weight:bold;">&#93;</span> ||= <span style="color:#996600;">'0'</span>
    args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:statusbar</span><span style="color:#006600; font-weight:bold;">&#93;</span> ||= <span style="color:#996600;">'0'</span>
    args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:location</span><span style="color:#006600; font-weight:bold;">&#93;</span> ||= <span style="color:#996600;">''</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#996600;">&quot;window.open('#{args[:location]}','#{args[:name]}','status=#{args[:statusbar]},toolbar=#{args[:toolbar]},height=#{args[:height]},width=#{args[:width]}')&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I know, the world&#8217;s longest line, but it&#8217;s straight forward. All I need to do is call it with:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">  &lt;a onclick=<span style="color:#996600;">&quot;&lt;%= javascript_popup_window :location =&gt; '/controller/action/id' %&gt;&quot;</span>&gt;Open&lt;/a&gt;</pre></div></div>

<p>Baddaboom, works like a charm.</p>
<p>You can also do something like this for a scriptaculous autocompleter:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color:#9966CC; font-weight:bold;">def</span> scriptaculous_autocomplete<span style="color:#006600; font-weight:bold;">&#40;</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    scriptaculous = <span style="color:#996600;">&quot;Event.observe(window, 'load' , function() {new Autocompleter.Local('#{args[:field]}' , '#{args[:div]}' , #{args[:name]});});&quot;</span>
    <span style="color:#CC0066; font-weight:bold;">array</span> = <span style="color:#996600;">''</span>
    args<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:values</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |v|
      <span style="color:#CC0066; font-weight:bold;">array</span> = <span style="color:#CC0066; font-weight:bold;">array</span> + <span style="color:#996600;">&quot;'#{v}',&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#996600;">&quot;&lt;script&gt;#{scriptaculous}<span style="color:#000099;">\n</span> var #{args[:name]} = [#{array}''];&lt;/script&gt;<span style="color:#000099;">\n</span>&lt;div id=<span style="color:#000099;">\&quot;</span>#{args[:div]}<span style="color:#000099;">\&quot;</span>&gt;&lt;/div&gt;&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Then you call it like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">  &lt;%= scriptaculous_autocomplete <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:field</span> =&gt; <span style="color:#996600;">'item_title'</span>,
                                 <span style="color:#ff3333; font-weight:bold;">:div</span> =&gt; <span style="color:#996600;">'item_title_choices'</span>, 
                                 <span style="color:#ff3333; font-weight:bold;">:name</span> =&gt; <span style="color:#996600;">'item_titles'</span>, 
                                 <span style="color:#ff3333; font-weight:bold;">:values</span> =&gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'and'</span>,<span style="color:#996600;">'one'</span>,<span style="color:#996600;">'two'</span>,<span style="color:#996600;">'three'</span>,<span style="color:#996600;">'four'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> %&gt;</pre></div></div>

<p>Of course, that&#8217;s just off the top of my head. To learn more about scriptaculous, and Prototype, why no head over<br />
to   <a href="http://pragmaticprogrammer.com/"> pragmatic programmer </a> and pick up their book on the subject.<br />
As for Helpers, check out prag&#8217;s agile webdevelopment book, it&#8217;s a scorcher.</p>
<p>Anyway, that&#8217;s all for now, let me know if there are any errors, most of this code is used by me in actual projects.<br />
Also, if you have a better, cleaner, more robust way, send it to me and I&#8217;ll plop it up here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jason-knight-martin.com/v2/?feed=rss2&amp;p=3</wfw:commentRss>
		</item>
	</channel>
</rss>
