[rspec-users] Proper Encapsulation of SQL WHERE / ORDER BY Clauses

Matt Wynne matt at mattwynne.net
Fri Aug 15 06:28:35 EDT 2008


Hi TDD Fans,

I'm pretty new to Ruby / RSpec / Rails but not to TDD.

This is more of a general 'how do you do good design in a rails app'  
question than an rspec-specific question. I'm asking it here because  
I know this list is read by lots of people who care about good  
design, but please feel free to point me somewhere else if you think  
it's not relevant to this list.

Probably through my inexperience with the language / framework, I'm  
finding that I'm tending to clutter my controllers with SQL-specific  
stuff.

e.g.

     def get_cities

       City.paginate(:all, get_find_params.merge!( :page => params 
[:page] ))

     end

     def get_find_params

       find_params = { :order => get_order_clause }

       if params[:name] || params[:last_24]
         find_params.merge! :conditions => get_conditions
       end

       return find_params

     end

     def get_conditions
       "name like '%#{params[:name]}%'" + (params[:last_24] ? " AND  
created_at >= '#{DateTime.now - 1.days}'" : "")
     end

     def get_order_clause
       (params[:sort] ? 'created_at DESC, ' : "") + 'name ASC'
     end

This is obviously horribly brittle to write specs for, but I'm not  
really sure what I should do instead...

How do I get my models to encapsulate this stuff, especially given  
I'm using the will_paginate plug-in?

Any tips / pointers greatly appreciated.

cheers,
Matt

http://blog.mattwynne.net
http://songkick.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080815/8647e5e3/attachment-0001.html>


More information about the rspec-users mailing list