joshsusser (owner)

Revisions

gist: 159824 Download_button fork
public
Public Clone URL: git://gist.github.com/159824.git
Embed All Files: show embed
index.html.erb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<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 %>
routes.rb #
1
2
3
4
5
6
7
ActionController::Routing::Routes.draw do |map|
 
  map.resources :topics, :has_many => :votes
 
end
 
 
topics_controller.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
 
 
topics_controller_test.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
 
 
votes_controller.rb #
1
2
3
4
5
6
7
8
9
10
class VotesController < ApplicationController
  def create
    topic = Topic.find(params[:topic_id])
    topic.votes.create!
    
    redirect_to topics_url
  end
end
 
 
votes_controller_test.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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