I'm using the Rails3 beta, will_paginate gem, and the geokit gem & plugin.
As the geokit-rails plugin, doesn't seem to support Rails3 scopes (including the :origin symbol is the issue), I need to use the .find syntax.
In lieu of scopes, I need to combine two sets of criteria in array format:
I have a default condition:
conditions = ["invoices.cancelled = ? AND invoices.paid = ?", false, false]
I may need to add one of the following conditions to the default condition, depending on a UI selection:
#aged 0
lambda {["created_at IS NULL OR created_at < ?", Date.today + 30.days]}
#aged 30
lambda {["created_at >= ? AND created_at < ?", Date.today + 30.days, Date.today + 60.days]}
#aged > 90
lamdba {["created_at >= ?", Date.today + 90.days]}
The resulting query resembles:
@invoices = Invoice.find(
:all,
:conditions => conditions,
:origin => ll #current_user's lat/lng pair
).paginate(:per_page => @per_page, :page => params[:page])
Questions:
- Is there an easy way to combine these two arrays of conditions (if I've worded that correctly)
- While it isn't contributing to the problem, is there a DRYer way to create these aging buckets?
- Is there a way to use Rails3 scopes with the geokit-rails plugin that will work?
Thanks for your time.
I think a better way is to use Anonymous scopes.
Check it out here: http://railscasts.com/episodes/112-anonymous-scopes
Try this:
Edit Approach 2
Monkey patch the
Array
class. Create a file calledmonkey_patch.rb
inconfig/initializers
directory.Now you can do this: