augustl (owner)

Revisions

gist: 23447 Download_button fork
public
Description:
Experimental finder syntax thingie, lol.
Public Clone URL: git://gist.github.com/23447.git
Embed All Files: show embed
conditions.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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