For work, my colleague and I sketched this method up as a brain dump of how we want this method to work. What I am trying to figure out is how can we break this method into smaller components so that it is easier to test to write tests for this. I am trying to avoid putting this into methods where they are coupled by parameters.
def self.for_venue(venue, options = {})
filter = options[:filter]
date_conditions = case options[:date]
when 'upcoming' then 'e.datetime > :datetime'
when 'past' then 'e.datetime < :datetime'
end
sql_query = [
"SELECT #{search_fields}",
"FROM events e #{search_joins}",
"WHERE " + [date_conditions, "e.venue_id = #{venue.id}", "e.deleted = 0"].reject(&:nil?).join(" AND ")
]
if options[:limit].present?
sql_query << "LIMIT #{options[:limit]}"
sql_query << "OFFSET #{options[:offset]}" if options[:offset].present?
end
sql_query << "ORDER BY #{options[:order]}" if options[:order].present?
sql = ActiveRecord::Base.prepare_sql([sql_query.join(" "), { :datetime => Time.now.beginning_of_day }])
results = SlaveDB.connection.select_all(sql)
filter ? filter.select(results) : results
end