Commit 3c282dd6
Changed files (2)
app
controllers
spec
controllers
app/controllers/search_controller.rb
@@ -1,8 +1,8 @@
class SearchController < ApplicationController
def index
@search = params[:q]
- if @search.blank?
- redirect_to(home_index_url)
+ if @search.blank?
+ redirect_to(root_url)
else
@creations = Creation.includes(:user).search(@search).page(params[:page]).per(100)
@members = User.includes(:avatar).where("upper(name) like upper(?)", "%#{@search}%")
spec/controllers/search_controller_spec.rb
@@ -2,31 +2,40 @@ require 'spec_helper'
describe SearchController do
describe "GET 'index'" do
- let!(:user) { FactoryGirl.create(:user, :name => 'cake') }
- let!(:bob) { FactoryGirl.create(:user, :name => 'bob') }
- let!(:cake) { FactoryGirl.create(:creation, :name => 'cake') }
- let!(:donut) { FactoryGirl.create(:creation, :name => 'donut') }
+ context "when no search query is given" do
+ before { get :index }
- before { get :index, { :q => 'cake' } }
-
- it "should be successful" do
- response.should be_success
+ it "should redirect you to the home page" do
+ response.should redirect_to(root_url)
+ end
end
+ context "when a valid search query is given" do
+ let!(:user) { FactoryGirl.create(:user, :name => 'cake') }
+ let!(:bob) { FactoryGirl.create(:user, :name => 'bob') }
+ let!(:cake) { FactoryGirl.create(:creation, :name => 'cake') }
+ let!(:donut) { FactoryGirl.create(:creation, :name => 'donut') }
- it "should return all creations that have a matching name" do
- assigns(:creations).should include(cake)
- end
+ before { get :index, { :q => 'cake' } }
- it "should not include cakes that do not match" do
- assigns(:creations).should_not include(donut)
- end
+ it "should be successful" do
+ response.should be_success
+ end
- it "should return all makers that match" do
- assigns(:members).should include(user)
- end
+ it "should return all creations that have a matching name" do
+ assigns(:creations).should include(cake)
+ end
+
+ it "should not include cakes that do not match" do
+ assigns(:creations).should_not include(donut)
+ end
+
+ it "should return all makers that match" do
+ assigns(:members).should include(user)
+ end
- it "should not include makers with names that do not match" do
- assigns(:members).should_not include(bob)
+ it "should not include makers with names that do not match" do
+ assigns(:members).should_not include(bob)
+ end
end
end
end