Created
October 21, 2010 19:29
-
-
Save jeantil/639144 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EventsController < ApplicationController | |
rescue_from(ActionController::RoutingError) {redirect_to '/'} | |
def index | |
render :layout=>"layouts/home" | |
end | |
def show | |
if params[:name] | |
@event=Event.where(:name=>params[:name]).first | |
@event||=Event.create(:name=>params[:name]) | |
end | |
@event||=Event.where(:id=>params[:id]).first | |
respond_to do |format| | |
if @event | |
format.html | |
else | |
format.html {redirect_to new_event_path} | |
end | |
end | |
end | |
def create | |
@event=Event.new(params[:event]) | |
if(@event.save) | |
@participant=@event.participants.new() | |
redirect_to @event | |
else | |
render :new | |
end | |
end | |
def new | |
@event=Event.new | |
end | |
def search | |
@email=params[:search].downcase | |
@events=Event.select("DISTINCT events.*").joins(:participants).where(:participants=>{:email=>@email}).to_a if @email | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
require 'factories/event' | |
class EventsControllerTest < ActionController::TestCase | |
context "A get on :index" do | |
should "display a home page" do | |
get :index | |
assert_response :success | |
assert_template :index, :layout=>"layouts/home" | |
end | |
end | |
context "A get on :show" do | |
context "for an existing event" do | |
setup do | |
@event=Factory(:event, :name=>'Event1') | |
end | |
should "allow to view the event by id" do | |
get(:show, {:id=>@event.id} ) | |
assert_response :success | |
assert_not_nil assigns(:event) | |
assert_template 'show' | |
end | |
# should "allow to view the event by name" do | |
# get(:show, {:name=>'Event1'} ) | |
# assert_response :success | |
# assert_equal @event,assigns(:event) | |
# end | |
end #by name or id | |
context "for a new event by id" do | |
should "redirect to a new event form" do | |
get(:show, {:id=>-1} ) | |
assert_response :redirect | |
assert_redirected_to(new_event_path) | |
end | |
end #fail by id | |
# context "for a new event by name" do | |
# should "create a new event and show it when no event is found with this name" do | |
# get(:show, {:name=>'toto'} ) | |
# assert_response :success | |
# assert_template 'show' | |
# assert_not_nil assigns(:event) | |
# end | |
# end #fail by name | |
end #:show | |
context "A get on :new" do | |
should "display the new event form" do | |
get(:new) | |
assert_response :success | |
assert_template 'new' | |
assert_not_nil assigns(:event) | |
end | |
end #:new | |
context "A post on :create" do | |
context "with a name and a date" do | |
should "create a new event" do | |
assert_difference('Event.count') do | |
post(:create,{:event=>{:name=>"toto", "date(1i)"=>"2010", "date(2i)"=>"8", "date(3i)"=>"8", "date(4i)"=>"09", "date(5i)"=>"47"}}) | |
end | |
assert_response :redirect | |
assert_redirected_to event_path(assigns(:event).id) | |
assert_not_nil assigns(:event) | |
assert_equal 'toto', assigns(:event).name | |
end | |
end | |
end #:create | |
context "A get on :search" do | |
setup do | |
@event=Factory(:event) | |
@participant=Factory(:participant, :event=>@event, :email=>"foo@example.com") | |
@event.participants << @participant | |
end | |
should "return a list of events which have a participant with this email" do | |
get :search, {:search=>"foo@example.com"} | |
assert_response :success | |
assert_not_nil assigns(:events) | |
assert !assigns(:events).empty?, "1 event should have been found !" | |
end | |
should "return a list of unique events which have a participant with this email" do | |
@participant=Factory(:participant, :event=>@event, :email=>"foo@example.com") | |
@event.participants << @participant | |
get :search, {:search=>"foo@example.com"} | |
assert_response :success | |
assert_not_nil assigns(:events) | |
assert !assigns(:events).empty?, "1 event should have been found !" | |
assert_equal 1,assigns(:events).count | |
end | |
context "with an mixed or upper cse mail" do | |
setup do | |
@event=Factory(:event) | |
@participant=Factory(:participant, :event=>@event, :email=>"UPCASE@EXAMPLE.com") | |
@event.participants << @participant | |
end | |
should "return a list of events which have a participant with this email in lowercase" do | |
get :search, {:search=>"UPCASE@EXAMPLE.com"} | |
assert_response :success | |
assert_not_nil assigns(:events) | |
assert !assigns(:events).empty?, "1 event should have been found !" | |
assert assigns(:events).include?(@event), "The event which was found is not the expected event!" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment