Permalink
Browse files

add simple search to comments list

  • Loading branch information...
ryanb committed Jul 26, 2011
1 parent 952aafb commit fa4fdd872a9a816c8078a2dde6eeb04a63962995
View
@@ -15,5 +15,6 @@ guard 'rspec', :version => 2, :notify => false do
watch(%r{^app/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" } watch(%r{^app/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" } watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/requests/#{m[1]}_request_spec.rb"] } watch(%r{^app/controllers/(.+)_(controller)\.rb}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/requests/#{m[1]}_request_spec.rb"] }
watch(%r{^app/views/(.+)/.+}) { |m| ["spec/requests/#{m[1]}_request_spec.rb"] }
watch('spec/factories.rb') { "spec/models" } watch('spec/factories.rb') { "spec/models" }
end end
@@ -2,7 +2,7 @@ class CommentsController < ApplicationController
load_and_authorize_resource load_and_authorize_resource
def index def index
@comments = @comments.recent.paginate(:page => params[:page], :per_page => 50) @comments = @comments.search(params[:comment_search]).recent.paginate(:page => params[:page], :per_page => 50)
end end
def new def new
View
@@ -9,6 +9,14 @@ class Comment < ActiveRecord::Base
has_paper_trail has_paper_trail
has_ancestry has_ancestry
def self.search(query)
if query.blank?
scoped
else
where("comments.content like ?", "%#{query}%")

This comment has been minimized.

Show comment
Hide comment
@KieranP

KieranP Jul 27, 2011

Would be nice to have it search by title as well.

@KieranP

KieranP Jul 27, 2011

Would be nice to have it search by title as well.

end
end
def request=(request) def request=(request)
self.user_ip = request.remote_ip self.user_ip = request.remote_ip
self.user_agent = request.env['HTTP_USER_AGENT'] self.user_agent = request.env['HTTP_USER_AGENT']
@@ -1,6 +1,12 @@
<% title "Recent Comments" %> <% title "Recent Comments" %>
<div class="content"> <div class="content">
<%= form_tag comments_path, :method => :get do %>
<p>
<%= text_field_tag :comment_search, params[:comment_search] %>
<%= submit_tag "Search Comments", :name => nil %>
</p>
<% end %>
<div id="comments"> <div id="comments">
<%= render @comments %> <%= render @comments %>
<%= will_paginate :previous_label => h("< Previous Page"), :next_label => h("Next Page >") %> <%= will_paginate :previous_label => h("< Previous Page"), :next_label => h("Next Page >") %>
@@ -64,10 +64,16 @@
page.should have_content("Hello world!") page.should have_content("Hello world!")
end end
it "lists recent comments" do it "lists and search recent comments" do
login Factory(:user, :admin => true) login Factory(:user, :admin => true)
comment = Factory(:comment, :content => "Hello world!") Factory(:comment, :content => "Hello world!")
Factory(:comment, :content => "Back to the Future")
visit comments_path visit comments_path
page.should have_content("Hello world!") page.should have_content("Hello world!")
page.should have_content("Back to the Future")
fill_in "comment_search", :with => "Future"
click_on "Search Comments"
page.should_not have_content("Hello world!")
page.should have_content("Back to the Future")
end end
end end

0 comments on commit fa4fdd8

Please sign in to comment.