All pastes #2061742 Raw Edit

Something

public text v1 · immutable
#2061742 ·published 2011-05-16 18:32 UTC
rendered paste body
class SearchController < ApplicationController
  
  def show
    
    if params[:f]?
       
       duration = params[:f]['duration'].to_i*60
       views    = params[:f]['views'].to_i
       title    = params[:f]['title'].to_s
       added    = params[:f]['added'].to_i

       if params['f']['categories']? # user has selected some categories

         categories_condition = 'AND ( false'

         params[:f]['categories'] = Array.new

         params['f']['categories'].each do |cc|
           categories_condition = categories_condition + ' OR categories.id = \'' + cc.to_i.to_s + '\''
         end

         categories_condition = categories_condition + ")"

       else
         categories_condition = ''
       end

       @videos = Video.find(:all, :order => sort_query, :include => 'categories',
         :conditions => ["title like ? AND duration >= ? AND views >= ? AND videos.created_at BETWEEN ? AND NOW() #{categories_condition}", 
                        "%#{params[:f]['search_term']}%", 
                        duration, 
                        views,
                        (Date::today-(added)).to_s.gsub(/-/, '').to_i]).paginate(:page => params[:page], :per_page => 36)

       
    else
      params['f'] = {:rating => nil}
      @videos = Video.find(:all, :order => sort_query, :conditions => ['title like ?', "%#{params[:id]}%"]).paginate(:page => params[:page], :per_page => 36)
    end
    
    if @videos.count.to_i == 0
      flash.now[:error] = "Sorry, your search query returned nothing. Please check and try again!"
    else
      flash.now[:success] = "We found results. Happy fappin!"
    end
    
    render 'index'
  end
  
end