Setting the :condition for list crud?

1 view
Skip to first unread message

rberger

unread,
May 26, 2007, 3:56:23 AM5/26/07
to Streamlined
I want to be able to pass in a conditon as the parameter in the url
for listing and I can't figure out how to do that and still use the
standard list function of streamlined.

The example is:
Conference has many participants

>From a Conference entry I want to link to a listing of all the
participants. The route would be:
map.connect 'legs_for/:conference_id',
:controller => "participant",
:action => "list"

and I want the list action to check for the parameter conference_id
and if it exists limit the list to those participants that meet that
criteria, something like:

Participant.find(:all, :conditions => ["conference_id =
#{params[:conference_id]}"

Is there a way to do something like this?

Is there a way to pass in :conditions to the finds being used in the
CrudMethods::list ?

Thanks!

Matthew

unread,
May 27, 2007, 8:27:43 PM5/27/07
to strea...@googlegroups.com
Nope, there currently isn't a way to do this that I know of. It's a
good idea, though! Please open a ticket on Trac and we'll look into
adding it. (Better yet, post your own patch!)

http://streamlinedframework.org:8079/trac/

Matthew

rberger

unread,
Jun 1, 2007, 5:09:04 PM6/1/07
to Streamlined
Ok, I submitted a ticket on it.

In the mean time I did accomplish my goal using the around_filter
with_scope kludge:

In application.rb:

class ApplicationController < ActionController::Base
# Pick a unique cookie name to distinguish our session data from
others'
session :session_key => '_zipdx_dash_session_id'

def filter_legs_by_conference_id
return yield if params[:conference_id].nil?

Leg.with_scope( :find => { :conditions => ["conference_id =
#{params[:conference_id]}"]}, :create => {:conference_id =>
params[:conference_id] } ) do
yield
end
end
end

Then in my LegController (the controller associated with the model
that contains the "belongs_to" declaration) I added:

around_filter :filter_legs_by_conference_id


Craig Buchek

unread,
Jun 12, 2007, 8:01:42 PM6/12/07
to Streamlined
I've been working on this since I attended the Streamlined tutorial
session at RailsConf2007. I came up with some not-so-great patches to
Streamlined that worked, but not very well. Then I discovered a
technique that doesn't require any modifications to Streamlined
(except overriding 1 simple core method). I spent an hour writing it
up here on Google Groups, but somehow Google must have eaten it. So
this is a shorter summary. I'll write up the full discussion in a few
days or weeks, on my blog, and on the Streamlined Trac/wiki.

My example uses Calculations and Users:

class Calculation < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :calculations
has_many :misc_calculations, :class_name =>
'Calculation', :conditions => 'project_id = 0 OR project_id IS NULL'
end
class CalculationsController < ApplicationController
layout 'streamlined'
acts_as_streamlined
before_filter :get_user_collection
def misc
@collection = @user.misc_calculations
# Call into the Streamlined list action.
# (We could just use list, if we haven't messed w/ it.)
streamlined_list
end
private
alias :streamlined_list :list
def get_user_collection
@user = User.find(params[:user])
@collection = @user.calculations
end
def model
@collection
end
end

There are a few subtle tricks involved here. First, we override
Streamlined's 'model' method. This would normally return the
Calculations class object itself. But Streamlined only uses a few
methods on the object, and it turns out that AR association proxies
implement basically the same methods as the AR class objects. So we
can use @user.calculations in place of Calculations. In my 'misc'
action, I added some more conditions using another named/conditioned
association (I couldn't find a name for this, but here's a great
article on it: http://www.adambondurant.com/2007/5/3/easier-association-proxies).
So we can use that in place of Calculations as well.

I'd like some feedback on this technique. I haven't tested it too
much, but it's working great for my needs. It even prevents users from
editing items that don't belong to them. I haven't tested it with
HABTM or HMT yet. I don't see why it wouldn't work, although looking
through the AR association proxy code for HABTM, I think it may be
missing the count() method that Streamlined needs. It shouldn't be too
hard to add that though. (Either by monkey-patching AR, or extending
the association as in the first example in the URL I referenced
above.)

Enjoy,
Craig

Mark Bennett

unread,
Jun 13, 2007, 8:27:20 AM6/13/07
to strea...@googlegroups.com
Thats cool.  Does it work with paging, sorting and filtering?

Mark

can use @ user.calculations in place of Calculations. In my 'misc'

Craig Buchek

unread,
Jun 13, 2007, 12:09:52 PM6/13/07
to Streamlined
> Thats cool. Does it work with paging, sorting and filtering?

Yes. I've tested it with all of those, plus listing, editing/updating,
creating, and showing.

Craig

Reply all
Reply to author
Forward
0 new messages