While working on the replacement web app for katrinahousing.org, I came up with the following query abstraction that may be useful to others. I haven''t seen this tackled before (and if someone else has done it, I''d like to know) so here''s a query mixin that will greatly simplify the case where you want to use the same form that you use to edit an object to search on that type of object. For example: class User < ActiveRecord::Base has_many :hobbies performs_query :include => [:hobbies] end Later, in your controller, you can make the query like this: users = User.query(params[:user]) (imagine that params[:user] is a hash that looks something like: { :first_name => "", :last_name => "", :phone => "555-1234", :email => "" } ) I''ve included the mixin as an attachment. Put it in lib/ and require it in environment.rb. Enjoy, Duane Johnson (canadaduane) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 8-okt-2005, at 22:00, Duane Johnson wrote:> While working on the replacement web app for katrinahousing.org, I > came up with the following query abstraction that may be useful to > others. > > I haven''t seen this tackled before (and if someone else has done > it, I''d like to know) so here''s a query mixin that will greatly > simplify the case where you want to use the same form that you use > to edit an object to search on that type of object. > > For example: > > class User < ActiveRecord::Base > has_many :hobbies > performs_query :include => [:hobbies] > end > > Later, in your controller, you can make the query like this: > > users = User.query(params[:user]) >Wha! This is QBE and it rocks (just as other hacks of yours). I want this in Rails 1.0. Really. -- Julian "Julik" Tarkhanov
On Oct 8, 2005, at 7:00 PM, Julian ''Julik'' Tarkhanov wrote:> On 8-okt-2005, at 22:00, Duane Johnson wrote: > >> While working on the replacement web app for katrinahousing.org, I >> came up with the following query abstraction that may be useful to >> others. >> >> I haven''t seen this tackled before (and if someone else has done >> it, I''d like to know) so here''s a query mixin that will greatly >> simplify the case where you want to use the same form that you use >> to edit an object to search on that type of object. >> >> For example: >> >> class User < ActiveRecord::Base >> has_many :hobbies >> performs_query :include => [:hobbies] >> end >> >> Later, in your controller, you can make the query like this: >> >> users = User.query(params[:user]) >> >> > > Wha! This is QBE and it rocks (just as other hacks of yours). I > want this in Rails 1.0. Really.The latest rails supports plugins, and this would be a great example of one. The plugin developer (Duane) would simply create a project with the following directory structure: query-mixin/ query-mixin/init.rb query-mixin/lib query-mixin/lib/query.rb The ''init.rb'' would look something like this: # assuming the mixin is called Query ActiveRecord::Base.send(:include, Query) Then, those that want to use it would just throw the project into their vendor/plugins directory, and rails will automatically detect it. The vendor/plugins/query-mixin/lib/ directory will be automatically added to the load path, and the ''init.rb'' file will be loaded when the application loads. Makes things like this a cinch to distribute, and helps us keep rails slimmer by making the core team feel less guilty about telling people to distribute their add-on independently. :) - Jamis
On Sunday 09 October 2005 04:03, Jamis Buck wrote:> The latest rails supports plugins, and this would be a great example > of one.Jamis, currently plugins are restricted to Ruby code; the load path is extended by another directory. Are there any plans to make it possible to include views and assets such as scripts and images? Michael -- Michael Schuerig Airtight arguments have mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org vacuous conclusions. http://www.schuerig.de/michael/ --A.O. Rorty, Explaining Emotions
On Oct 8, 2005, at 8:59 PM, Michael Schuerig wrote:> On Sunday 09 October 2005 04:03, Jamis Buck wrote: > >> The latest rails supports plugins, and this would be a great example >> of one. >> > > Jamis, > > currently plugins are restricted to Ruby code; the load path is > extended > by another directory. Are there any plans to make it possible to > include views and assets such as scripts and images?My exception email notification plugin has (ActionMailer) views in it--it can be done, with a bit of minor trickery. As for assets and images, how would you propose making that work? The plugins system as it currently stands is just the first iteration--I''m sure it''ll grow as people''s needs require it to. Patches, as ever, are welcome. - Jamis
On Sunday 09 October 2005 05:40, Jamis Buck wrote:> As for assets and > images, how would you propose making that work? The plugins system as > it currently stands is just the first iteration--I''m sure it''ll > grow as people''s needs require it to. Patches, as ever, are welcome.I have no idea :-) But I''d like to have the functionality. Michael -- Michael Schuerig Face reality and stare it down mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org --Jethro Tull, Silver River Turning http://www.schuerig.de/michael/
On Oct 8, 2005, at 7:03 PM, Jamis Buck wrote:>> On 8-okt-2005, at 22:00, Duane Johnson wrote: >>> While working on the replacement web app for katrinahousing.org, >>> I came up with the following query abstraction that may be useful >>> to others. > > The latest rails supports plugins, and this would be a great > example of one. The plugin developer (Duane) would simply create a > project with the following directory structure: >This is fantastic! Thanks for the heads-up, Jamis. I''ll look in to developing a plugin when I get the chance. Duane Johnson (canadaduane)