This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the the ability to add comments on repositories
Fixes: #204 Signed-off-by: Jürgen Löhel <[email protected]>
- Loading branch information
Jürgen Löhel
committed
Nov 26, 2015
1 parent
08e1c76
commit 4d780d9
Showing
27 changed files
with
334 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
$(document).on "page:change", -> | ||
|
||
# Shows and hides the comment form | ||
$('#write_comment_repository_btn').unbind('click').on 'click', (event) -> | ||
$('#write_comment_form').toggle 400, "swing", -> | ||
if $('#write_comment_form').is(':visible') | ||
$('#comment_body').focus() | ||
layout_resizer() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,5 @@ | |
@import 'tables'; | ||
@import 'forms'; | ||
@import 'types'; | ||
@import 'comments'; | ||
@import 'responsive-utilities'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#write_comment_repository_btn, .destroy_comments_btn { | ||
padding-top: 0px; | ||
padding-bottom: 0px; | ||
border: 0px; | ||
} | ||
|
||
.comment-thumbnail { | ||
padding: 10px 0px; | ||
img { | ||
border-radius: 100%; | ||
border: 4px solid $gray-light; | ||
width: 40px; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class CommentsController < ApplicationController | ||
before_action :set_repository | ||
respond_to :js, :html | ||
|
||
# POST /repositories/1/comments | ||
# POST /repositories/1/comments.json | ||
def create | ||
@comment = @repository.comments.new(params.require(:comment).permit(:body)) | ||
@comment.author = current_user | ||
authorize @comment | ||
|
||
if @comment.save | ||
respond_with(@comment) | ||
else | ||
respond_with @comment.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# DELETE /repositories/1/comments/1 | ||
# DELETE /repositories/1/comments/1.json | ||
def destroy | ||
@comment = @repository.comments.find(params[:id]) | ||
authorize @comment | ||
@comment.destroy | ||
respond_with @comment | ||
end | ||
|
||
private | ||
|
||
def set_repository | ||
@repository = Repository.find(params[:repository_id]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Comment < ActiveRecord::Base | ||
include PublicActivity::Common | ||
belongs_to :repository | ||
belongs_to :author, class_name: "User", foreign_key: "user_id" | ||
|
||
validates :body, presence: true | ||
|
||
# Returns true if the user is the author of the comment | ||
def author?(user) | ||
user_id == user.id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class CommentPolicy | ||
attr_reader :user, :comment | ||
|
||
def initialize(user, comment) | ||
@user = user | ||
@comment = comment | ||
end | ||
|
||
# Allows the admin to write comments | ||
# Allows all members of a team to comment on their repos | ||
# Allows all users to comment on repos under a public namespace | ||
def create? | ||
@user.admin? || | ||
@comment.repository.namespace.public? || | ||
@comment.repository.namespace.team.team_users.where(user_id: @user.id).any? | ||
end | ||
|
||
# Allows only the admin and author to delete comments | ||
def destroy? | ||
@user.admin? || @comment.author?(@user) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
.row id="comment_#{comment.id}" | ||
.col-md-1.comment-thumbnail | ||
.pull-right | ||
.user-image | ||
= user_image_tag(comment.author.email) | ||
.col-md-11 | ||
.panel.panel-default | ||
.panel-heading | ||
h5 | ||
strong | ||
= comment.author.username | ||
' commented | ||
= activity_time_tag comment.updated_at | ||
' ago | ||
.pull-right | ||
button.btn.btn-link.btn-x.btn-edit-role.destroy_comments_btn[ | ||
data-placement="left" | ||
data-toggle="popover" | ||
data-title="Please confirm" | ||
data-content='<p>Are you sure you want to remove this \ | ||
comment?</p><a class="btn btn-default">No</a> <a class="btn \ | ||
btn-primary" data-method="delete" rel="nofollow" \ | ||
data-remote="true" href="#{url_for([comment.repository, comment])}">Yes</a>' | ||
data-html="true" | ||
role="button"] | ||
if.fa.fa-trash | ||
| Delete comment | ||
.panel-body | ||
= markdown(comment.body) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<% if @comment.errors.any? %> | ||
$('#alert p').html("<%= escape_javascript(@comment.errors.full_messages.join('<br/>')) %>"); | ||
<% else %> | ||
<% if @repository.comments.count == 1 %> | ||
$(".comment_string").text(" Comment"); | ||
$(".container-full").text("") | ||
<% elsif @repository.comments.count == 2 %> | ||
$(".comment_string").text(" Comments"); | ||
<% end %> | ||
$(".number_of_comments").text("<%= escape_javascript(@repository.comments.count.to_s) %>"); | ||
$("<%= escape_javascript(render @comment) %>").appendTo(".container-full"); | ||
$('#alert p').html("New comment posted"); | ||
$('#write_comment_form').fadeOut(); | ||
<% end %> | ||
$('#alert').fadeIn(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<% if @comment.errors.any? %> | ||
$('#alert p').html("<%= escape_javascript(@comment.errors.full_messages.join('<br/>')) %>"); | ||
<% else %> | ||
<% if @repository.comments.count == 0 %> | ||
$(".comment_string").text(" Comments"); | ||
$(".container-full").text("Nobody has left a comment yet."); | ||
<% elsif @repository.comments.count == 1 %> | ||
$(".comment_string").text(" Comment"); | ||
<% end %> | ||
$("#comment_<%= escape_javascript(@comment.id.to_s) %>").remove(); | ||
$(".number_of_comments").text("<%= escape_javascript(@repository.comments.count.to_s) %>"); | ||
$('#alert p').html("Comment successfully deleted"); | ||
<% end %> | ||
$('#alert').fadeIn(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateComments < ActiveRecord::Migration | ||
def change | ||
create_table :comments do |t| | ||
t.string :title | ||
t.text :body | ||
t.references :repository, index: true, foreign_key: true | ||
|
||
t.timestamps null: false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddAuthorToComment < ActiveRecord::Migration | ||
def change | ||
add_belongs_to :comments, :user, index: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Removetitle < ActiveRecord::Migration | ||
def change | ||
remove_column :comments, :title | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
require "rails_helper" | ||
|
||
describe CommentsController, type: :controller do | ||
let(:admin) { create(:admin) } | ||
let(:owner) { create(:user) } | ||
let(:user) { create(:user) } | ||
let!(:team) { create(:team, owners: [owner]) } | ||
let!(:public_namespace) { create(:namespace, public: 1, team: team) } | ||
let!(:visible_repository) { create(:repository, namespace: public_namespace) } | ||
let!(:private_namespace) { create(:namespace, public: 0, team: team) } | ||
let!(:invisible_repository) { create(:repository, namespace: private_namespace) } | ||
let(:comment) { create(:comment, author: owner) } | ||
let!(:commented_repository) do | ||
create(:repository, comments: [comment], namespace: private_namespace) | ||
end | ||
|
||
let(:valid_attributes) do | ||
{ body: "short test comment" } | ||
end | ||
|
||
let(:invalid_attributes) do | ||
{ foo: "not valid" } | ||
end | ||
|
||
describe "PUT #create" do | ||
context "with valid params" do | ||
it "creates a new comment" do | ||
sign_in owner | ||
expect do | ||
repository_id = invisible_repository.id | ||
post :create, repository_id: repository_id, comment: valid_attributes, format: "js" | ||
expect(response.status).to eq 200 | ||
end.to change(Comment, :count).by(1) | ||
expect(assigns(:comment).author.id).to eq owner.id | ||
end | ||
|
||
it "does allow everyone to write comments for a repository under a public namespace" do | ||
sign_in user | ||
post :create, repository_id: visible_repository.id, comment: valid_attributes, format: :js | ||
expect(response.status).to eq 200 | ||
end | ||
|
||
it "does allow the admin to write comments for every repository" do | ||
sign_in admin | ||
post :create, repository_id: invisible_repository.id, comment: valid_attributes, format: :js | ||
expect(response.status).to eq 200 | ||
end | ||
|
||
it "does not allow a user who has no access to the repository to write comments" do | ||
sign_in user | ||
post :create, repository_id: invisible_repository.id, comment: valid_attributes, format: :js | ||
expect(response.status).to eq 401 | ||
end | ||
end | ||
|
||
context "with invalid params" do | ||
it "does not allow invalid parameters" do | ||
sign_in owner | ||
repository_id = invisible_repository.id | ||
post :create, repository_id: repository_id, comment: invalid_attributes, format: :js | ||
expect(assigns(:comment)).to be_a_new(Comment) | ||
expect(response.status).to eq 422 | ||
end | ||
end | ||
end | ||
|
||
describe "DELETE #destroy" do | ||
it "deletes a comment" do | ||
sign_in owner | ||
expect do | ||
delete :destroy, repository_id: commented_repository.id, id: comment.id, format: :js | ||
end.to change(Comment, :count).by(-1) | ||
end | ||
|
||
it "does allow to delete a comment if the user is admin" do | ||
sign_in admin | ||
expect do | ||
delete :destroy, repository_id: commented_repository.id, id: comment.id, format: :js | ||
end.to change(Comment, :count).by(-1) | ||
end | ||
|
||
it "does not allow to delete the comment if the user is not the author" do | ||
sign_in user | ||
expect do | ||
delete :destroy, repository_id: commented_repository.id, id: comment.id, format: :js | ||
end.to change(Comment, :count).by(0) | ||
expect(response.status).to eq 401 | ||
end | ||
end | ||
end |
Oops, something went wrong.