An architectural query here I guess.
I’m building a shopping site, when a user sees a results page they can then filter by price, size, brand, store etc. The way I do this is I send the filters up to the url – this works fine. However –
My controller (actually recently moved this logic into the model) has so many if else statements its crazy. My current implementation has:
######## different rhs scenarios ########
if params[:brand] and params[:store]
@products = Product.search_tank(params[:search], :per_page => @@pp, :page => params[:page], :category_filters => {:store => "#{params[:store]}", :brand => "#{params[:brand]}"})
elsif params[:store]
@products = Product.search_tank(params[:search], :per_page => @@pp, :page => params[:page], :category_filters => {:store => "#{params[:store]}"})
elsif params[:brand]
@products = Product.search_tank(params[:search], :per_page => @@pp, :page => params[:page], :category_filters => {:brand => "#{params[:brand]}"})
elsif params[:price]
@products = Product.search_tank(params[:search], :per_page => @@pp, :page => params[:page], :category_filters => {:price => "#{params[:price]}"})
else
@products = Product.search_tank(params[:search], :per_page => @@pp, :page => params[:page])
end
It basically means I need to have every combination:
1) search
2) search + price
3) search + brand
4) search + store
5) search + size
6) repeat for brand/store etc!!
This can’t be right, surely I don’t have to program every possible combination? I recently discovered params.merge, which looks like at least on the front-end it can help me. I’m a bit of a noob, it makes me think maybe this is where design patterns come in.
Can someone point me in the right direction?
Thanks for all help in advance.
New solution: