<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>The Dev Blog: Adding More Control to the Displayed Data in AjaxScaffold</title>
    <link>http://devblog.famundo.com/articles/2007/02/15/adding-more-control-to-the-displayed-data-in-ajaxscaffold</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Putting Family Management on Rails!</description>
    <item>
      <title>Adding More Control to the Displayed Data in AjaxScaffold</title>
      <description>&lt;p&gt;Wow, my 4th post of &lt;a href="http://www.ajaxscaffold.com"&gt;AjaxScaffold&lt;/a&gt;! This time a small change in it to let you better control the query used to retireve the data into the grid.&lt;/p&gt;

&lt;p&gt;The problem I'm trying to solve, is including a join to the query used when laoding the grid. It's especially useful when doing filtering and searching. My example is the User model that has also a UserSetting association. I want the query to include a join to the user_settings table, as I want to be able to search on the fields from the joined table.&lt;/p&gt;

&lt;p&gt;The easiest way to search/filter on AjaxScaffold is to define in your controller the method:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;conditions_for_#{plural_name}_collection&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The value returned from this method is assigned internally by AjaxScaffold to the &lt;strong&gt;:conditions&lt;/strong&gt; option of find. But if you now try to write a condition like:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="punct"&gt;['&lt;/span&gt;&lt;span class="string"&gt;lower(name) LIKE ? OR lower(city) LIKE ?&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="ident"&gt;name&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;city&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt; 

&lt;p&gt;With city coming from the user_settings table, it will fail, as this field isn't part of the query. So we need to also change the query to include additional query parameters. All we need to do, is add another callback like the one above, that will let us adjust the options used for the find:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;adjust_options&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt; &lt;span class="ident"&gt;options&lt;/span&gt; &lt;span class="punct"&gt;)&lt;/span&gt;
  &lt;span class="ident"&gt;options&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;merge!&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt; &lt;span class="symbol"&gt;:joins&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;inner join user_settings on user_settings.user_id = users.id&lt;/span&gt;&lt;span class="punct"&gt;'}&lt;/span&gt; &lt;span class="punct"&gt;)&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To implement the change, we need to add a call to &lt;strong&gt;adjust&lt;em&gt;options&lt;/strong&gt; before using the options in the code. Look in &lt;strong&gt;vendor/plugins/ajaxscaffoldp/lib/ajax&lt;/em&gt;scaffold&lt;em&gt;plugin.rb&lt;/strong&gt; for the function &lt;strong&gt;def #{prefix}table&lt;/em&gt;setup&lt;/strong&gt;. Around line 200, after the code:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;options&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt; &lt;span class="symbol"&gt;:order&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="ident"&gt;order&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt;
                &lt;span class="symbol"&gt;:conditions&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="ident"&gt;conditions_for_&lt;/span&gt;&lt;span class="comment"&gt;#{plural_name}_collection,&lt;/span&gt;
                &lt;span class="symbol"&gt;:direction&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="ident"&gt;current_sort_direction&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;params&lt;/span&gt;&lt;span class="punct"&gt;),&lt;/span&gt;
                &lt;span class="symbol"&gt;:per_page&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="comment"&gt;#{rows_per_page} }&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Add the following:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;adjust_options&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;options&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="constant"&gt;self&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;respond_to?&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;adjust_options&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is a hack, as I didn't want to do a big patch when a new and highly modified version AjaxScaffold is on it's way. Should be fine as an interim solution.&lt;/p&gt;</description>
      <pubDate>Thu, 15 Feb 2007 05:00:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:be00d84e-da9d-467f-a08c-a0209b2de5c0</guid>
      <author>guy.naor@famundo.com (Guy Naor)</author>
      <link>http://devblog.famundo.com/articles/2007/02/15/adding-more-control-to-the-displayed-data-in-ajaxscaffold</link>
      <category>Rails</category>
    </item>
  </channel>
</rss>
