<?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>Floorplanner Tech Blog &#187; scoped_search</title>
	<atom:link href="http://techblog.floorplanner.com/tag/scoped_search/feed/" rel="self" type="application/rss+xml" />
	<link>http://techblog.floorplanner.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 19 Nov 2009 04:00:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>New version of Scoped search</title>
		<link>http://techblog.floorplanner.com/2009/08/31/new-version-of-scoped-search/</link>
		<comments>http://techblog.floorplanner.com/2009/08/31/new-version-of-scoped-search/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 09:37:22 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[LIKE]]></category>
		<category><![CDATA[query language]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[scoped_search]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=698</guid>
		<description><![CDATA[After an almost complete rewrite, I am proud to present version 2.0 of scoped_search, the ActiveRecord plugin that makes it easy to find records using a simple query language. This new version support a new query language that supports more complex constructs, and can therefore be used to conduct more fine-grained queries on your models.
New [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2009%2F08%2F31%2Fnew-version-of-scoped-search%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2009%2F08%2F31%2Fnew-version-of-scoped-search%2F" height="61" width="51" /></a></div><p>After an almost complete rewrite, I am proud to present version 2.0 of <a href="http://github.com/wvanbergen/scoped_search">scoped_search</a>, the ActiveRecord plugin that <a href="http://techblog.floorplanner.com/2008/07/26/easy-search-with-activerecord/">makes it easy to find records using a simple query language</a>. This new version support a new query language that supports more complex constructs, and can therefore be used to conduct more fine-grained queries on your models.</p>
<h3>New query language</h3>
<ul>
<li>Logical operators: AND (&amp;, &amp;&amp;), OR (|, ||) and NOT (!, -) operators, and parentheses to structure the boolean logic: <code>police AND (car || uniform), -"village people"</code>. By default, the AND operator is used to combine different segments of your query.</li>
<li>Comparison operators: the most common comparison operators are supported, and to what you expect on integer and date field.</li>
<li>Explicit field support: only search in the specified field instead of all fields: <code>age &gt;= 21</code>, <code>created &lt; 2009-01-01</code>, <code>username != "root"</code>.</li>
<li>Check for NULL fields: <code>null? parent</code>, <code>set? error_message</code></li>
<li>Commas are supported to separate the different parts of the query.</li>
</ul>
<p>More information about <a href="http://wiki.github.com/wvanbergen/scoped_search/query-language">the query language</a> can be found in the project wiki on GitHub.</p>
<h3>New definition syntax</h3>
<p>The new version supports a new syntax to define what fields of your model can be searched and in what cases. An example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>   
  belongs_to <span style="color:#ff3333; font-weight:bold;">:account_type</span>
&nbsp;
  scoped_search <span style="color:#ff3333; font-weight:bold;">:on</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;">:first_name</span>, <span style="color:#ff3333; font-weight:bold;">:last_name</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  scoped_search <span style="color:#ff3333; font-weight:bold;">:on</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:created_at</span>, <span style="color:#ff3333; font-weight:bold;">:alias</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:created</span>, <span style="color:#ff3333; font-weight:bold;">:only_explicit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
  scoped_search <span style="color:#ff3333; font-weight:bold;">:in</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:account_type</span>, <span style="color:#ff3333; font-weight:bold;">:on</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;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:description</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>After the fields have been defined, the <code>search_for</code> method can be used to search your models using a named scope, just like it was before. The <a href="http://wiki.github.com/wvanbergen/scoped_search/search-definition">project wiki</a> has more information about this new syntax. The search syntax itself hasn&#8217;t changed:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#0066ff; font-weight:bold;">@users</span> = User.<span style="color:#9900CC;">search_for</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;">:q</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">paginate</span><span style="color:#006600; font-weight:bold;">&#40;</span>page <span style="color:#006600; font-weight:bold;">=&gt;</span> params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:page</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<h3>Installation or upgrade</h3>
<p>Include the gem in your <code>environment.rb</code> configuration and run <code>rake gems:install</code> to install it:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">'scoped_search'</span>, <span style="color:#ff3333; font-weight:bold;">:source</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'http://gemcutter.org'</span></pre></div></div>

<h3>Backwards compatibility</h3>
<p>The new version has a new syntax to define the fields that can be searched with a query. This new syntax gives you more fine-grained control over the queries that will be generated, so I urge you to adopt this new syntax. However, the old <code>searchable_on</code> syntax is still available for backwards compatibility.</p>
<p>Please contact me if you have any issues with the new version.</p>
<p><small>(Updated with new gemcutter installation instructions.)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2009/08/31/new-version-of-scoped-search/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Easy search with ActiveRecord</title>
		<link>http://techblog.floorplanner.com/2008/07/26/easy-search-with-activerecord/</link>
		<comments>http://techblog.floorplanner.com/2008/07/26/easy-search-with-activerecord/#comments</comments>
		<pubDate>Sat, 26 Jul 2008 14:47:35 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[named_scope]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[scoped_search]]></category>
		<category><![CDATA[searching]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=103</guid>
		<description><![CDATA[A couple of minutes ago I released scoped_search, a Rails/ActiveRecord plugin that makes it easy to search your models. It is very easy to use:

Install the plugin in your vendor/plugins directory from http://github.com/wvanbergen/scoped_search
Add the gem to your rails environment.rb:

config.gem 'wvanbergen-scoped_search', :lib =&#62; 'scoped_search', 
    :source =&#62; 'http://gems.github.com'

Call rake gems:install afterwards to ensure the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2008%2F07%2F26%2Feasy-search-with-activerecord%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2008%2F07%2F26%2Feasy-search-with-activerecord%2F" height="61" width="51" /></a></div><p>A couple of minutes ago I released <a href="http://github.com/wvanbergen/scoped_search">scoped_search</a>, a Rails/ActiveRecord plugin that makes it easy to search your models. It is very easy to use:</p>
<ol>
<li><del datetime="2008-09-20T04:26:19+00:00">Install the plugin in your vendor/plugins directory from <a href="http://github.com/wvanbergen/scoped_search">http://github.com/wvanbergen/scoped_search</a></del><br />
Add the gem to your rails <code>environment.rb</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">'wvanbergen-scoped_search'</span>, <span style="color:#ff3333; font-weight:bold;">:lib</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'scoped_search'</span>, 
    <span style="color:#ff3333; font-weight:bold;">:source</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'http://gems.github.com'</span></pre></div></div>

<p>Call <code>rake gems:install</code> afterwards to ensure the gem is installed.
</li>
<li>Define in what fields your model should be searched by calling<br /> <code>searchable_on :some, :field, :names</code></li>
<li>Find your records by calling <code>search_for("query keywords")</code></li>
</ol>
<p>That&#8217;s all! A short example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Project <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  searchable_on <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:description</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Project.<span style="color:#9900CC;">search_for</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;search keywords&quot;</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> <span style="color:#006600; font-weight:bold;">|</span>project<span style="color:#006600; font-weight:bold;">|</span> 
  <span style="color:#CC0066; font-weight:bold;">puts</span> project.<span style="color:#9900CC;">name</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># SELECT * FROM projects WHERE </span>
<span style="color:#008000; font-style:italic;">#      (name LIKE '%search%' OR description LIKE '%search%') </span>
<span style="color:#008000; font-style:italic;">#  AND (name LIKE '%keywords%' OR description LIKE '%keywords%')</span></pre></div></div>

<p>This functionality is completely build upon <a href="http://railscasts.com/episodes/108">named_scope</a>. The <code>search_for</code> method is actually a named scope that was created by the call to <code>searchable_on</code>. Because these scopes can be chained, this offers some great possibilities. </p>
<p>For example, in Floorplanner, we only want you to search on the projects you have access to. We have implemented this access logic in another named scope. The calls can simply be chained:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Project <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  searchable_on <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:description</span>
&nbsp;
  named_scope <span style="color:#ff3333; font-weight:bold;">:accessible_by</span>, <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>user<span style="color:#006600; font-weight:bold;">|</span> ... <span style="color:#006600; font-weight:bold;">&#125;</span>
  named_scope <span style="color:#ff3333; font-weight:bold;">:published</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'published_at IS NOT NULL'</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#0066ff; font-weight:bold;">@projects</span> = Project.<span style="color:#9900CC;">accessible_by</span><span style="color:#006600; font-weight:bold;">&#40;</span>current_user<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">published</span>.<span style="color:#9900CC;">search_for</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'query'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#0066ff; font-weight:bold;">@projects</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>project<span style="color:#006600; font-weight:bold;">|</span> ... <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>This plugin is released under the MIT license, so please use it for any purpose you see fit. There are some <a href="http://github.com/wvanbergen/scoped_search/tree/master/TODO">TODOs</a>: you currently can not search on fields in other tables, and splitting the search string into keywords is very basic. Please contact me if you have implemented any of these features and you are willing to share them! Do not hesitate to contact me in case or problems either.</p>
<p><strong>Update:</strong> I added support for quotes and the minus sign to the query language:<br />
<code>Project.search_for('willem -"van bergen"').count</code></p>
<p><strong>Update #2:</strong> Wes Hays implemented the <code>OR</code> keyword:<br />
<code>Project.search_for('wes OR hays').count</code>. <br />A big thanks to Wes for helping out on this project!!</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2008/07/26/easy-search-with-activerecord/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
