Skip to content

Commit 02bcd8b

Browse files
committed
Discussions now show the author name, and security.
Oops, let's not let them change who made a post with firebug, shall we? set those authors in the controller! And now everything has authors. Rock.
1 parent 3cbec7e commit 02bcd8b

8 files changed

Lines changed: 30 additions & 14 deletions

File tree

controllers/forums.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
end
2525

2626
post "/forums/:forum/discussions" do
27+
params[:author] = current_user.username
2728
@discussion = Discussion.create(params)
2829
flash[:notice] = "Discussion created!"
29-
redirect "/forums/#{params[:forum]}"
30+
redirect "/forums/#{params[:forum]}/#{@discussion.slug}"
3031
end
3132

3233
#you view a discussion here
@@ -44,6 +45,7 @@
4445

4546
post "/forums/reply/to/:forum/:discussion" do
4647
@discussion = Discussion.first(:forum => params[:forum], :slug => params[:discussion])
48+
params[:reply][:author] = current_user.username
4749
@discussion.replies << Reply.new(params[:reply])
4850
@discussion.save
4951

factories/discussion.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
Factory.define :discussion do |t|
2-
1+
Factory.define :discussion do |d|
2+
d.author "jane_hacker"
33
end
44
Factory.define :discussion_with_reply, :parent => :discussion do |t|
55
t.replies {|r| [Factory(:reply)] }
66
end
77

88
Factory.define :reply do |r|
99
r.body "I'm here to help!"
10+
r.author "john_hacker"
1011
end

features/forum.feature

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Feature: The Hackety Forum
1414
Scenario: View replies to a discussion
1515
Given I'm logged in
1616
And there's a discussion named "I need help" in "learning_ruby" with a reply
17-
When I go to the discussion "learning_ruby/i_need_help"
17+
When I go to the discussion "i_need_help" in "learning_ruby"
1818
Then I should see "I need help"
1919
And I should see "I'm here to help!"
2020
Scenario: Make a new reply
@@ -26,18 +26,19 @@ Feature: The Hackety Forum
2626
When I follow "Reply"
2727
And I fill in "Body" with "I'll help you!"
2828
And I press "Create Reply"
29-
Then I should be on the discussion "learning_ruby/i_need_help" page
29+
Then I should be on the discussion "i_need_help" in "learning_ruby"
3030
And I should see "Replied!"
3131
And I should see "I'll help you"
3232
And I should see "steve says:"
3333
Scenario: Make a new discussion
34-
Given I'm logged in
34+
Given I'm logged in as "some_hacker"
3535
And I go to the forums
3636
And I follow "Learning Ruby"
3737
When I follow "New Discussion"
3838
And I fill in "Title" with "HALP!"
3939
And I fill in "Body" with "I'm really unsure as to how to do something."
4040
And I press "Create Discussion"
41-
Then I should be on the discussion "learning_ruby"
41+
Then I should be on the discussion "halp" in "learning_ruby"
4242
And I should see "Discussion created!"
4343
And I should see "HALP!"
44+
And I should see "some_hacker says:"

features/support/paths.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ def path_to(page_name)
4646
"/programs/#{$1}"
4747
when /the forums/
4848
"/forums"
49-
when /the discussion "(.*)"/
49+
when /the forum "(.*)"/
5050
"/forums/#{$1}"
51+
when /the discussion "(.*)" in "(.*)"/
52+
"/forums/#{$2}/#{$1}"
5153
when /the help page/
5254
"/help"
5355
when /the github page/

models/discussion.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ class Discussion
66
#the title of a discussion
77
key :title, String
88

9+
#the person who created this discussion
10+
key :author, String
11+
912
#the text of a discussion
1013
key :body, String
1114

@@ -19,10 +22,17 @@ class Discussion
1922
key :slug, String
2023

2124
before_save :make_slug
25+
validate_on_create :author_check
2226

2327
private
2428
def make_slug
2529
self.slug = self.title.to_slug
2630
end
2731

32+
def author_check
33+
if author.nil?
34+
errors.add(:author, "Someone must have started this discussion!")
35+
end
36+
end
37+
2838
end

views/discussions/new.haml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
%h1 New Discussion
22

33
%form{:action => "/forums/#{@forum}/discussions", :method => "POST"}
4-
%label{ :for => "discussion_title"} Title:
5-
%input{ :type => "text", :name => "title", :id => "discussion_title"}
6-
%label{ :for => "discussion_body"} Body:
7-
%input{ :type => "text", :name => "body", :id => "discussion_body"}
8-
%input{ :type => "submit", :value => "Create Discussion" }
4+
%label{ :for => "discussion_title"} Title:
5+
%input{ :type => "text", :name => "title", :id => "discussion_title"}
6+
%label{ :for => "discussion_body"} Body:
7+
%input{ :type => "text", :name => "body", :id => "discussion_body"}
8+
%input{ :type => "submit", :value => "Create Discussion" }

views/discussions/show.haml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
%h1= @discussion.title
2+
%p #{@discussion.author} says:
23

34
%p= @discussion.body
45

views/forums/reply.haml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
%form{:action => "/forums/reply/to/#{@forum}/#{@discussion}", :method => "POST"}
22
%label{:for => "reply_body"} Body
33
%input{:type => "text", :id => "reply_body", :name => "reply[body]"}
4-
%input{:type => "hidden", :id => "reply_author", :name => "reply[author]", :value => current_user.username}
54
%input{:type => "submit", :value => "Create Reply"}

0 commit comments

Comments
 (0)