messages: implement batch-delete function

closes #119
This commit is contained in:
joshua stein 2014-02-19 13:31:48 -06:00
parent 8755384bd8
commit d0711892f6
3 changed files with 63 additions and 19 deletions

View file

@ -87,6 +87,31 @@ class MessagesController < ApplicationController
end
end
def batch_delete
deleted = 0
params.each do |k,v|
if v.to_s == "1" && m = k.match(/^delete_(.+)$/)
if (message = Message.where(:short_id => m[1]).first)
if message.author_user_id == @user.id
message.deleted_by_author = true
elsif message.recipient_user_id == @user.id
message.deleted_by_recipient = true
else
next
end
message.save!
deleted += 1
end
end
end
flash[:success] = "Deleted #{deleted} message#{deleted == 1 ? "" : "s"}."
return redirect_to "/messages"
end
def keep_as_new
@message.has_been_read = false
@message.save

View file

@ -1,3 +1,12 @@
<script>
$(document).ready(function() {
$("#delete_all").click(function(e) {
var table = $(e.target).closest("table");
$("td input:checkbox", table).attr("checked", e.target.checked);
});
});
</script>
<div class="box wide">
<div class="legend" style="float: right;">
<% if @direction == :out %>
@ -14,27 +23,35 @@
</div>
<% if @messages.any? %>
<table class="data zebra" width="100%" cellspacing=0>
<tr>
<th width="15%"><%= @direction == :in ? "From" : "To" %></th>
<th width="20%"><%= @direction == :in ? "Received" : "Sent" %></th>
<th width="60%">Subject</th>
</tr>
<% @messages.includes(:author, :recipient).each do |message| %>
<tr class="<%= message.has_been_read? ? "" : "bold" %>">
<td><% if @direction == :in %>
<a href="/u/<%= message.author.username %>"><%=
message.author.username %></a>
<% else %>
<a href="/u/<%= message.recipient.username %>"><%=
message.recipient.username %></a>
<% end %></td>
<td><%= raw(time_ago_in_words_label(message.created_at)) %> ago</td>
<td><a href="/messages/<%= message.short_id %>"><%= message.subject
%></a></td>
<%= form_tag batch_delete_messages_url do %>
<table class="data zebra" width="100%" cellspacing=0>
<tr>
<th width="3%"><%= check_box_tag "delete_all",
:id => "delete_all" %></th>
<th width="15%"><%= @direction == :in ? "From" : "To" %></th>
<th width="17%"><%= @direction == :in ? "Received" : "Sent" %></th>
<th width="60%">Subject</th>
</tr>
<% @messages.includes(:author, :recipient).each do |message| %>
<tr class="<%= message.has_been_read? ? "" : "bold" %>">
<td><%= check_box_tag "delete_#{message.short_id}" %></td>
<td><% if @direction == :in %>
<a href="/u/<%= message.author.username %>"><%=
message.author.username %></a>
<% else %>
<a href="/u/<%= message.recipient.username %>"><%=
message.recipient.username %></a>
<% end %></td>
<td><%= raw(time_ago_in_words_label(message.created_at)) %> ago</td>
<td><a href="/messages/<%= message.short_id %>"><%= message.subject
%></a></td>
</tr>
<% end %>
</table>
<p>
<%= submit_tag "Delete Selected" %>
</p>
<% end %>
</table>
<% else %>
<p>
You do not have any <%= @direction == :in ? "" : "sent" %> private

View file

@ -61,6 +61,8 @@ Lobsters::Application.routes.draw do
get "/comments/page/:page" => "comments#index"
get "/messages/sent" => "messages#sent"
post "/messages/batch_delete" => "messages#batch_delete",
:as => "batch_delete_messages"
resources :messages do
post "keep_as_new"
end