Skip to content

Commit

Permalink
[webui][api] Fix deletion of commented packages/projects
Browse files Browse the repository at this point in the history
  • Loading branch information
hennevogel committed Feb 14, 2014
1 parent f518ab8 commit ef4e219
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/api/app/controllers/comments_controller.rb
Expand Up @@ -47,7 +47,7 @@ def delete_comment
unless comment.check_delete_permissions
raise NoPermission.new "No permission to delete #{params[:id]}"
end
comment.destroy
comment.blank_or_destroy
render_ok
end

Expand Down
3 changes: 2 additions & 1 deletion src/api/app/controllers/webui/comments_controller.rb
Expand Up @@ -7,7 +7,8 @@ def destroy
redirect_to :back
return
end
comment.destroy
comment.blank_or_destroy


respond_to do |format|
format.js { render json: 'ok' }
Expand Down
12 changes: 9 additions & 3 deletions src/api/app/models/comment.rb
Expand Up @@ -11,7 +11,8 @@ class Comment < ActiveRecord::Base

after_create :create_notification

has_many :children, :class_name => 'Comment', :foreign_key => 'parent_id'
has_many :children, dependent: :destroy, :class_name => 'Comment', :foreign_key => 'parent_id'


def create_notification(params = {})
params[:commenter] = self.user.id
Expand Down Expand Up @@ -57,13 +58,18 @@ def to_xml(builder)
end
end

def destroy
def blank_or_destroy
if self.children.exists?
self.body = 'This comment has been deleted'
self.user = User.find_by_login '_nobody_'
self.save!
else
super
self.destroy
end
end

# FIXME: This is to work around https://github.com/rails/rails/pull/12450/files
def destroy
super
end
end
2 changes: 1 addition & 1 deletion src/api/app/models/package.rb
Expand Up @@ -59,7 +59,7 @@ class ReadSourceAccessError < APIException
has_many :products, :dependent => :destroy
has_many :channels, :dependent => :destroy, foreign_key: :package_id

has_many :comments, :dependent => :delete_all, inverse_of: :package
has_many :comments, :dependent => :destroy, inverse_of: :package

before_destroy :delete_cache_lines
before_destroy :remove_linked_packages
Expand Down
2 changes: 1 addition & 1 deletion src/api/app/models/project.rb
Expand Up @@ -65,7 +65,7 @@ class ForbiddenError < APIException
has_many :develprojects, :class_name => 'Project', :foreign_key => 'develproject_id'
belongs_to :develproject, :class_name => 'Project'

has_many :comments, :dependent => :delete_all, inverse_of: :project
has_many :comments, :dependent => :destroy, inverse_of: :project

has_many :project_log_entries, :dependent => :delete_all

Expand Down
2 changes: 1 addition & 1 deletion src/api/app/views/webui/project/_delete_dialog.html.erb
Expand Up @@ -10,7 +10,7 @@
<p>Other projects still have source links to this project:</p>
<ul>
<% @linking_projects.each do |link| %>
<li><%= link_to(elide(link.name, 40), :controller => 'project', :action => 'show', :project => link) %></li>
<li><%= link_to(elide(link, 40), :controller => 'project', :action => 'show', :project => link) %></li>
<% end %>
</ul>
<%= check_box_tag(:ignore_linking_projects)%> Ignore project source links
Expand Down
52 changes: 52 additions & 0 deletions src/api/test/fixtures/comments.yml
Expand Up @@ -85,3 +85,55 @@ request_comment_reply_2:
created_at: 2013-07-25 18:00:21.000000000 Z
updated_at: 2014-01-13 11:07:05.000000000 Z
user: adrian
king_package_comment_parent:
id: 400
body: This is the start of a comment thread on a package
type: CommentPackage
created_at: 2014-02-14 12:00:00.000000000 Z
updated_at: 2014-02-14 12:00:04.000000000 Z
user: Admin
package: home_king_commentpack
king_package_comment_reply_1:
id: 401
body: This is the first reply
parent_id: 400
type: CommentPackage
created_at: 2014-02-14 12:01:00.000000000 Z
updated_at: 2014-02-14 12:01:04.000000000 Z
user: tom
package: home_king_commentpack
king_package_comment_reply_2:
id: 402
body: This is a reply to the first reply
parent_id: 401
type: CommentPackage
created_at: 2014-02-14 12:02:00.000000000 Z
updated_at: 2014-02-14 12:02:04.000000000 Z
user: adrian
package: home_king_commentpack
king_project_comment_parent:
id: 500
body: This is the start of a comment thread on a project
type: CommentProject
created_at: 2014-02-14 12:00:00.000000000 Z
updated_at: 2014-02-14 12:00:04.000000000 Z
user: Admin
project: home_king
king_project_comment_reply_1:
id: 501
body: This is the first reply
parent_id: 500
type: CommentProject
created_at: 2014-02-14 12:01:00.000000000 Z
updated_at: 2014-02-14 12:01:04.000000000 Z
user: tom
project: home_king
king_project_comment_reply_2:
id: 502
body: This is a reply to the first reply
parent_id: 501
type: CommentProject
created_at: 2014-02-14 12:02:00.000000000 Z
updated_at: 2014-02-14 12:02:04.000000000 Z
user: adrian
project: home_king
10 changes: 10 additions & 0 deletions src/api/test/fixtures/packages.yml
Expand Up @@ -326,6 +326,16 @@ home_dmayr_x11vnc:
activity_index: 100.0
delta: 1
project: home_dmayr
home_king_commentpack:
name: commentpack
title: commentpack
description: description
created_at: 2014-02-14 01:00:02.000000000 Z
updated_at: 2014-02-14 12:48:59.000000000 Z
update_counter: 0
activity_index: 100.0
delta: 1
project: home_king
kde4_kdebase:
name: kdebase
title: blub
Expand Down
8 changes: 8 additions & 0 deletions src/api/test/fixtures/projects.yml
Expand Up @@ -247,6 +247,14 @@ home_tom:
updated_at: 2012-11-29 21:18:02.000000000 Z
type_id: 1
delta: 1
home_king:
name: home:king
title: test_delete_commented
description: ''
created_at: 2014-02-14 01:00:01.000000000 Z
updated_at: 2014-02-14 01:00:01.000000000 Z
type_id: 1
delta: 1
kde:
name: kde
title: blub
Expand Down
14 changes: 14 additions & 0 deletions src/api/test/functional/comments_controller_test.rb
Expand Up @@ -53,6 +53,20 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest

end

test 'delete commented project' do
# BaseDistro has comments
login_king
delete '/source/home:king'
assert_response :success
end

test 'delete commented package' do
# BaseDistro3/pack2 has comments
login_king
delete '/source/home:king/commentpack'
assert_response :success
end

test 'create request comment' do
post create_request_comment_path(id: 2)
assert_response 401 # no anonymous comments
Expand Down
2 changes: 1 addition & 1 deletion src/api/test/functional/statistics_controller_test.rb
Expand Up @@ -62,7 +62,7 @@ def test_latest_updated
assert_response :success
assert_xml_tag :tag => 'latest_updated', :child => { :tag => 'project' }
assert_xml_tag :tag => 'project', :attributes => {
:name => "kde4",
:name => "home:coolo",
}

login_fred
Expand Down

0 comments on commit ef4e219

Please sign in to comment.