New release of ez_where plugin
Posted by ezmobius Fri, 30 Jun 2006 19:58:00 GMT
Fabien Franzen and I have created a new release of the ez-where plugin with many new features and a much nicer API. Some things have changed including module and class names so if you are using an old verion and want to update you will have to make a few small search and replace changes. Change Caboose::EZ::Condition to EZ::Where::Condition. Other then that your old code should work fine.But the new API is much nicer to use. And we have added support for multi search easier by excluding nil or empty values from a search.
So instead of:
cond = EZ::Where::Condition.new :my_table do
title =~ "%#{params[:search]}%" unless params[:search].blank?
user == params[:user] unless params[:user].blank?
end
You can do:
cond = EZ::Where::Condition.new :my_table do
title =~ "%#{params[:search]}%"
user == params[:user]
end
And if any of the right hand side values are blank? that whole line will be excluded from the query. There are way too many new features to list here so I will show some of the highlights. The huge test cases are currently the best place to look to see all the things this plugin can do. I will wrtie up a few more tutorials soon on some even more advanced features like compositions.
So without further ado, here are some cool things you can do with the new version:
# find all posts where with body LIKE '%rails%'
@posts = Post.find_where(:all) { |p| p.body =~ "%rails%" }
=> ["posts.body LIKE ?", "%rails%"]
# find all posts where with title, body or extended LIKE '%rails%'
@posts = Post.find_where :all do |post|
post.any_of(:title, :body, :extened) =~ '%rails%'
end
=> ["(posts.title LIKE ? OR posts.body LIKE ?
OR posts.extended LIKE ?)", "%rails%", "%rails%", "%rails%"]
# find all articles with title, body or extended LIKE "%#{params[:search]}%"
# AND (author.name = params[:author] OR comment.body LIKE "%#{params[:search]}%")
@articles = Article.find_where(:all, :include => [:author, { :comments => :users }]) do
|article, author, comment|
article.any_of(:title, :body, :extended) =~ "%#{params[:search]}%"
any {
author.name == params[:author]
comment.body =~ "%#{params[:search]}%"
}
end
=>["(articles.title LIKE ? OR articles.body LIKE ?
OR articles.extended LIKE ?) AND ((authors.name = ?)
OR (comments.body LIKE ?))", "%foo%", "%foo%", "%foo%", "Ezra", "%foo%"]
One of the coolest new features is the c method. This promotes conditions
into first class objects. It works like this:
c{ title =~ '%foo%' }.to_sql
=> ["title LIKE ?", "%foo%"]
With a naked block like this it just converts whats inside the block
into a condition.
c(:posts) { title =~ '%foo%' }.to_sql
=> ["posts.title LIKE ?", "%foo%"]
When you give it a plural table name as an arguments it scopes the condition to that
table.
You can also use the c object directly in a find like so:
Post.find :all, :conditions => c{ body =~ '%rails%' }
Now that you can have conditions as objects you can do some really interesting
and complex queries with them. You can use operators to define how
the conditions get strung together. So:
+ == AND
| == OR
- == AND NOT
For example:
cond1 = c(:posts) { body =~ '%rails%'}
cond2 = c(:comments) { username == 'ezmobius'}
cond3 = c(:posts) { author_id === (1..4) }
(cond1 + cond2 | cond3).to_sql
=> ["((posts.body LIKE ?) AND (comments.username = ?))
OR (posts.author_id IN (?))", "%rails%", "ezmobius", [1, 2, 3, 4]]
Now you can pass these into a find like this and .to_sql will
automatically be called on the compound condition:
Post.find :all, :conditions => (cond1 + cond2 | cond3)
You can also build up one compound condition use +=, -= or |=
cond = c{ title =~ '%ruby%' }
cond += c{ description =~ '%ez-where%' }
cond |= c{ user_id === (1..5) }
cond -= c{ pub_date > Time.now.to_s(:db) }
Post.find :all, :conditions => cond
=> ["((title LIKE ?) AND (description LIKE ?))
OR ((user_id IN (?)) AND NOT (pub_date > ?))",
"%ruby%", "%ez-where%", [1, 2, 3, 4, 5], "2006-06-21 01:17:47"]
Get it here:$ script/plugin install svn://rubyforge.org//var/svn/ez-where
I moved the project to rubyforge so there is now a mailing list where you can ask questions or contribute ideas.
http://rubyforge.org/mailman/listinfo/ez-where-devel Enjoy! ;)
Searching...





Ezra, your plugin is amazing! I love the ruby way of sql ;-) (I hate do sql queries) Thank you for your work!
Thanks Luis, I'm glad you like it!
Ezra, thank you very much! Your plugin is one of the most useful I have tried. You have saved me a lot of work when building advanced search forms.
Great! Nested includes are working! That is what I was looking forward to. Thanks Ezra!
Thanks man, really cool!
Thanks for the plugin, works great. However, I'm trying to get something done, but it's not working, maybe you can help. Users of my search form are allowed to specify whether they want a field to be <=, ==, >= to a certain value. Rather than doing: case (comparator) when "==" field == value when "<=" field <= value when ">=" field >= value end I'd like to do something like send((field + comparator).to_sym, value) But that doesn't work. Any idea?
Found it myself; Condition has a create_clause method which takes care of what I want.
I tried to get the new version at:
svn://rubyforge.org//var/svn/ez-where
I got the following error mesage:
svn: Can't connect to host 'rubyforge.org': Permission denied
Can anyone help, please?
Thanks.
I haven't tried the plugin, but from what i have seen so far, it is exactly what i am looking for. amazing! Now I need something for dynamically creating group_by statements ;)
Wow great. The nested includes are working well. Thats what i looked for. Thanks!
This ne API is much better. I was using the old one. This plugin is the most useful. Especially the nested include I like a lot. Thanks for realeasing this.
Great Plugin! How do you do case insensitive searches?
can you do REGEX or RLIKE conditions?
I'm having trouble creating a condition with a single NOT:
def test_EZ_Where_not_problemcond_tmp = EZ::Where::Condition.new
tmp2 = cond_tmp.not( c {id==5} )
assert_equal ["NOT (id = ?)", 5], tmp2.to_sql
# This fails, because ["(id = ?)", 5] is returned
end
How can you create a single NOT condition?
@Erik
c { id! == 5 }.to_sql