Skip to content

Instantly share code, notes, and snippets.

@gmoeck
Created May 8, 2009 19:23
Show Gist options
  • Save gmoeck/108953 to your computer and use it in GitHub Desktop.
Save gmoeck/108953 to your computer and use it in GitHub Desktop.
<div class="user-messages-nav">
<span <%= current == "inbox" ? 'class = "active" ' : '' %>>
<%= link_to "Inbox", :controller => :mailboxes, :action => :show, :user_id => current_user.id, :id => "inbox" %>
</span>
<span <%= current == "sentbox" ? 'class = "active" ' : '' %>>
<%= link_to "Sent Messages", :controller => :mailboxes, :action => :show, :user_id => current_user.id, :id => "sentbox" %>
</span>
<span <%= current == "trash" ? 'class = "active" ' : '' %>>
<%= link_to "Archive", :controller => :mailboxes, :action => :show, :user_id => current_user.id, :id => "trash" %>
</span>
<span>
<%= link_to "Compose Message",:controller => :messages, :action => :new, :user_id => current_user.id %>
</span>
</div>
<tr class="listRow0" onmouseover="oldColor = this.style.backgroundColor; this.style.backgroundColor = '#cfc'" onmouseout="this.style.backgroundColor = oldColor">
<td valign="top" nowrap="nowrap" class="grid_cell">
<%= check_box_tag "message_ids[]",message.id %>
</td>
<td class="grid_cell" onclick="location = '<%= user_message_url(current_user,message) %>'">
<%= message.created_at.to_s(:long_am_pm) %>
</td>
<td class="grid_cell" onclick="location = '<%= user_message_url(current_user,message) %>'">
<%= message.message.sender.name %>
</td>
<td class="grid_cell" onclick="location = '<%= user_message_url(current_user,message) %>'">
<%= message.message.subject %>
</td>
<td class="grid_cell" onclick="location = '<%= user_message_url(current_user,message) %>'">
<%= show_message_status(message) %>
</td>
<td valign="top" class="grid_cell" nowrap align="right">
<% links = [] %>
<% links.push(link_to 'Read', user_message_path(current_user,message)) %>
<% links.push(link_to 'Delete', user_message_path(current_user,message), :confirm => 'Are you sure?', :method => :delete) %>
<%= quick_menu(:title => 'Options', :identifier => message.id.to_s, :links => links) %>
</td>
</tr>
class MailboxesController < ApplicationController
before_filter :login_required
before_filter :find_user_and_mailbox_from_params
# GET /users/:user_id/mailboxes
def index
redirect_to :action => :show, :user_id => params[:user_id], :id => "inbox"
end
# GET /users/:user_id/mailboxes/:id
# GET /users/:user_id/mailboxes/:id.:format
def show
@messages = @mailbox.mail
end
def update
@messages = get_messages_from_params
@todo = params[:command]
@messages.each do |message|
if @todo == "archive"
@user.mailbox.move_to(:trash, :conditions => "mail.id = #{message.id}")
elsif @todo == "unread"
@user.mailbox.move_to(:inbox, :conditions => "mail.id = #{message.id}")
message.mark_as_unread
elsif @todo == "read"
@user.mailbox.move_to(:inbox, :conditions => "mail.id = #{message.id}")
message.mark_as_read
elsif @todo == "delete"
@user.mailbox.delete(:conditions => "mail.id = #{message.id}")
end
end
redirect_to :action => :show, :user_id => params[:user_id], :id => params[:type]
end
protected
def find_user_and_mailbox_from_params
@user = User.find(params[:user_id])
@mailbox = @user.mailbox[params[:id]] if params[:id]
@mailbox_type = params[:id]
end
def get_messages_from_params
messages = []
params[:message_ids].each do |id|
messages << Mail.find(id)
end
messages
end
end
class MessagesController < ApplicationController
before_filter :login_required
before_filter :find_user_from_params, :except => :reply_create
# GET /users/:user_id/messages/:id
# GET /users/:user_id/messages/:id.:format
def show
@mail = Mail.find(params[:id])
@mail.mark_as_read; @mail.save
@message = @mail.message
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @message.to_xml }
end
end
# GET /users/:user_id/messages/new
def new
@send_to = params[:send_to]
@message = Message.new(:subject => params[:subject])
end
# POST /users/:user_id/messages
def create
@user = current_user
respond_to do |format|
if @user.send_message(find_recipients_from_params, params[:message][:body], params[:message][:subject])
format.html { redirect_to user_mailboxes_url(@user)}
end
end
end
# GET /users/:user_id/messages/reply/:id
def reply
@user = current_user
@old_message = Mail.find(params[:id])
@message = Message.new(:subject => "RE:#{@old_message.message.subject}")
end
# POST /messages/reply_create/:id
def reply_create
@user = current_user
@old_message = Mail.find(params[:id])
respond_to do |format|
if @user.reply_to_sender(@old_message,params[:message][:body])
format.html { redirect_to user_mailboxes_url(@user)}
end
end
end
# DELETE /users/:user_id/messages/:id
def destroy
@message = Mail.find(params[:id])
@user.mailbox.delete(:conditions => "mail.id = #{@message.id}")
respond_to do |format|
format.html { redirect_to user_mailboxes_url(@user) }
format.xml { head :ok }
end
end
protected
def find_user_from_params
@user = User.find(params[:user_id])
end
def find_recipients_from_params
recipients = []
params[:message][:send_ids].each do |id|
recipients << User.find(id)
end
recipients
end
end
<h1>Compose Message</h1>
<%= render :partial => "mailboxes/mailboxes", :locals => {:current => "new"}%>
<% form_for([current_user,@message]) do |f| %>
<table border="0" cellspacing="0" cellpadding="0" width="60%" class="listContainer">
<tr valign="top">
<td width="5"><%= image_tag 'tb-1tp.gif', :width => 5, :height => 5 %></td>
<td width="*"><%= image_tag 'spacer.gif', :width => 1, :height => 5 %></td>
<td width="5"><%= image_tag 'tb-2tp.gif', :width => 5, :height => 5 %></td>
</tr>
<tr valign="top">
<td width="5"><%= image_tag "spacer.gif", :width => 5, :height => 1 %></td>
<td width="*">
<table cellspacing="0" width="100%" cellpadding="5" class="formbox1">
<tr>
<td valign="top" class="formHead3">Details</td>
<td align="right" class="formHead3">
<%= f.submit "Send", :class => "btn" %>
</td>
</tr>
<tr>
<td valign="top" class="formbox2" width="100">&nbsp;</td>
<td valign="top">&nbsp;</td>
</tr>
<tr>
<td class="formbox2"><span class="label">Send To: </span></td>
<td>
<% User.can_receive_email.each do |user| %>
<%= check_box_tag "message[send_ids][]",user.id, @send_to.to_i == user.id -%> <%= user.name %><br />
<% end %>
</td>
</tr>
<tr>
<td class="formbox2"><span class="label">Subject: </span></td>
<td><%= f.text_field :subject %></td>
</tr>
<tr>
<td class="formbox2"><span class="label">Body: </span></td>
<td><%= f.text_area :body %></td>
</tr>
<tr>
<td colspan="2" align="right" class="formHead3">
<%= f.submit "Send", :class => "btn" %>
</td>
</tr>
</table>
</td>
</tr>
<tr valign="top">
<td width="5"><%= image_tag 'tb-2.gif', :width => 5, :height => 5 %></td>
<td width="*"><%= image_tag 'spacer.gif', :width => 1, :height => 5 %></td>
<td width="5"><%= image_tag 'tb-1.gif', :width => 5, :height => 5 %></td>
</tr>
</table>
<% end %>
<h1>Compose Message</h1>
<%= render :partial => "mailboxes/mailboxes", :locals => {:current => "new"}%>
<% form_for(@message, :url =>"/messages/reply_create/#{@old_message.id}", :method => :post) do |f| %>
<%= hidden_field_tag "reply_id", @old_message.id %>
<table border="0" cellspacing="0" cellpadding="0" width="60%" class="listContainer">
<tr valign="top">
<td width="5"><%= image_tag 'tb-1tp.gif', :width => 5, :height => 5 %></td>
<td width="*"><%= image_tag 'spacer.gif', :width => 1, :height => 5 %></td>
<td width="5"><%= image_tag 'tb-2tp.gif', :width => 5, :height => 5 %></td>
</tr>
<tr valign="top">
<td width="5"><%= image_tag "spacer.gif", :width => 5, :height => 1 %></td>
<td width="*">
<table cellspacing="0" width="100%" cellpadding="5" class="formbox1">
<tr>
<td valign="top" class="formHead3">Details</td>
<td align="right" class="formHead3">
<%= f.submit "Send", :class => "btn" %>
</td>
</tr>
<tr>
<td valign="top" class="formbox2" width="100">&nbsp;</td>
<td valign="top">&nbsp;</td>
</tr>
<tr>
<td class="formbox2"><span class="label">Send To: </span></td>
<td>
<%= @old_message.message.sender.name %>
</td>
</tr>
<tr>
<td class="formbox2"><span class="label">Subject: </span></td>
<td><%= @message.subject %></td>
</tr>
<tr>
<td class="formbox2"><span class="label">Body: </span></td>
<td><%= f.text_area :body %></td>
</tr>
<tr>
<td colspan="2" align="right" class="formHead3">
<%= f.submit "Send", :class => "btn" %>
</td>
</tr>
</table>
</td>
</tr>
<tr valign="top">
<td width="5"><%= image_tag 'tb-2.gif', :width => 5, :height => 5 %></td>
<td width="*"><%= image_tag 'spacer.gif', :width => 1, :height => 5 %></td>
<td width="5"><%= image_tag 'tb-1.gif', :width => 5, :height => 5 %></td>
</tr>
</table>
<% end %>
<h1><%= @mailbox_type.capitalize %> Messages</h1>
<%= render :partial => "mailboxes/mailboxes", :locals => {:current => @mailbox_type}%>
<% form_tag url_for(:action => :update), :id => "mailboxes_form", :method => :put do |f| %>
<%= select_tag "command", options_for_select([["",""],
["Send to Archive", "archive"],
["Mark as New", "unread"],
["Mark as Read", "read"],
["Delete","delete"]]),
:onchange => "$('mailboxes_form').submit()" %>
<%= hidden_field_tag 'type', @mailbox_type %>
<table width="75%" cellspacing="0" cellpadding="2" style="border:thin solid #444444; color:#FFFFFF;">
<tr>
<td class="listInactiveColumnHead" align="center">&nbsp;</td>
<td class="listInactiveColumnHead"><span class="listInactiveColumnHeadText">Date</span></td>
<td class="listInactiveColumnHead"><span class="listInactiveColumnHeadText">Sender</span></td>
<td class="listInactiveColumnHead"><span class="listInactiveColumnHeadText">Subject</span></td>
<td class="listInactiveColumnHead"><span class="listInactiveColumnHeadText">Status</span></td>
<td class="listInactiveColumnHead" align="center">&nbsp;</td>
</tr>
<%= render :partial => "message", :collection => @messages, :locals => {:form => f} if @messages%>
</table>
<% end %>
<h1>Message Details</h1>
<%= render :partial => "mailboxes/mailboxes", :locals => {:current => :none}%>
<table border="0" cellspacing="0" cellpadding="0" width="60%" class="listContainer">
<tr valign="top">
<td width="5"><%= image_tag 'tb-1tp.gif', :width => 5, :height => 5 %></td>
<td width="*"><%= image_tag 'spacer.gif', :width => 1, :height => 5 %></td>
<td width="5"><%= image_tag 'tb-2tp.gif', :width => 5, :height => 5 %></td>
</tr>
<tr valign="top">
<td width="5"><%= image_tag "spacer.gif", :width => 5, :height => 1 %></td>
<td width="*">
<table cellspacing="0" width="100%" cellpadding="5" class="formbox1">
<tr>
<td valign="top" class="formHead3">Details</td>
<td align="right" class="formHead3">
<%= button_to "Reply", {:action => "reply", :id => @mail.id, :user_id => current_user.id}, :class => "btn" %>
</td>
</tr>
<tr>
<td valign="top" class="formbox2" width="100">&nbsp;</td>
<td valign="top">&nbsp;</td>
</tr>
<tr>
<td class="formbox2"><span class="label">Sent: </span></td>
<td><%= @message.created_at.to_s(:long_am_pm)%> by <%= @message.sender.name %></td>
</tr>
<tr>
<td class="formbox2" valign="top"><span class="label">Recipient(s): </span></td>
<td>
<% @message.recipients.each do |recipient| %>
<%= recipient.name %><br />
<% end %>
</td>
<tr>
<td class="formbox2"><span class="label">Subject: </span></td>
<td><%= @message.subject %></td>
</tr>
<tr>
<td valign="top" class="formbox2"><span class="label">Message: </span></td>
<td><%= @message.body %></td>
</tr>
</tr>
</table>
</td>
</tr>
<tr valign="top">
<td width="5"><%= image_tag 'tb-2.gif', :width => 5, :height => 5 %></td>
<td width="*"><%= image_tag 'spacer.gif', :width => 1, :height => 5 %></td>
<td width="5"><%= image_tag 'tb-1.gif', :width => 5, :height => 5 %></td>
</tr>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment