<?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>Wylie &#187; ruby</title>
	<atom:link href="http://www.wyliethomas.com/blog/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wyliethomas.com/blog</link>
	<description>web consultant.developer.angler</description>
	<lastBuildDate>Wed, 07 Dec 2011 18:14:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Making a sortable, dynamic grid</title>
		<link>http://www.wyliethomas.com/blog/2009/10/11/making-a-sortable-dynamic-grid/</link>
		<comments>http://www.wyliethomas.com/blog/2009/10/11/making-a-sortable-dynamic-grid/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 05:56:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.wyliethomas.com/blog/?p=80</guid>
		<description><![CDATA[I came up against an interesting challenge on a current project. This was actually pretty fun to figure out. Here is what I came up with.
I needed to display a grid with columns and rows that had information in each cell. The information in the cells needed to be dynamic, or change based on a [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-medium wp-image-81" title="Grid" src="http://www.wyliethomas.com/blog/wp-content/uploads/2009/10/Picture-2-300x239.png" alt="Grid" width="300" height="239" />I came up against an interesting challenge on a current project. This was actually pretty fun to figure out. Here is what I came up with.</p>
<p>I needed to display a grid with columns and rows that had information in each cell. The information in the cells needed to be dynamic, or change based on a UI perspective.</p>
<p>Also, if there were more than 10 rows in a column, the remaining cells needed to wrap to the next column.</p>
<p>This is definitely a job for a helper so I wrote these methods for the helper to render this sortable, dynamic grid. So far it seems to be holding up when rendering grids of various columns and rows.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">def</span> my_grid</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re1">@my_data</span> = <span class="st0">&#8221;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re1">@my_data</span> += <span class="st0">&#8216;&lt;table&gt;&#8217;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="re1">@my_data</span> += <span class="st0">&#8216;&lt;tr&gt;&#8217;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; num = <span class="nu0">1</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">#this starts a new column for each category</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re1">@columns</span>.<span class="me1">each</span> <span class="kw1">do</span> |col|</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; cell = Cell.<span class="me1">find</span><span class="br0">&#40;</span><span class="re3">:all</span>, <span class="re3">:conditions</span> =&gt; <span class="br0">&#91;</span><span class="st0">&#8216;category_id = ?&#8217;</span>, col<span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="kw1">rescue</span> <span class="kw2">nil</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; count = cell.<span class="me1">size</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; item = <span class="nu0">0</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; itemtotal = <span class="nu0">1</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; cell.<span class="me1">each</span> <span class="kw1">do</span> |row|</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">#if this is the 0 item start a new column</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> item == <span class="nu0">0</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">@my_data</span> += <span class="kw2">self</span>.<span class="me1">start_col</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">end</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; score = row.<span class="me1">get_my_score</span><span class="br0">&#40;</span>row.<span class="me1">id</span>, <span class="re1">@grid</span>, <span class="re1">@user</span><span class="br0">&#41;</span>.<span class="me1">to_s</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">@my_data</span> += <span class="kw2">self</span>.<span class="me1">list_item</span><span class="br0">&#40;</span>score, row, itemtotal<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">#if this is the 9th item, or is the last of the cells, close the column and reset count</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; item += <span class="nu0">1</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> item == <span class="nu0">10</span> || itemtotal == count</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re1">@my_data</span> += <span class="kw2">self</span>.<span class="me1">end_col</span><span class="br0">&#40;</span>num<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num = num.<span class="me1">to_i</span> + <span class="nu0">1</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item = <span class="nu0">0</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; itemtotal += <span class="nu0">1</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re1">@my_data</span> += <span class="st0">&#8216;&lt;/tr&gt;&#8217;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re1">@my_data</span> += <span class="st0">&#8216;&lt;/table&gt;&#8217;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">#return @my_data</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> start_col</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re1">@start</span> = <span class="st0">&#8216;&lt;td valign=&quot;bottom&quot;&gt;&lt;div style=&quot;position: relative; bottom: 0px; display: block;&quot;&gt;&#8217;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re1">@start</span> += <span class="st0">&#8216;&lt;ul style=&quot;vertical-align: bottom&quot;&gt;&#8217;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw2">return</span> <span class="re1">@start</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> end_col<span class="br0">&#40;</span>num<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re1">@end</span> = <span class="st0">&#8216;&lt;/ul&gt;&#8217;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re1">@end</span> += num.<span class="me1">to_s</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="re1">@end</span> += <span class="st0">&#8216;&lt;/td&gt;&#8217;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">return</span> <span class="re1">@end</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> list_item<span class="br0">&#40;</span>score, row, item<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="re1">@list</span> = <span class="st0">&#8216;&lt;li class=key_&#8217;</span>+ score +<span class="st0">&#8216;&gt;&#8217;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">#some stuff here&#8230;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="re1">@list</span> += <span class="st0">&#8216;&lt;/li&gt;&#8217;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">return</span> <span class="re1">@list</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">end</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.wyliethomas.com/blog/2009/10/11/making-a-sortable-dynamic-grid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building site search with Ferret</title>
		<link>http://www.wyliethomas.com/blog/2008/07/25/building-site-search-with-ferret/</link>
		<comments>http://www.wyliethomas.com/blog/2008/07/25/building-site-search-with-ferret/#comments</comments>
		<pubDate>Fri, 25 Jul 2008 20:07:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[acts_as_ferret]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://www.wyliethomas.com/2008/07/25/building-site-search-with-ferret/</guid>
		<description><![CDATA[I recently had the opportunity to build a search feature using the Ferret project.
So I wanted to document what I learned while it was still fresh in mind. First, getting Ferret and acts_as_ferret installed was pretty simple and straight forward. This blog post got me off to a good start.
The first index it created was [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had the opportunity to build a search feature using the Ferret project.</p>
<p>So I wanted to document what I learned while it was still fresh in mind. First, getting Ferret and acts_as_ferret installed was pretty simple and straight forward. <a href="http://www.railsenvy.com/2007/2/19/acts-as-ferret-tutorial" target="_blank">This blog post</a> got me off to a good start.</p>
<p>The first index it created was ok but I was looking for more relevant results. The boost option made the difference I was looking for. Here is how I set it up in my model.</p>
<p>#use acts_as_ferret plug in to assist in creating the index<br />
acts_as_ferret :fields =&gt; { :main_tags        =&gt; { :boost =&gt; 4, :store =&gt; :yes },<br />
:description      =&gt; { :boost =&gt; 2 },<br />
:keywords         =&gt; { :boost =&gt; 1, :store =&gt; :yes }, #have to add store =&gt; yes for lazy to work<br />
:photo_code       =&gt; { :boost =&gt; 1 },<br />
}</p>
<p>The production server has over 230k records in it. It took over 6 hours to index everything&#8230; (im still working out how to speed that up)&#8230; so for development I started with just a few hundred records. That only took a couple of minutes to index.</p>
<p>Once I had my index done I was ready to search it.</p>
<p>search_string = params[:search_term]</p>
<p>@results = Photo.find_by_contents(search_string, :limit =&gt; 50) #defaults to 10 results</p>
<p>(pagination notes are coming in a later post)</p>
<p>One of the unique requirements of this project was the need to refine the search on a specific set of keywords. For example, we need to know how many of the search results are also tagged with age, gender or ethnic keywords. So, to know how many of the search results are also tagged with &#8220;Mid Adult Women (2,544) &#8221; I used the lazy method in ferret to spare a hit to the database for it. Going to the database for these refine search options put page loads over 10 seconds, 20+ seconds in some cases. Since that is completely unacceptable, Im glad the Ferret team came up with :lazy. It may have saved my job <img src='http://www.wyliethomas.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Photo.find_by_contents(search_string + &#8221; AND Mid Adult Women&#8221;, {:lazy =&gt; [:keywords]}).total_hits</p>
<p>I learned that find_by_contents will ignore the :condition option for extra query info. But, you can do multiple AND OR statements as part of the search string. Now I can refine my search many times over without ever touching the database!</p>
<p>In the end I am searching over 230k records getting as much as 20k results on some searches and showing total_hits on over 60 refine search options in less than .4 seconds with only one hit to the database. Pretty cool.</p>
<p>Next is pagination</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wyliethomas.com/blog/2008/07/25/building-site-search-with-ferret/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

