Skip to content

Commit

Permalink
Merge pull request #2722 from lethliel/comment_reply_function
Browse files Browse the repository at this point in the history
[api] reply to comment function
  • Loading branch information
hennevogel committed Mar 6, 2017
2 parents bf349cb + dba51b5 commit b0485f1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
6 changes: 3 additions & 3 deletions docs/api/api/api.txt
Expand Up @@ -1015,9 +1015,9 @@ GET /comments/package/:project/:pname

XmlResult: directory

POST /comments/request/:id
POST /comments/project/:name
POST /comments/package/:project/:pname
POST /comments/request/:id?parent_id=<parent_comment_id>
POST /comments/project/:name?parent_id=<parent_comment_id>
POST /comments/package/:project/:pname?parent_id=<parent_comment_id>

Create a comment for the object

Expand Down
2 changes: 1 addition & 1 deletion src/api/app/controllers/comments_controller.rb
Expand Up @@ -7,7 +7,7 @@ def show_comments
end

def create
@obj.comments.create!(body: request.raw_post, user: User.current)
@obj.comments.create!(body: request.raw_post, user: User.current, parent_id: params[:parent_id])
render_ok
end

Expand Down
8 changes: 8 additions & 0 deletions src/api/app/models/comment.rb
Expand Up @@ -7,6 +7,8 @@ class Comment < ApplicationRecord

validates :body, :commentable, :user, presence: true

validate :validate_parent_id

after_create :create_notification
after_destroy :delete_parent_if_unused

Expand Down Expand Up @@ -88,4 +90,10 @@ def destroy
def delete_parent_if_unused
parent.destroy if parent && parent.user == User.find_nobody! && parent.children.length.zero?
end

def validate_parent_id
return unless parent_id
return if commentable.comments.where(id: parent_id).present?
errors.add(:parent, "belongs to different object")
end
end
10 changes: 7 additions & 3 deletions src/api/spec/models/comment_spec.rb
Expand Up @@ -2,9 +2,9 @@

RSpec.describe Comment do
let(:comment_package) { create(:comment_package) }
let(:comment_package_with_parent) { create(:comment_package, parent: comment_package) }
let(:comment_package_with_parent_2) { create(:comment_package, parent: comment_package) }
let(:comment_package_with_grandparent) { create(:comment_package, parent: comment_package_with_parent) }
let(:comment_package_with_parent) { create(:comment_package, parent: comment_package, commentable: comment_package.commentable) }
let(:comment_package_with_parent_2) { create(:comment_package, parent: comment_package, commentable: comment_package.commentable) }
let(:comment_package_with_grandparent) { create(:comment_package, parent: comment_package_with_parent, commentable: comment_package.commentable) }

describe "has a valid Factory" do
it { expect(comment_package).to be_valid }
Expand All @@ -21,6 +21,10 @@
it { is_expected.to validate_presence_of(:body) }
it { is_expected.to validate_presence_of(:commentable) }
it { is_expected.to validate_presence_of(:user) }
it {
expect { create(:comment_package, parent: comment_package) }.to raise_error(
ActiveRecord::RecordInvalid, "Validation failed: Parent belongs to different object")
}
end

describe "to_xml" do
Expand Down

0 comments on commit b0485f1

Please sign in to comment.