Class MessagesController
In: ../../../app/controllers/emails_controller.rb
../../../test/functional/emails_controller_test.rb
Parent: ApplicationController

Re-raise errors caught by the controller.

Methods

Included Modules

RestfulEasyEmailsControllerSystem

Public Instance methods

POST /emails

[Source]

    # File ../../../app/controllers/emails_controller.rb, line 34
34:   def create
35:     @email = Message.new((params[:email] || {}).merge(:sender => rezm_user))
36:     
37:     respond_to do |format|
38:       if @email.save
39:         flash[:notice] = 'Message was sent successfully.'
40:         format.html { redirect_to outbox_profile_emails_path }
41:       else
42:         format.html { render :action => "new" }
43:       end
44:     end
45:   end

DELETE /emails/1

[Source]

    # File ../../../app/controllers/emails_controller.rb, line 48
48:   def destroy
49:     @email= Message.find_by_id(params[:id])
50:     
51:     respond_to do |format|
52:       if can_view(@email)
53:         mark_email_for_destruction(@email)
54:         format.html { redirect_to current_mailbox }
55:       else
56:         headers["Status"] = "Forbidden"
57:         format.html {render :file => "public/403.html", :status => 403}
58:       end
59:     end
60:   end

POST /emails/destroy_selected

[Source]

     # File ../../../app/controllers/emails_controller.rb, line 119
119:   def destroy_selected
120:   
121:     respond_to do |format|
122:       if !params[:to_delete].nil?
123:         emails = params[:to_delete].map { |m| Message.find_by_id(m) } 
124:         emails.each do |email| 
125:           mark_email_for_destruction(email)
126:         end
127:         format.html { redirect_to current_mailbox }
128:       else
129:         format.html { redirect_to inbox_profile_emails_path }
130:       end
131:     end
132:   end

GET /emails/inbox GET /emails/inbox.xml GET /emails/inbox.atom Displays all new and read and undeleted emails in the User‘s inbox

[Source]

    # File ../../../app/controllers/emails_controller.rb, line 68
68:   def inbox
69:     session[:mail_box] = "inbox"
70:     @emails = rezm_user.inbox_emails
71:     
72:     respond_to do |format|
73:       format.html { render :action => "index" }
74:       format.xml  { render :xml    => @emails.to_xml }
75:       format.atom { render :action => "index" }
76:     end
77:   end

GET /emails

[Source]

    # File ../../../app/controllers/emails_controller.rb, line 9
 9:   def index
10:     redirect_to inbox_profile_emails_url
11:   end

GET /emails/new

[Source]

    # File ../../../app/controllers/emails_controller.rb, line 29
29:   def new
30:     @email= Message.new
31:   end

GET /emails/outbox Displays all emails sent by the user

[Source]

    # File ../../../app/controllers/emails_controller.rb, line 81
81:   def outbox
82:     session[:mail_box] = "outbox"
83:     @emails = rezm_user.outbox_emails
84:     
85:     respond_to do |format|
86:       format.html { render :action => "index" }
87:     end
88:   end

GET /emails/1/reply

[Source]

     # File ../../../app/controllers/emails_controller.rb, line 102
102:   def reply
103:     @email = Message.find_by_id(params[:id])
104:     
105:     respond_to do |format|
106:       if can_view(@email)
107:         @email.recipient = @email.sender_name
108:         @email.subject = "Re: " + @email.subject 
109:         @email.body = "\n\n___________________________\n" + @email.sender_name + " wrote:\n\n" + @email.body
110:         format.html { render :action => "new" }
111:       else
112:         headers["Status"] = "Forbidden"
113:         format.html {render :file => "public/403.html", :status => 403}
114:       end
115:     end
116:   end

[Source]

   # File ../../../test/functional/emails_controller_test.rb, line 5
5:                           def rescue_action(e) raise e end

GET /emails/1

[Source]

    # File ../../../app/controllers/emails_controller.rb, line 14
14:   def show
15:     @email = Message.find_by_id(params[:id])
16:     
17:     respond_to do |format|
18:       if can_view(@email)
19:         @email.mark_email_read(rezm_user)
20:         format.html # show.html.erb
21:       else
22:         headers["Status"] = "Forbidden"
23:         format.html {render :file => "public/403.html", :status => 403}
24:       end
25:     end
26:   end

GET /emails/trashbin Displays all emails deleted from the user‘s inbox

[Source]

    # File ../../../app/controllers/emails_controller.rb, line 92
92:   def trashbin
93:     session[:mail_box] = "trashbin"
94:     @emails = rezm_user.trashbin_emails
95:     
96:     respond_to do |format|
97:       format.html { render :action => "index" }
98:     end
99:   end

Protected Instance methods

Security check to make sure the requesting user is either the sender (for outbox display) or the receiver (for inbox or trash_bin display)

[Source]

     # File ../../../app/controllers/emails_controller.rb, line 138
138:   def can_view(email)
139:     true if !email.nil? and (rezm_user.id == email.sender_id or rezm_user.id == email.receiver_id)
140:   end

[Source]

     # File ../../../app/controllers/emails_controller.rb, line 142
142:   def current_mailbox
143:     case session[:mail_box]
144:     when "inbox"
145:       inbox_profile_emails_path
146:     when "outbox"
147:       outbox_profile_emails_path
148:     when "trashbin"
149:       trashbin_profile_emails_path
150:     else
151:       inbox_profile_emails_path
152:     end
153:   end

Performs a "soft" delete of a email then check if it can do a destroy on the email

  • Marks Inbox emails as "receiver deleted" essentially moving the email to the Trash Bin
  • Marks Outbox emails as "sender_deleted" and "purged" removing the email from [:inbox_emails, :outbox_emails, :trashbin_emails]
  • Marks Trash Bin emails as "receiver purged"
  • Checks to see if both the sender and reciever have purged the email. If so, the email record is destroyed

Returns to the updated view of the current "mailbox"

[Source]

     # File ../../../app/controllers/emails_controller.rb, line 162
162:   def mark_email_for_destruction(email)
163:     if can_view(email)
164:       
165:       # "inbox"
166:       if rezm_user.id == email.receiver_id and !email.receiver_deleted
167:         email.receiver_deleted = true             
168:             
169:       # "outbox"
170:       elsif rezm_user.id == email.sender_id
171:         email.sender_deleted = true
172:         email.sender_purged = true
173:             
174:       # "trash_bin"
175:       elsif rezm_user.id == email.receiver_id and email.receiver_deleted
176:         email.receiver_purged = true
177:       end
178:       
179:       email.save(false) 
180:       email.purge
181:     end
182:   end

[Validate]