The Dev Blog

Putting Family Management on Rails!

Adding More Control to the Displayed Data in AjaxScaffold

Posted by Guy Naor Thu, 15 Feb 2007 10:00:00 GMT

Wow, my 4th post of AjaxScaffold! This time a small change in it to let you better control the query used to retireve the data into the grid.

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.

The easiest way to search/filter on AjaxScaffold is to define in your controller the method:

def conditions_for_#{plural_name}_collection
end

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

['lower(name) LIKE ? OR lower(city) LIKE ?', name, city]

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:

def adjust_options( options )
  options.merge!( { :joins => 'inner join user_settings on user_settings.user_id = users.id'} )
end

To implement the change, we need to add a call to adjustoptions before using the options in the code. Look in vendor/plugins/ajaxscaffoldp/lib/ajaxscaffoldplugin.rb for the function def #{prefix}tablesetup. Around line 200, after the code:

options = { :order => order,
                :conditions => conditions_for_#{plural_name}_collection,
                :direction => current_sort_direction(params),
                :per_page => #{rows_per_page} }

Add the following:

adjust_options(options) if self.respond_to? "adjust_options"

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.

Posted in  | no comments

del.icio.us:Adding More Control to the Displayed Data in AjaxScaffold digg:Adding More Control to the Displayed Data in AjaxScaffold spurl:Adding More Control to the Displayed Data in AjaxScaffold wists:Adding More Control to the Displayed Data in AjaxScaffold simpy:Adding More Control to the Displayed Data in AjaxScaffold newsvine:Adding More Control to the Displayed Data in AjaxScaffold blinklist:Adding More Control to the Displayed Data in AjaxScaffold furl:Adding More Control to the Displayed Data in AjaxScaffold reddit:Adding More Control to the Displayed Data in AjaxScaffold fark:Adding More Control to the Displayed Data in AjaxScaffold blogmarks:Adding More Control to the Displayed Data in AjaxScaffold Y!:Adding More Control to the Displayed Data in AjaxScaffold smarking:Adding More Control to the Displayed Data in AjaxScaffold magnolia:Adding More Control to the Displayed Data in AjaxScaffold segnalo:Adding More Control to the Displayed Data in AjaxScaffold

Comments

Comments are disabled

Subscribe to The Dev Blog