[ANN] ez_where plugin updated features


#1

Friends-

I wanted to let people know that there is a new experimental release

of this plugin. I would love feedback on syntax and features. There
is now a full test suite with fixtures that covers all the available
syntax. Look at the test suite for more syntax possibilities. There
have been many additions since my last release. Fabien Atelier has
been working on this with me and has added a bunch of great code.

You can get it from here:

http://http://brainspl.at/opensvn.csie.org/ezra/rails/plugins/dev/
ez_where/

This is a request for feedback. I want to solidify the api soon so

suggestions are welcome.

Here’s a snippet from the README:

Welcome to the new improved ez_where plugin for rails. This plugin is
meant
to be used as a nice ruby like syntax for creating the :conditions
part of an
ActiveRecord::Base.find. We also add the ActiveRecord::Base.ez_find
method.
This method takes a block to simplify single and multi table queries.
There
is now support for nested sub conditions using a few different
techniques. The
default boolean used is ‘AND’. any takes a subcondition block and
uses ‘OR’
to join them together.

@articles = Article.ez_find(:all, :include => :author) do |article,
author|
article.title =~ “%Foo Title%”
author.any do
name == ‘Ezra’
name == ‘Fab’
end
end

This issue a find with these :conditions => [“article.title LIKE ? AND
(authors.name = ? OR authors.name = ?)”, “%Foo Title%”, “Ezra”, “Fab”]

Basically here is the breakdown of how we map ruby operators to SQL
operators:

foo == ‘bar’ #=> [“foo = ?”, ‘bar’]
foo =~ ‘%bar’ #=> [“foo LIKE ?”, ‘%bar’]
foo <=> (1…5) #=> [“foo BETWEEN ? AND ?”, 1, 5]
id === [1, 2, 3, 5, 8] #=> [“id IN(?)”, [1, 2, 3, 5, 8]]
<, >, >=, <= et all will just work like you expect.

And here is a link to my blog with the entire README for your perusal:

Cheers-
-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732


#2

Nice idea Ezra! Will play with it when I can.

b


#3

Harvey-

Thanks for testing this out for me. It will be easy to do what you

want. The problem you are having relates to the use of instance vars
like @postcode_var. Since the condition is getting evaluated inside
the Caboose::EZ::Condition block, @postcode_var won’t work and will
return the sting you wanted as ‘%%’. The way around this is to just
use plain old local vars. SO what you want to do can be done like this:

postcode_var = params[:home_search][:postcode_area]

cond = Caboose::EZ::Condition.new do
postcode =~ “%#{postcode_var}%”
end

@record_count = Home.count(cond.to_sql)

Also you could do the same thing like this:

cond = Home.ez_condition { postcode =~ “%#{postcode_var}%” }

@record_count = Home.count(cond.to_sql)

I need to add to the docs the fact that you can't use @instance vars

inside the conditions as they are not evaluated in the correct
context. Try this solution out and see how it works for you and let
me know.

Cheers-
-Ezra

On Jan 31, 2006, at 11:41 AM, Harvey B. wrote:

The problem I am having is building the where condition. I cannot

Search for a home using the information entered in index.

  end

Processing HomeController#search (for 127.0.0.1 at 2006-01-31
(postcode LIKE ‘%%’) e[0m
Rendered shared/_list_object (0.00250)
Rendered shared/_list_object (0.00248)
(0.00587) Rendered shared/_top_menu (0.00289) Rendered
Harvey

now a full test suite with fixtures that covers all the available

This method takes a block to simplify single and multi table queries.
name == ‘Fab’

This e-mail has been scanned for all viruses by MessageLabs.

Welcome to the new improved ez_where plugin for rails. This plugin is
uses ‘OR’

foo <=> (1…5) #=> [“foo BETWEEN ? AND ?”, 1, 5]
WebMaster
http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
removed_email_address@domain.invalid


#4

This plugin seems to be exactly what I am looking for. However, I¹ve
been struggling this afternoon to get it to work. I should point out
that I am a newbie to Rails. I¹ve created a search page using a model
which does not have a corresponding table, as outlined here
(http://rails.techno-weenie.net/tip/2005/11/19/validate_your_forms_with_a_ta
ble_less_model). This bit works perfectly as the simple validation
rules
work as expected.

The problem I am having is building the where condition. I cannot seem
to get the right syntax to get hold of the params value and build the
condition. I¹m just trying to build a very simple example. I have a
textfield called postcode_area and all I want to do is create the
following select (SELECT * FROM homes WHERE postcode LIKE
‘%postcode_area%’

The code in the controller is as follows:-

Search for a home using the information entered in index.

def search
@home_search = HomeSearch.new(params[:home_search])

if @home_search.valid?
@postcode_var = params[:home_search][:postcode_area]

  condition = Caboose::EZ::Condition.new do
    postcode =~ "%#{@postcode_var}%"
   # postcode =~ "%G77%"
  end

  @record_count = Home.count(condition.to_sql)
  if @record_count == 0
    flash_message 'There are no results for this search.  Please try 

again.’
redirect_to :action => ‘index’
else
@list_pages, @list_objects = paginate :homes, :per_page => 25,
:conditions => condition.to_sql
render :action => ‘list’
end
else
render :action => ‘index’
end
end

Everything in the table is returned because the SQL used is SELECT *
FROM homes WHERE (postcode LIKE ‘%%’). Here is the development log
output:-

Processing HomeController#search (for 127.0.0.1 at 2006-01-31 16:29:07)
[POST]
Parameters: {“home_search”=>{“postcode”=>“G77”, “room_types”=>"",
“care_types”=>“nursing care”}, “commit”=>“Search”, “action”=>“search”,
“controller”=>“home”, “max_price”=>“2000”, “min_price”=>“0”}
e[4;36;1mHome Count (0.002739)e[0m e[0;1mSELECT COUNT() FROM homes
WHERE (postcode LIKE ‘%%’) e[0m
e[4;35;1mHome Count (0.002695)e[0m e[0mSELECT COUNT(
) FROM homes
WHERE
(postcode LIKE ‘%%’) e[0m
e[4;36;1mHome Load (0.003985)e[0m e[0;1mSELECT * FROM homes WHERE
(postcode LIKE ‘%%’) LIMIT 0, 25e[0m
Rendering layoutfalseactionlist within layouts/application Rendering
home/list Rendered shared/_list_left_side (0.00054)
e[4;35;1mHome Columns (0.001351)e[0m e[0mSHOW FIELDS FROM homese[0m
Rendered shared/_list_object (0.04215)
Rendered shared/_list_object (0.00287)
Rendered shared/_list_object (0.00250)
Rendered shared/_list_object (0.00266)
Rendered shared/_list_object (0.00250)
Rendered shared/_list_object (0.00267)
Rendered shared/_list_object (0.00823)
Rendered shared/_list_object (0.00245)
Rendered shared/_list_object (0.00292)
Rendered shared/_list_object (0.00254)
Rendered shared/_list_object (0.00229)
Rendered shared/_list_object (0.00230)
Rendered shared/_list_object (0.00248)
Rendered shared/_list_object (0.00238)
Rendered shared/_list_object (0.00232)
Rendered shared/_list_object (0.00248)
Rendered shared/_list_object (0.00256)
Rendered shared/_list_object (0.00231)
Rendered shared/_list_object (0.00247)
Rendered shared/_list_object (0.00245)
Rendered shared/_list_object (0.00232)
Rendered shared/_list_object (0.00230)
Rendered shared/_list_object (0.00255)
Rendered shared/_list_object (0.00232)
Rendered shared/_list_object (0.00232)
Rendered shared/_list_right_side (0.13730) Rendered shared/_html_head
(0.00587) Rendered shared/_top_menu (0.00289) Rendered
shared/_agecare_menu (0.00548) Rendered shared/_footer (0.00178)
Completed in 0.20923 (4 reqs/sec) | Rendering: 0.16196 (77%) | DB:
0.01077
(5%) | 200 OK [http://localhost/home/search]

I know I am doing something really stupid but I have been going round in
circles all afternoon. I would really appreciate any assistance.
Regards
Harvey

On 31/1/06 03:32, “Ezra Z.” removed_email_address@domain.invalid wrote:

Friends-

I wanted to let people know that there is a new experimental release
of this plugin. I would love feedback on syntax and features. There is
now a full test suite with fixtures that covers all the available
syntax. Look at the test suite for more syntax possibilities. There
have been many additions since my last release. Fabien Atelier has
been working on this with me and has added a bunch of great code.

You can get it from here:

http://http://brainspl.at/opensvn.csie.org/ezra/rails/plugins/dev/ez_w
here/

This is a request for feedback. I want to solidify the api soon so
suggestions are welcome.

Here’s a snippet from the README:

Welcome to the new improved ez_where plugin for rails. This plugin is
meant to be used as a nice ruby like syntax for creating the
:conditions part of an ActiveRecord::Base.find. We also add the ActiveRecord::Base.ez_find method.
This method takes a block to simplify single and multi table queries.
There is now support for nested sub conditions using a few different
techniques. The default boolean used is ‘AND’. any takes a subcondition block and uses ‘OR’
to join them together.

@articles = Article.ez_find(:all, :include => :author) do |article, author|
article.title =~ “%Foo Title%”
author.any do
name == ‘Ezra’
name == ‘Fab’
end
end

This issue a find with these :conditions => [“article.title LIKE ? AND
(authors.name = ? OR authors.name = ?)”, “%Foo Title%”, “Ezra”, “Fab”]

Basically here is the breakdown of how we map ruby operators to SQL operators:

foo == ‘bar’ #=> [“foo = ?”, ‘bar’]
foo =~ ‘%bar’ #=> [“foo LIKE ?”, ‘%bar’]
foo <=> (1…5) #=> [“foo BETWEEN ? AND ?”, 1, 5]
id === [1, 2, 3, 5, 8] #=> [“id IN(?)”, [1, 2, 3, 5, 8]] <, >, >=, <=
et all will just work like you expect.

And here is a link to my blog with the entire README for your perusal:

http://brainspl.at/articles/2006/01/30/i-have-been-busy

Cheers-

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732

This e-mail has been scanned for all viruses by MessageLabs.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

Ezra Z. wrote:

Friends-

I wanted to let people know that there is a new experimental release
of this plugin. I would love feedback on syntax and features. There
is now a full test suite with fixtures that covers all the available
syntax. Look at the test suite for more syntax possibilities. There
have been many additions since my last release. Fabien Atelier has
been working on this with me and has added a bunch of great code.

You can get it from here:

http://http://brainspl.at/opensvn.csie.org/ezra/rails/plugins/dev/
ez_where/

This is a request for feedback. I want to solidify the api soon so
suggestions are welcome.

Here’s a snippet from the README:

Welcome to the new improved ez_where plugin for rails. This plugin is
meant
to be used as a nice ruby like syntax for creating the :conditions
part of an
ActiveRecord::Base.find. We also add the ActiveRecord::Base.ez_find
method.
This method takes a block to simplify single and multi table queries.
There
is now support for nested sub conditions using a few different
techniques. The
default boolean used is ‘AND’. any takes a subcondition block and
uses ‘OR’
to join them together.

@articles = Article.ez_find(:all, :include => :author) do |article,
author|
article.title =~ “%Foo Title%”
author.any do
name == ‘Ezra’
name == ‘Fab’
end
end

This issue a find with these :conditions => [“article.title LIKE ? AND
(authors.name = ? OR authors.name = ?)”, “%Foo Title%”, “Ezra”, “Fab”]

Basically here is the breakdown of how we map ruby operators to SQL
operators:

foo == ‘bar’ #=> [“foo = ?”, ‘bar’]
foo =~ ‘%bar’ #=> [“foo LIKE ?”, ‘%bar’]
foo <=> (1…5) #=> [“foo BETWEEN ? AND ?”, 1, 5]
id === [1, 2, 3, 5, 8] #=> [“id IN(?)”, [1, 2, 3, 5, 8]]
<, >, >=, <= et all will just work like you expect.

And here is a link to my blog with the entire README for your perusal:

http://brainspl.at/articles/2006/01/30/i-have-been-busy

Cheers-
-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732