event comment delete

This commit is contained in:
Kyle Drake 2014-05-02 22:45:57 -07:00
parent a4a2b5165b
commit 8210670122
6 changed files with 47 additions and 16 deletions

13
app.rb
View file

@ -896,6 +896,19 @@ post '/event/:event_id/delete' do |event_id|
return {result: 'error'}.to_json return {result: 'error'}.to_json
end end
post '/comment/:comment_id/delete' do |comment_id|
require_login
content_type :json
comment = Comment[id: comment_id]
if comment.event.site == current_site || comment.actioning_site == current_site
comment.delete
return {result: 'success'}.to_json
end
return {result: 'error'}.to_json
end
def require_admin def require_admin
redirect '/' unless signed_in? && current_site.is_admin redirect '/' unless signed_in? && current_site.is_admin
end end

View file

@ -0,0 +1,9 @@
Sequel.migration do
up {
DB.add_column :comments, :is_deleted, :boolean, default: false
}
down {
DB.drop_column :comments, :is_deleted
}
end

View file

@ -1,4 +1,5 @@
class Comment < Sequel::Model class Comment < Sequel::Model
include Sequel::ParanoidDelete
many_to_one :event many_to_one :event
many_to_one :actioning_site, class: :Site many_to_one :actioning_site, class: :Site
end end

View file

@ -1,16 +1,19 @@
var Comment = function(eventId, csrfToken) { var Comment = {
this.eventId = eventId create: function(eventId, csrfToken, form) {
this.csrfToken = csrfToken var form = $(form)
} var comment = form.find('[name="comment"]').val()
form.remove()
Comment.prototype.create = function(form) {
var self = this $.post('/event/'+eventId+'/comment', {csrf_token: csrfToken, message: comment}, function(res) {
var form = $(form) console.log(res)
var comment = form.find('[name="comment"]').val() location.reload()
form.remove() })
},
$.post('/event/'+this.eventId+'/comment', {csrf_token: this.csrfToken, message: comment}, function(res) {
console.log(res) delete: function(commentId, csrfToken) {
}) $.post('/comment/'+commentId+'/delete', {csrf_token: csrfToken}, function(res) {
console.log(res)
location.reload()
})
}
} }

View file

@ -15,13 +15,18 @@
<div class="content"> <div class="content">
<div class="comments"> <div class="comments">
<% event.comments.each do |comment| %> <% event.comments.each do |comment| %>
<div class="comment"> <div class="comment" id="comment_<%= comment.id %>">
<img class="avatar" src="<%= comment.actioning_site.screenshot_url('index.html', '82x62') %>"> <img class="avatar" src="<%= comment.actioning_site.screenshot_url('index.html', '82x62') %>">
<a href="" class="user"><%= comment.actioning_site.username %></a> <a href="" class="user"><%= comment.actioning_site.username %></a>
<span class="date"><%= comment.created_at.ago %></span> <span class="date"><%= comment.created_at.ago %></span>
<p><%= comment.message %></p> <p><%= comment.message %></p>
</div> </div>
<div class="actions"><a href="">Like (1)</a></div> <div class="actions">
<a id="like" href="#" onclick="Comment.toggleLike(<%= comment.id %>, '<%= csrf_token %>)">Like (1)</a>
<% if event.site_id == current_site.id || comment.actioning_site_id == current_site.id %>
<a href="#" onclick="Comment.delete(<%= comment.id %>, '<%= csrf_token %>'); return false">Delete</a>
<% end %>
</div>
<% end %> <% end %>
</div> </div>
</div> </div>

View file

@ -1,5 +1,5 @@
<div id="comment-template" style="display: none"> <div id="comment-template" style="display: none">
<form onsubmit="new Comment({{ eventId }}, '<%= csrf_token %>').create(this); location.reload(); return false"> <form onsubmit="Comment.create({{ eventId }}, '<%= csrf_token %>', this); location.reload(); return false">
<input name="comment" type="text" autocomplete="off" maxlength="<%= Site::MAX_COMMENT_SIZE %>" style="width: 100%" placeholder="Comment on this..."> <input name="comment" type="text" autocomplete="off" maxlength="<%= Site::MAX_COMMENT_SIZE %>" style="width: 100%" placeholder="Comment on this...">
<button class="btn-Action">Post</button> <button class="btn-Action">Post</button>
</form> </form>