Add comment resource

openid
Micha Wrobel 13 years ago
parent 58092ae50e
commit 70cf1bdc3b

@ -4,6 +4,8 @@ class ApplicationController < ActionController::Base
protect_from_forgery
rescue_from(ActiveRecord::RecordNotFound) { render 'exceptions/not_found' }
class Unauthorized < Exception; end
helper_method :current_user
def current_user
@ -20,4 +22,10 @@ class ApplicationController < ActionController::Base
end
end
private
def ensure_authenticated!
raise Unauthorized unless current_user
end
end

@ -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

@ -1,9 +1,15 @@
AsciiIo::Application.routes.draw do
resources :comments
resources :asciicasts
match ':id' => 'asciicasts#show', :constraints => { :id => /\d+/ }
namespace :api do
resources :asciicasts
resources :asciicasts do
resources :comments
end
end
match "/auth/:provider/callback" => "sessions#create"

@ -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

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120226184448) do
ActiveRecord::Schema.define(:version => 20120227230718) do
create_table "asciicasts", :force => true do |t|
t.integer "user_id"
@ -36,6 +36,17 @@ ActiveRecord::Schema.define(:version => 20120226184448) do
add_index "asciicasts", ["recorded_at"], :name => "index_asciicasts_on_recorded_at"
add_index "asciicasts", ["user_id"], :name => "index_asciicasts_on_user_id"
create_table "comments", :force => true do |t|
t.text "body"
t.integer "user_id"
t.integer "asciicast_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "comments", ["asciicast_id"], :name => "index_comments_on_asciicast_id"
add_index "comments", ["user_id"], :name => "index_comments_on_user_id"
create_table "users", :force => true do |t|
t.string "provider", :null => false
t.string "uid", :null => false

@ -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

@ -3,6 +3,8 @@
FactoryGirl.define do
factory :asciicast do
user_id 1
stdout "MyString"
stdout_timing "MyString"
title "MyString"
duration 1
recorded_at "2011-11-23 22:06:07"

@ -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

@ -11,6 +11,7 @@ RSpec.configure do |config|
config.mock_with :rspec
config.use_transactional_fixtures = true
config.include(ControllerMacros, :type => :controller)
config.infer_base_class_for_anonymous_controllers = false
end

@ -0,0 +1,7 @@
module ControllerMacros
def login_as(user)
controller.stub(:current_user => user)
end
end
Loading…
Cancel
Save