Add comment resource
parent
58092ae50e
commit
70cf1bdc3b
@ -0,0 +1,34 @@
|
|||||||
|
class CommentsController < ApplicationController
|
||||||
|
respond_to :json
|
||||||
|
|
||||||
|
before_filter :ensure_authenticated!, :only => [:create, :update, :destroy]
|
||||||
|
before_filter :load_asciicast, :only => [:index, :create]
|
||||||
|
|
||||||
|
def index
|
||||||
|
respond_with @asciicast.comments
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
@comment = Comment.new(params[:comment])
|
||||||
|
@comment.asciicast = @asciicast
|
||||||
|
@comment.user = current_user
|
||||||
|
|
||||||
|
@comment.save
|
||||||
|
|
||||||
|
respond_with @comment
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
respond_with Comment.update(params[:id], params[:comment])
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
respond_with Comment.destroy(params[:id])
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def load_asciicast
|
||||||
|
@asciicast = Asciicast.find(params[:asciicast_id])
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,10 @@
|
|||||||
|
class Comment < ActiveRecord::Base
|
||||||
|
|
||||||
|
validates :body, :presence => true
|
||||||
|
validates :asciicast_id, :presence => true
|
||||||
|
validates :user_id, :presence => true
|
||||||
|
|
||||||
|
belongs_to :user
|
||||||
|
belongs_to :asciicast
|
||||||
|
|
||||||
|
end
|
@ -0,0 +1,14 @@
|
|||||||
|
class CreateComments < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
create_table :comments do |t|
|
||||||
|
t.text :body
|
||||||
|
t.integer :user_id
|
||||||
|
t.integer :asciicast_id
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
|
||||||
|
add_index(:comments, :asciicast_id)
|
||||||
|
add_index(:comments, :user_id)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,71 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe CommentsController do
|
||||||
|
|
||||||
|
let(:user) { mock_model(User) }
|
||||||
|
let(:asciicast) { mock_model(Asciicast) }
|
||||||
|
|
||||||
|
it "should ensure user is authenticated" do
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
Asciicast.stub(:find).and_return(asciicast)
|
||||||
|
login_as(user)
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#create" do
|
||||||
|
|
||||||
|
context "given valid data" do
|
||||||
|
def dispatch
|
||||||
|
post :create,
|
||||||
|
:asciicast_id => asciicast.id,
|
||||||
|
:comment => {"body" => "Foo"},
|
||||||
|
:format => :json
|
||||||
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
Comment.any_instance.should_receive(:save).and_return(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "assigns current_user" do
|
||||||
|
dispatch
|
||||||
|
assigns(:comment).user.should_not be_blank
|
||||||
|
end
|
||||||
|
|
||||||
|
it "assigns asciicast" do
|
||||||
|
dispatch
|
||||||
|
assigns(:comment).asciicast.should_not be_blank
|
||||||
|
end
|
||||||
|
|
||||||
|
it "assigns asciicast" do
|
||||||
|
dispatch
|
||||||
|
response.status.should == 201
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "given not valid data" do
|
||||||
|
def dispatch
|
||||||
|
post :create,
|
||||||
|
:asciicast_id => asciicast.id,
|
||||||
|
:comment => {},
|
||||||
|
:format => :json
|
||||||
|
end
|
||||||
|
|
||||||
|
it "response should be 422" do
|
||||||
|
dispatch
|
||||||
|
response.status.should == 422
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#index" do
|
||||||
|
|
||||||
|
it "return comments" do
|
||||||
|
asciicast.should_receive(:comments).and_return([])
|
||||||
|
get :index, :asciicast_id => asciicast.id
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
@ -0,0 +1,7 @@
|
|||||||
|
FactoryGirl.define do
|
||||||
|
factory :comment do
|
||||||
|
body "My fancy comment"
|
||||||
|
association :user
|
||||||
|
association :asciicast
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,8 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Comment do
|
||||||
|
|
||||||
|
it "factory should be valid" do
|
||||||
|
Factory.build(:comment).should be_valid
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,7 @@
|
|||||||
|
module ControllerMacros
|
||||||
|
|
||||||
|
def login_as(user)
|
||||||
|
controller.stub(:current_user => user)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Loading…
Reference in New Issue