Created
August 1, 2009 22:26
-
-
Save joshsusser/159824 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
<h1>Listing topics</h1> | |
<table> | |
<tr> | |
<th> </th> | |
<th>Votes</th> | |
<th>Title</th> | |
<th>Description</th> | |
</tr> | |
<% @topics.each do |topic| %> | |
<tr id="topic_<%= topic.id %>"> | |
<td><%= button_to "+1", topic_votes_path(topic) %></td> | |
<td><%= topic.votes.length %></td> | |
<td><%=h topic.title %></td> | |
<td><%=h topic.description %></td> | |
<td><%= link_to 'Show', topic %></td> | |
<td><%= link_to 'Edit', edit_topic_path(topic) %></td> | |
<td><%= link_to 'Destroy', topic, :confirm => 'Are you sure?', :method => :delete %></td> | |
</tr> | |
<% end %> | |
</table> | |
<br /> | |
<%= link_to 'New topic', new_topic_path %> |
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
ActionController::Routing::Routes.draw do |map| | |
map.resources :topics, :has_many => :votes | |
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
class TopicsController < ApplicationController | |
# GET /topics | |
# GET /topics.xml | |
def index | |
@topics = Topic.find(:all, | |
:include => :votes).sort_by {|t| -t.votes.length} | |
respond_to do |format| | |
format.html # index.html.erb | |
format.xml { render :xml => @topics } | |
end | |
end | |
# GET /topics/1 | |
# GET /topics/1.xml | |
def show | |
@topic = Topic.find(params[:id]) | |
respond_to do |format| | |
format.html # show.html.erb | |
format.xml { render :xml => @topic } | |
end | |
end | |
# GET /topics/new | |
# GET /topics/new.xml | |
def new | |
@topic = Topic.new | |
respond_to do |format| | |
format.html # new.html.erb | |
format.xml { render :xml => @topic } | |
end | |
end | |
# GET /topics/1/edit | |
def edit | |
@topic = Topic.find(params[:id]) | |
end | |
# POST /topics | |
# POST /topics.xml | |
def create | |
@topic = Topic.new(params[:topic]) | |
respond_to do |format| | |
if @topic.save | |
flash[:notice] = 'Topic was successfully created.' | |
format.html { redirect_to(@topic) } | |
format.xml { render :xml => @topic, :status => :created, :location => @topic } | |
else | |
format.html { render :action => "new" } | |
format.xml { render :xml => @topic.errors, :status => :unprocessable_entity } | |
end | |
end | |
end | |
# PUT /topics/1 | |
# PUT /topics/1.xml | |
def update | |
@topic = Topic.find(params[:id]) | |
respond_to do |format| | |
if @topic.update_attributes(params[:topic]) | |
flash[:notice] = 'Topic was successfully updated.' | |
format.html { redirect_to(@topic) } | |
format.xml { head :ok } | |
else | |
format.html { render :action => "edit" } | |
format.xml { render :xml => @topic.errors, :status => :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /topics/1 | |
# DELETE /topics/1.xml | |
def destroy | |
@topic = Topic.find(params[:id]) | |
@topic.destroy | |
respond_to do |format| | |
format.html { redirect_to(topics_url) } | |
format.xml { head :ok } | |
end | |
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' | |
class TopicsControllerTest < ActionController::TestCase | |
test "should get index" do | |
get :index | |
assert_response :success | |
assert_not_nil assigns(:topics) | |
end | |
test "index should show vote count" do | |
Topic.create!(:title => "test topic 1", | |
:description => "very cool") | |
Topic.create!(:title => "test topic 2", | |
:description => "also cool") | |
get :index | |
assert_response :success | |
topic1 = assigns(:topics).first | |
assert_equal "test topic 1", topic1.title | |
assert_select "tr#topic_#{topic1.id} td", topic1.votes.count.to_s | |
end | |
test "index should list topics with most votes first" do | |
t1 = Topic.create!(:title => "test topic 1", :description => "very cool") | |
t2 = Topic.create!(:title => "test topic 2", :description => "also cool") | |
t3 = Topic.create!(:title => "test topic 3", :description => "also cool") | |
5.times { t1.votes.create } | |
7.times { t2.votes.create } | |
3.times { t3.votes.create } | |
get :index | |
assert_response :success | |
assert_equal [t2, t1, t3], assigns(:topics) | |
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
class VotesController < ApplicationController | |
def create | |
topic = Topic.find(params[:topic_id]) | |
topic.votes.create! | |
redirect_to topics_url | |
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' | |
class VotesControllerTest < ActionController::TestCase | |
# Replace this with your real tests. | |
test "creating should add to the topic vote count" do | |
t = Topic.create!(:title => "test topic 1", | |
:description => "very cool") | |
total_votes = Vote.count | |
topic_votes = t.votes.count | |
post :create, :topic_id => t.to_param | |
assert_equal total_votes + 1, Vote.count | |
assert_equal topic_votes + 1, t.reload.votes.count | |
assert_redirected_to topics_url | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment