Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added Comment entity & route fixes

  • Loading branch information...
commit 17b8848179f692854aa1981905de06865e3d25e7 1 parent cbb7aea
@av-ast authored
View
3  app/assets/javascripts/comments.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
View
3  app/assets/stylesheets/comments.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the Comments controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
16 app/controllers/comments_controller.rb
@@ -0,0 +1,16 @@
+class CommentsController < ApplicationController
+ # POST /comments
+ # POST /comments.json
+ def create
+ @comment = Comment.new(params[:comment]).save
+ redirect_to :back
+ end
+
+ # DELETE /comments/1
+ # DELETE /comments/1.json
+ def destroy
+ @comment = Comment.find(params[:id])
+ @comment.destroy
+ redirect_to :back
+ end
+end
View
2  app/controllers/stories_controller.rb
@@ -24,6 +24,8 @@ def index
# GET /stories/1.json
def show
@story = Story.find(params[:id])
+ @comment = Comment.new
+ @comments = Comment.where(:story_id => params[:id])
respond_to do |format|
format.html # show.html.erb
View
2  app/helpers/comments_helper.rb
@@ -0,0 +1,2 @@
+module CommentsHelper
+end
View
7 app/models/comment.rb
@@ -0,0 +1,7 @@
+class Comment < ActiveRecord::Base
+ belongs_to :user
+ belongs_to :story
+
+ validates :user_id, :story_id, :text, :presence => true
+ validates :text, :length => { :maximum => 1000 }
+end
View
1  app/models/story.rb
@@ -1,5 +1,6 @@
class Story < ActiveRecord::Base
belongs_to :user
+ has_many :comments, :dependent => :destroy
scope :filter, lambda{ |params| { :conditions => params } }
View
1  app/models/user.rb
@@ -1,5 +1,6 @@
class User < ActiveRecord::Base
has_many :stories, :dependent => :destroy
+ has_many :comments, :dependent => :destroy
attr_accessible :email, :name, :role, :password, :password_confirmation
has_secure_password
View
19 app/views/stories/index.html.erb
@@ -1,6 +1,6 @@
<h1>Listing stories</h1>
-<%= form_for :filter, :method => :get do |f| %>
+<%= form_for :filter do |f| %>
<table>
<tr>
<td>
@@ -12,8 +12,7 @@
{:include_blank => '- Select story state -'} %>
</td>
<td>
- <button type="submit" class="btn">Filter</button>
- <!--%= f.submit "Filter" %-->
+ <%= f.submit "Filter" %>
</td>
</tr>
</table>
@@ -24,21 +23,23 @@
<th>Id</th>
<th>User</th>
<th>Title</th>
+ <th>Comments</th>
<th>Content</th>
<th>State</th>
<th></th>
+ <th></th>
</tr>
<% @stories.each_with_index do |story,i| %>
<tr>
- <td><%= story.id %></td>
- <td><%= story.user.name %> &lt;<%= story.user.email %>&gt;</td>
- <td><%= story.title %></td>
+ <td><%= link_to story.id, story %></td>
+ <td><%= link_to story.user.name || story.user.email, story.user %></td>
+ <td><%= link_to story.title, story_path(story) %></td>
+ <td><%= story.comments.size %></td>
<td><%= truncate(story.content, :length => 50, :omission => '...') %></td>
<td><%= story.state %></td>
- <td><%= link_to 'Show', story %>
- <%= link_to 'Edit', edit_story_path(story) %>
- <%= link_to 'Destroy', story, :confirm => 'Are you sure?', :method => :delete %></td>
+ <td><%= link_to 'Edit', edit_story_path(story) %></td>
+ <td><%= link_to 'Destroy', story, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
View
23 app/views/stories/show.html.erb
@@ -11,15 +11,30 @@
</p>
<p>
- <b>Content:</b>
- <%= @story.content %>
+ <b>State:</b>
+ <%= @story.state %>
</p>
<p>
- <b>State:</b>
- <%= @story.state %>
+ <b>Content:</b>
+ <%= @story.content %>
</p>
+<h2>Post a comment</h2>
+<%= form_for @comment do |f| %>
+ <input type="hidden" name=comment[user_id] value="<%= @current_user.id %>">
+ <input type="hidden" name=comment[story_id] value="<%= @story.id %>">
+ <%= f.text_area :text, :rows => 5 %>
+ <br />
+ <%= f.submit "Post" %>
+<% end %>
+
+<% @comments.each do |c| %>
+ <div>
+ <div><%= c.created_at.strftime("%d %b %H:%M")%>&nbsp;<%= link_to c.user.name || c.user.email, c.user %><% if c.user == @current_user || current_user.role == "admin" %>&nbsp;<%= link_to 'Destroy', c, :confirm => 'Are you sure?', :method => :delete %><% end %></div>
+ <div><%= c.text%></div>
+ </div>
+<% end %>
<%= link_to 'Edit', edit_story_path(@story) %> |
<%= link_to 'Back', stories_path %>
View
12 app/views/users/index.html.erb
@@ -2,20 +2,22 @@
<table border="1" >
<tr>
- <th>Email</th>
<th>Name</th>
+ <th>Email</th>
<th>Role</th>
- <th></th>
+ <th>Stories</th>
+ <th>Comments</th>
<th></th>
<th></th>
</tr>
<% @users.each do |user| %>
<tr>
- <td><%= user.email %></td>
- <td><%= user.name %></td>
+ <td><%= link_to user.name, user %></td>
+ <td><%= link_to user.email, user %></td>
<td><%= user.role %></td>
- <td><%= link_to 'Show', user %></td>
+ <td><%= user.stories.count %></td>
+ <td><%= user.comments.count %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
View
13 config/routes.rb
@@ -1,11 +1,24 @@
+class FilterRecognizer
+ def initialize(query_param)
+ @query_param = query_param.to_sym
+ end
+
+ def matches?(request)
+ request.params.has_key?(@query_param)
+ end
+end
+
SimpleTaskManager::Application.routes.draw do
get "login" => "sessions#new", :as => "login"
get "logout" => "sessions#destroy", :as => "logout"
+ post "stories" => "stories#index", :constraints => FilterRecognizer.new(:filter)
+
resources :users
resources :sessions
resources :stories
+ resources :comments
# The priority is based upon order of creation:
# first created -> highest priority.
View
11 db/migrate/20120225105504_create_comments.rb
@@ -0,0 +1,11 @@
+class CreateComments < ActiveRecord::Migration
+ def change
+ create_table :comments do |t|
+ t.integer :user_id
+ t.integer :story_id
+ t.text :text
+
+ t.timestamps
+ end
+ end
+end
View
10 db/schema.rb
@@ -10,7 +10,15 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120224105930) do
+ActiveRecord::Schema.define(:version => 20120225105504) do
+
+ create_table "comments", :force => true do |t|
+ t.integer "user_id"
+ t.integer "story_id"
+ t.text "text"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
create_table "stories", :force => true do |t|
t.integer "user_id"
View
11 test/fixtures/comments.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ user_id: 1
+ story_id: 1
+ text: MyText
+
+two:
+ user_id: 1
+ story_id: 1
+ text: MyText
View
49 test/functional/comments_controller_test.rb
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class CommentsControllerTest < ActionController::TestCase
+ setup do
+ @comment = comments(:one)
+ end
+
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:comments)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create comment" do
+ assert_difference('Comment.count') do
+ post :create, :comment => @comment.attributes
+ end
+
+ assert_redirected_to comment_path(assigns(:comment))
+ end
+
+ test "should show comment" do
+ get :show, :id => @comment
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, :id => @comment
+ assert_response :success
+ end
+
+ test "should update comment" do
+ put :update, :id => @comment, :comment => @comment.attributes
+ assert_redirected_to comment_path(assigns(:comment))
+ end
+
+ test "should destroy comment" do
+ assert_difference('Comment.count', -1) do
+ delete :destroy, :id => @comment
+ end
+
+ assert_redirected_to comments_path
+ end
+end
View
7 test/unit/comment_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class CommentTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
4 test/unit/helpers/comments_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class CommentsHelperTest < ActionView::TestCase
+end
Please sign in to comment.
Something went wrong with that request. Please try again.