using with scope_out
Reported by Deleted User | August 9th, 2007 @ 12:43 AM
I've been using scope_out to define finders on my models
class Task < ActiveRecord::Base
scope_out :recent_incomplete, :conditions => {:completed => nil},
:order => 'tasks.created_at DESC',
:include => :user
end
and I also have a User model which has many tasks
class User < ActiveRecord::Base
has_many :tasks
end
The database table has a range of tasks for different users, some complete, some not.
and I was wanting to use these with will_paginate so I could do the following in my controller:
@all_incomplete_tasks = Task.paginate_recent_incomplete(:all, :page => params[:page])
@user_incomplete_tasks = current_user.tasks.paginate_recent_incomplete(:all, :page => params[:page])
However, the problem I found was that although the correct records were returned by the call to paginate_recent_incomplete, the total number of records (and hence the number of pages) was incorrect as it ignores the scoping implied by scope_out (wp_count! doesn't take this into account).
I've updated the code for wp_count! so it handles the above syntax when used with scope_out and doesn't break the existing tests. I've, however, not added any tests to validate the code I've added. I'm only just starting to learn about testing so that's a bit beyond me for the moment.
Not sure if this is a sensible thing to do or not - patch attached for your perusal though
Cheers
Rupert
Comments and changes to this ticket
-
Chris Wanstrath August 9th, 2007 @ 12:43 AM
- State changed from new to open
- Assigned user changed from Chris Wanstrath to Mislav
-
Deleted User August 9th, 2007 @ 12:43 AM
I've just updated the patch....
with_scope adds a method which can be used like with_scope to use the defined scope.
So in the example above a method Task.with_recent_incomplete is added. In the new version of the patch, if this method exists it evaluates the count within it.
Still not sure if this is a good idea or not ...dependencies between plugins, but it's proved useful for me!
Cheers
Rupert
-
Jack Danger August 9th, 2007 @ 12:43 AM
+1 for this patch.
Scope out is killer useful. So's will paginate. Any chance we can get them to play nice?
-
Chris Wanstrath August 18th, 2007 @ 05:04 AM
- State changed from open to resolved
(from [326]) will_paginate: add in support for the scope_out plugin [rupertv, Chris Wanstrath] [#70 state:resolved]
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป
Everyone's favorite Ruby library for pagination of practically anything!