Skip to content

Commit 0caf44c

Browse files
committed
First run at comments on content.
This doesn't look spectacular, but it works. Sweet.
1 parent 11f3dd1 commit 0caf44c

5 files changed

Lines changed: 50 additions & 2 deletions

File tree

controllers/content.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,20 @@
1515
@content = Content.find(params[:id])
1616
haml :"content/show"
1717
end
18+
19+
post "/content/:id/comment" do
20+
@content = Content.first(:id => params[:id])
21+
if current_user
22+
params[:comment][:author] = current_user.username
23+
params[:comment][:author_email] = current_user.email
24+
else
25+
params[:comment][:author] = "Anonymous"
26+
params[:comment][:author_email] = "anonymous@example.com"
27+
end
28+
@content.comments << Comment.new(params[:comment])
29+
@content.save
30+
31+
flash[:notice] = "Replied!"
32+
redirect "/content/#{@content.id}"
33+
end
34+

helpers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ def require_login_or_api!(opts = {:return => "/"})
6262

6363
#gives a gravatar url for an email
6464
def gravatar_url_for email
65-
require 'md5'
66-
"http://www.gravatar.com/avatar/#{MD5::md5(email.downcase)}"
65+
require 'digest/md5'
66+
"http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email.downcase)}"
6767
end
6868

6969
end

models/comment.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Comment
2+
include MongoMapper::EmbeddedDocument
3+
4+
#the body of the comment
5+
key :body, String
6+
7+
#the person who wrote it
8+
key :author, String
9+
key :author_email, String
10+
11+
end

models/content.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class Content
66

77
key :author, String
88
key :author_email, String
9+
10+
many :comments
911

1012
def image
1113
require 'digest/md5'

views/content/show.haml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,21 @@
22
%p
33
:markdown
44
#{@content.body}
5+
- @content.comments.each do |reply|
6+
%div
7+
%a{:href => "/hackers/#{reply.author}"}
8+
%img{:src => gravatar_url_for(reply.author_email), :alt => "#{reply.author.downcase}_gravatar", :class => "comment_gravatar"}
9+
%div
10+
%p
11+
<a href="/hackers/#{reply.author}">#{reply.author}</a> says:
12+
%p
13+
= reply.body
14+
15+
%h2 Add a comment
16+
%form{:action => "/content/#{@content.id}/comment", :method => "POST"}
17+
%label{:for => "comment_body"} Body
18+
%br
19+
%textarea{:id => "comment_body", :name => "comment[body]", :rows => 20, :cols => 50}
20+
%br
21+
%input{:type => "submit", :value => "Submit", :class => "button"}
22+

0 commit comments

Comments
 (0)