Skip to content

Instantly share code, notes, and snippets.

@augustl
Created November 10, 2008 08:15
Show Gist options
  • Select an option

  • Save augustl/23447 to your computer and use it in GitHub Desktop.

Select an option

Save augustl/23447 to your computer and use it in GitHub Desktop.
Experimental finder syntax thingie, lol.
require 'rubygems'
require 'active_support'
# Turns
# c('title LIKE ', "%#{query}%", ' AND published = ', true)
# into
# ['title LIKE ? AND published = ?', "%#{query}%", true]
#
# In other words, ladies and mentlegen, you can now do a find as such:
# find(:all, :conditions => c('gender =', params[:gender], 'AND published =', true))
#
# I don't think I'll ever, ever use this myself.
def c(*conditions)
conditions.in_groups_of(2).inject([""]) do |ary, group|
ary[0] << "#{group[0].strip} ? "
ary << group[1]
ary
end
end
query = "hey"
puts c('title LIKE', "%#{query}%", 'AND published =', true).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment