# GET /thumbs
# GET /thumbs.xml
def index
- @thumbs = Thumb.paginate(:page => params[:page])
+ @search = Thumb::Search.new(params[:search])
+ @thumbs = Thumb.paginate(@search.find_options.merge(:page => params[:page]))
respond_to do |format|
format.html # index.html.erb
class Thumb < ActiveRecord::Base
set_primary_key :video_id
+
+ class Search
+ attr_accessor :text
+
+ def initialize(attributes)
+ attributes.each do |name, value|
+ send("#{name}=", value)
+ end if attributes
+ end
+
+ def conditions
+ @conditions ||= begin
+ sql = "TRUE"
+ ret = [ sql ]
+ terms = []
+ unless text.blank?
+ sql.concat " and (title like ? or description like ?)"
+ ret.push "%#{text}%"
+ ret.push "%#{text}%"
+ end
+ ret
+ end
+ end
+
+ def find_options
+ { :conditions => conditions }
+ end
+ end
end
+<% form_for(:search, @search, :url => thumbs_path, :html => { :method => :get }) do |f| %>
+ <p>
+ <%= _'Thumb|Title' %> or <%= _'Thumb|Description' %><br />
+ <%= f.text_field :text %>
+ <%= f.submit _("Search") %>
+ </p>
+<% end %>
<p>
Total <%=h @thumbs.total_entries %>
</p>