The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Adding More Control to the Displayed Data in AjaxScaffold

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Guy Naor

Posts: 104
Nickname: familyguy
Registered: Mar, 2006

Guy Naor is one of the founders of famundo.com and a long time developer
Adding More Control to the Displayed Data in AjaxScaffold Posted: Feb 15, 2007 6:47 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Guy Naor.
Original Post: Adding More Control to the Displayed Data in AjaxScaffold
Feed Title: Famundo - The Dev Blog
Feed URL: http://devblog.famundo.com/xml/rss/feed.xml
Feed Description: A blog describing the development and related technologies involved in creating famundo.com - a family management sytem written using Ruby On Rails and postgres
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Guy Naor
Latest Posts From Famundo - The Dev Blog

Advertisement

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.

Read: Adding More Control to the Displayed Data in AjaxScaffold

Topic: How much ���less code��� is better? Previous Topic   Next Topic Topic: heckle version 1.3.0 has been released!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use