218e262d73d0c38480a087b19f916b09

Users can add yearly recurring events with a starts_at and ends_at field, specifying start/end day/month. I have to find the current events, for which I use a scheme as specified here: http://gist.github.com/4692

I am using SQLite3, and this is one hell of an ugly piece of code. Give it a shot!

Event.find(:all, :conditions =>
                    "((strftime('%m%d', ends_at) <= strftime('%m%d', starts_at)) AND (strftime('%m%d', 'now') <= strftime('%m%d', ends_at)))
                    OR
                    ((strftime('%m%d', starts_at) <= strftime('%m%d', ends_at)) AND (strftime('%m%d', 'now') <= strftime('%m%d', ends_at)) AND (strftime('%m%d', 'now') >= strftime('%m%d', starts_at)))
                    OR
                    ((strftime('%m%d', ends_at) <= strftime('%m%d', starts_at)) AND (strftime('%m%d', starts_at) <= strftime('%m%d', 'now')))")

Refactorings

No refactoring yet !

B066cb3c505933f832faa83238489a89

halogenandtoast

October 30, 2008, October 30, 2008 19:45, permalink

No rating. Login to rate!

This was a pain to figure out, but this should work

Event.find(:all, :conditions =>
                    "(strftime('%m%d', 'now') between strftime('%m%d', starts_at) and strftime('%m%d', ends_at)) or (strftime('%m%d', ends_at) <= strftime('%m%d', starts_at))")

Your refactoring





Format Copy from initial code

or Cancel