OSDN Git Service

search thumbs
authornomeu <nomeu@nomeu.org>
Sun, 13 Jun 2010 22:01:29 +0000 (07:01 +0900)
committernomeu <nomeu@nomeu.org>
Sun, 13 Jun 2010 22:01:29 +0000 (07:01 +0900)
app/controllers/thumbs_controller.rb
app/models/thumb.rb
app/views/thumbs/index.html.erb

index 8f2ac7a..cc9e0a9 100644 (file)
@@ -4,7 +4,8 @@ class ThumbsController < ApplicationController
   # 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
index 6fcaff1..b8845c5 100644 (file)
@@ -1,3 +1,31 @@
 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
index e0cad09..a01a93b 100644 (file)
@@ -1,3 +1,10 @@
+<% 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>