Created
August 18, 2008 20:22
-
-
Save mattwynne/6091 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Responsible for mapping a hash of parameters that will typically be POSTed to a controller into a hash that can be sent to find(:all) | |
# containing SQL clauses in :conditions / :order. | |
# This helps us decouple the view / controller layers from any database specific stuff. | |
class Venue::QueryAdapter < Hash | |
def initialize(params) | |
parse params | |
self.merge!(get_find_params) | |
end | |
private | |
def parse(params) | |
@sort_column = params[:sort] | |
@city_id = params[:city_id] | |
@name = params[:name] | |
@page = params[:page] | |
end | |
def get_find_params | |
find_params = {} | |
find_params.merge!( :order => get_order_clause ) if get_order_clause.length > 0 | |
find_params.merge!( :conditions => get_where_clause ) if get_where_clause.length > 0 | |
find_params.merge!( :page => @page ) | |
return find_params | |
end | |
def get_where_clause | |
clause = [] | |
clause << "city_id = #{@city_id}" if @city_id | |
clause << "name like '%#{@name}%'" if @name | |
return clause.join(" AND ") | |
end | |
def get_order_clause | |
clause = [] | |
clause << 'created_on DESC' if @sort_column == 'created_at' | |
clause << 'name ASC' | |
return clause.join(", ") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment