Skip to content

Commit 938fdf8

Browse files
committed
Messages are back
1 parent 8978821 commit 938fdf8

7 files changed

Lines changed: 142 additions & 28 deletions

File tree

controllers/messages.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#people can get a form to send messages here
2+
get "/messages/new/to/:username" do
3+
#gotta be logged in!
4+
require_login!
5+
6+
#we've got to save the username to put it in the view
7+
@username = params[:username]
8+
9+
#render the template
10+
haml :"messages/new"
11+
end
12+
13+
#this is where the form POSTs to
14+
post "/messages" do
15+
#gotta be logged in!
16+
require_login!
17+
18+
params[:message][:sender] = current_user.username
19+
20+
#make a new message with our params
21+
message = Message.create(params[:message])
22+
23+
#set a friendly message
24+
flash[:notice] = "Message sent."
25+
26+
#render the page of the recipient
27+
redirect "/hackers/#{message.recipient}"
28+
end
29+
30+
#this is the page where you can see your messages
31+
get "/messages" do
32+
require_login!
33+
@messages = Message.all({"recipient" => current_user.username})
34+
haml :"messages/index"
35+
end
36+

models/message.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#this is the class for inter-site messages
2+
class Message
3+
#we want to include them in the database!
4+
include MongoMapper::Document
5+
6+
#this is the body text of the message
7+
key :body, String
8+
9+
#this is the username of the person who gets the message
10+
key :recipient, String
11+
12+
#this is the username of the person who sent the message
13+
key :sender, String
14+
15+
after_create :send_notification
16+
17+
private
18+
19+
def send_notification
20+
unless development?
21+
recipient_email = Hacker.first(:username => self.recipient).email
22+
Notifier.send_message_notification(recipient_email, self.sender)
23+
end
24+
end
25+
26+
end
27+

models/notifier.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Notifier
2+
def self.send_message_notification(recipient, who)
3+
Pony.mail(:to => recipient,
4+
:subject => "Hackety Hack: New Message",
5+
:from => "steve+hackety@steveklabnik.com",
6+
:body => render_haml_template("message", who),
7+
:via => :smtp, :via_options => PONY_VIA_OPTIONS)
8+
end
9+
10+
private
11+
12+
def self.render_haml_template(template, who)
13+
engine = Haml::Engine.new(File.open("views/notifier/#{template}.haml", "rb").read)
14+
engine.render(Object.new, :who => who)
15+
end
16+
end
17+

views/hackers/show.haml

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,45 @@
2121
- else
2222
%a{:href => "/hackers/#{@hacker.username}/follow"} Follow #{@hacker.username}
2323
- else
24-
%h2 Your Account
25-
%p You can use these forms to update your information:
24+
%a{:href => "#", :class => "button" } Change My Settings
25+
%div{:id => "account_settings", :style => "display:none"}
26+
%h2 Your Account
27+
%p You can use these forms to update your information:
28+
%br
29+
%p
30+
%form{:action => "/hackers/update", :method => "POST"}
31+
%table
32+
%tr
33+
%td
34+
%label{:for => "password[new]"} New Password
35+
%td
36+
%input{:type => "password", :name => "password[new]"}
37+
%tr
38+
%td
39+
%label{:for => "password[confirm]"} Confirm New Password
40+
%td
41+
%input{:type => "password", :name => "password[confirm]"}
42+
%tr
43+
%td
44+
%td
45+
%input{:type => "submit", :value => "Change password"}
46+
%hr
47+
%p
48+
%form{:action => "/hackers/update", :method => "POST"}
49+
%label{:for => "hacker[about]"} About Me
50+
%br
51+
%textarea{:name => "hacker[about]", :rows => 10, :cols => 40}
52+
%br
53+
%input{:type => "submit", :value => "Update About"}
2654
%br
27-
%p
28-
%form{:action => "/hackers/update", :method => "POST"}
29-
%table
30-
%tr
31-
%td
32-
%label{:for => "password[new]"} New Password
33-
%td
34-
%input{:type => "password", :name => "password[new]"}
35-
%tr
36-
%td
37-
%label{:for => "password[confirm]"} Confirm New Password
38-
%td
39-
%input{:type => "password", :name => "password[confirm]"}
40-
%tr
41-
%td
42-
%td
43-
%input{:type => "submit", :value => "Change password"}
44-
%hr
45-
%p
46-
%form{:action => "/hackers/update", :method => "POST"}
47-
%label{:for => "hacker[about]"} About Me
48-
%br
49-
%textarea{:name => "hacker[about]", :rows => 10, :cols => 40}
50-
%br
51-
%input{:type => "submit", :value => "Update About"}
52-
55+
%br
56+
%br
57+
- content_for :head do
58+
:javascript
59+
$(function() {
60+
$( ".button").button();
61+
$(".button").click(function() {
62+
$(".button").hide("blinds");
63+
$('#account_settings').toggle("blinds");
64+
});
65+
});

views/messages/index.haml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%ul
2+
- @messages.each do |message|
3+
%li From: #{message.sender}
4+
%li Message: #{message.body}
5+

views/messages/new.haml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
%h2 Sending a message to #{@username}
2+
%form{:action => "/messages", :method => "POST"}
3+
%input{:type => "hidden", :id => "message_recipient", :name => "message[recipient]", :value => @username}
4+
%br/
5+
%label{:for => "message_body" } Message text:
6+
%br/
7+
%textarea{:id => "message_body", :name => "message[body]"}
8+
%br/
9+
%input{:type => "submit", :value => "Send message", :class => "button"}
10+

views/notifier/message.haml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Hey there! You have a new message on the Hackety Hack site from #{who}! You can check it out here:
2+
3+
http://hackety-hack.com/messages
4+
5+
\- The Hackety Hack Robot
6+

0 commit comments

Comments
 (0)