Forum: Ruby on Rails [ANN] ez_where plugin updated features.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ezra Z. (Guest)
on 2006-01-31 05:35
(Received via mailing list)
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/ra...
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
Ben M. (Guest)
on 2006-01-31 07:43
(Received via mailing list)
Nice idea Ezra! Will play with it when I can.

b
Harvey B. (Guest)
on 2006-01-31 21:41
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/vali...
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"}
  Home Count (0.002739)   SELECT COUNT(*) FROM homes
WHERE (postcode LIKE '%%') 
  Home Count (0.002695)   SELECT COUNT(*) FROM homes
WHERE
(postcode LIKE '%%') 
  Home Load (0.003985)   SELECT * FROM homes WHERE
(postcode LIKE '%%') LIMIT 0, 25
Rendering layoutfalseactionlist within layouts/application Rendering
home/list Rendered shared/_list_left_side (0.00054)
  Home Columns (0.001351)   SHOW FIELDS FROM homes
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/ra...
> 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/ra...
> 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
Ezra Z. (Guest)
on 2006-01-31 23:05
(Received via mailing list)
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 '%%') 
> 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
This topic is locked and can not be replied to.