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! ;)

Tags , ,  | 15 comments