Get rid of the asciicast comments that are not used anymore
parent
20645271a1
commit
c22cb1cdcc
@ -1,46 +0,0 @@
|
||||
class Api::CommentsController < ApplicationController
|
||||
respond_to :json
|
||||
|
||||
before_filter :ensure_authenticated!, :only => [:create, :destroy]
|
||||
before_filter :load_asciicast, :only => [:index, :create]
|
||||
|
||||
def index
|
||||
respond_with CommentDecorator.decorate_collection(@asciicast.comments)
|
||||
end
|
||||
|
||||
def create
|
||||
comment = Comment.new(params[:comment])
|
||||
comment.asciicast = @asciicast
|
||||
comment.user = current_user
|
||||
|
||||
if comment.save
|
||||
notify_via_email(@asciicast.user, comment)
|
||||
end
|
||||
|
||||
decorated_comment = CommentDecorator.new(comment)
|
||||
location = comment.persisted? ? api_comment_url(comment) : nil
|
||||
respond_with decorated_comment, :location => location
|
||||
end
|
||||
|
||||
def destroy
|
||||
comment = Comment.find(params[:id])
|
||||
|
||||
if comment.user == current_user
|
||||
respond_with comment.destroy
|
||||
else
|
||||
raise Forbidden
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_asciicast
|
||||
@asciicast = Asciicast.find(params[:asciicast_id])
|
||||
end
|
||||
|
||||
def notify_via_email(user, comment)
|
||||
if user && user.email.present? && user != comment.user
|
||||
UserMailer.new_comment_email(user, comment).deliver
|
||||
end
|
||||
end
|
||||
end
|
@ -1,22 +0,0 @@
|
||||
class CommentDecorator < ApplicationDecorator
|
||||
|
||||
def created
|
||||
created_at && (h.time_ago_in_words(created_at) + " ago")
|
||||
end
|
||||
|
||||
def as_json(opts = nil)
|
||||
opts ||= {}
|
||||
|
||||
options = {
|
||||
:include => { :user => { :only => [:id, :nickname, :avatar_url] } }
|
||||
}
|
||||
options.merge!(opts)
|
||||
|
||||
data = model.as_json(options)
|
||||
data['processed_body'] = markdown(data['body'])
|
||||
data['created'] = created
|
||||
|
||||
data
|
||||
end
|
||||
|
||||
end
|
@ -1,154 +0,0 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Api::CommentsController do
|
||||
|
||||
let(:user) { FactoryGirl.create(:user) }
|
||||
let(:asciicast) { FactoryGirl.create(:asciicast) }
|
||||
|
||||
before do
|
||||
login_as(user)
|
||||
end
|
||||
|
||||
describe "#create" do
|
||||
before do
|
||||
Asciicast.stub(:find).and_return(asciicast)
|
||||
end
|
||||
|
||||
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 "response status should be 201" do
|
||||
dispatch
|
||||
response.status.should == 201
|
||||
end
|
||||
|
||||
it "notifies asciicast author via email" do
|
||||
@controller.should_receive(:notify_via_email)
|
||||
dispatch
|
||||
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
|
||||
before do
|
||||
Asciicast.stub(:find).and_return(asciicast)
|
||||
end
|
||||
|
||||
it "return comments" do
|
||||
asciicast.should_receive(:comments).and_return([])
|
||||
get :index, :asciicast_id => asciicast.id
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#destroy" do
|
||||
let(:comment) { mock_model(Comment).as_null_object }
|
||||
before do
|
||||
Comment.stub(:find).with("1").and_return(comment)
|
||||
end
|
||||
|
||||
context "when user is creator of comment" do
|
||||
before do
|
||||
comment.stub(:user).and_return(user)
|
||||
end
|
||||
|
||||
it "calls destroy on comment" do
|
||||
comment.should_receive(:destroy)
|
||||
delete :destroy, :id => 1, :format => :json
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "when user is not creator of comment" do
|
||||
let(:other_user) { FactoryGirl.create(:user) }
|
||||
|
||||
before do
|
||||
comment.stub(:user).and_return(other_user)
|
||||
end
|
||||
|
||||
it "doesn't call destroy on comment" do
|
||||
comment.should_not_receive(:destroy)
|
||||
delete :destroy, :id => 1, :format => :json
|
||||
end
|
||||
|
||||
it "responses with 403 when xhr" do
|
||||
xhr :delete, :destroy, :id => 1, :format => :json
|
||||
response.status.should == 403
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
describe '#notify_via_email' do
|
||||
let(:user) { stub_model(User) }
|
||||
let(:comment) { stub_model(Comment) }
|
||||
|
||||
context 'when asciicast author has email' do
|
||||
before do
|
||||
user.email = "jolka@pamietasz.pl"
|
||||
end
|
||||
|
||||
context 'and he is not comment author' do
|
||||
before do
|
||||
comment.user = stub_model(User)
|
||||
end
|
||||
|
||||
it "sends email" do
|
||||
mail = double('mail', :deliver => true)
|
||||
UserMailer.should_receive(:new_comment_email).and_return(mail)
|
||||
@controller.send(:notify_via_email, user, comment)
|
||||
end
|
||||
end
|
||||
|
||||
context 'and he is comment author' do
|
||||
before do
|
||||
comment.user = user
|
||||
end
|
||||
|
||||
it "doesn't send email" do
|
||||
UserMailer.should_not_receive(:new_comment_email)
|
||||
@controller.send(:notify_via_email, user, comment)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when asciicast author has no email' do
|
||||
it "doesn't send email" do
|
||||
UserMailer.should_not_receive(:new_comment_email)
|
||||
@controller.send(:notify_via_email, user, comment)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when asciicast author is unknown (nil)' do
|
||||
let(:user) { nil }
|
||||
|
||||
it "doesn't send email" do
|
||||
UserMailer.should_not_receive(:new_comment_email)
|
||||
@controller.send(:notify_via_email, user, comment)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1,42 +0,0 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe CommentDecorator do
|
||||
let(:decorated_comment) { CommentDecorator.new(comment) }
|
||||
|
||||
describe "#as_json" do
|
||||
let(:comment) { FactoryGirl.build(:comment) }
|
||||
|
||||
it "includes user comment creator properties user" do
|
||||
hash = decorated_comment.as_json
|
||||
hash.should include(:user)
|
||||
hash[:user].should include("nickname")
|
||||
hash[:user].should include("avatar_url")
|
||||
hash[:user].should include("id")
|
||||
end
|
||||
|
||||
it "should include comment.created" do
|
||||
decorated_comment.as_json.should include 'created'
|
||||
end
|
||||
end
|
||||
|
||||
describe "#created" do
|
||||
let(:comment) { stub_model(Comment) }
|
||||
|
||||
context "when created_at present" do
|
||||
before { comment.created_at = Time.now }
|
||||
|
||||
it "returns string" do
|
||||
decorated_comment.created.should be_kind_of(String)
|
||||
end
|
||||
end
|
||||
|
||||
context "no created_at" do
|
||||
before { comment.created_at = nil }
|
||||
|
||||
it "returns string" do
|
||||
decorated_comment.created.should be_nil
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue