On Fri, Aug 15, 2008 at 5:28 AM, Matt Wynne <[EMAIL PROTECTED]> wrote:
> 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
Hey Matt - welcome!
The paginate() method lives on the model class, so there's nothing
stopping you from wrapping those calls in methods on the model,
slinging around the params object.
# CityController
def get_cities
City.paginate_all(params)
end
# City
def self.paginate_all(params)
self.paginate(:all, get_find_params(params).merge!(:page => params[:page]))
end
etc
HTH,
David
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users