Skip to content

Commit 8fb0883

Browse files
committed
Added even more tests, an .autotest file for growl notifications, fixed up a few more places where it was needlessly calling #id on objects for route helpers.
Fixed up the relationships between forums and the last post and the sub forum they were posted in. Changed the #belongs_to_user method for messages to just #belongs_to? The post model now will correctly define which forum it was last posted in. Removed the rcov file, use rake spec:rcov SHOW_ONLY="app" now.
1 parent 82bbcdd commit 8fb0883

34 files changed

+4215
-2113
lines changed

.autotest

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Autotest::Growl
2+
3+
def self.growl title, msg, img, pri=0, sticky=""
4+
system "growlnotify -n autotest --image #{img} -p #{pri} -m #{msg.inspect} #{title} #{sticky}"
5+
end
6+
7+
Autotest.add_hook :ran_command do |at|
8+
results = [at.results].flatten.join("\n")
9+
output = results.slice(/(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+not implemented)?/)
10+
if output
11+
if $~[2].to_i > 0
12+
growl "FAIL", "#{output}", "~/.autotest_images/fail.png", 2
13+
else
14+
growl "Pass", "#{output}", "~/.autotest_images/pass.png"
15+
end
16+
end
17+
end
18+
end

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ db/*.sqlite3
55
config/database.yml
66
nbproject/**/*
77
nbproject/project.*
8-
commit
98
test
109
coverage
1110
coverage/*

app/controllers/admin/accounts_controller.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def ban_ip
1616
else
1717
@banned_ip = BannedIp.new(params[:banned_ip])
1818
end
19-
2019
@banned = BannedIp.find(:all, :conditions => ["ban_time > ?",Time.now])
2120
end
2221

app/controllers/messages_controller.rb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def index
77
end
88

99
def new
10-
@message = Message.new
10+
@message = current_user.outbox_messages.new
1111
@users = User.find(:all, :order => "login ASC") - [current_user]
1212
if @users.empty?
1313
flash[:notice] = "There's nobody else to send a message to!"
@@ -16,8 +16,7 @@ def new
1616
end
1717

1818
def create
19-
params[:message][:from_id] = session[:user]
20-
@message = Message.new(params[:message])
19+
@message = current_user.outbox_messages.new(params[:message])
2120
if @message.save
2221
flash[:notice] = "The message has been sent."
2322
redirect_back_or_default(messages_path)
@@ -28,14 +27,9 @@ def create
2827
end
2928

3029
def destroy
31-
#refactor! if if else end if end else end... this can be done so much better.
32-
@message = Message.find(params[:id])
33-
if @message.belongs_to_user(current_user.id)
34-
if @message.from_id == current_user.id
35-
@message.update_attribute("from_deleted",true)
36-
else
37-
@message.update_attribute("to_deleted",true)
38-
end
30+
@message = Message.find(params[:id])
31+
if @message.belongs_to?(current_user.id)
32+
@message.update_attribute("#{@message.from_id == current_user.id ? "from" : "to"}_deleted",true)
3933
@message.destroy if @message.from_deleted == @message.to_deleted
4034
flash[:notice] = "This message has been deleted."
4135
else
@@ -46,7 +40,7 @@ def destroy
4640

4741
def show
4842
@message = Message.find(params[:id])
49-
if !@message.belongs_to_user(current_user.id)
43+
if !@message.belongs_to?(current_user.id)
5044
flash[:notice] = "That message does not belong to you."
5145
redirect_back_or_default(messages_path) and return
5246
else

app/controllers/posts_controller.rb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class PostsController < ApplicationController
2-
#TODO: Rescue on error where needed.
32
before_filter :login_required
43
def edit
54
@post = Post.find(params[:id])
@@ -9,7 +8,8 @@ def edit
98
else
109
render :layout => false
1110
end
12-
11+
rescue ActiveRecord::RecordNotFound
12+
not_found
1313
end
1414

1515
def update
@@ -19,17 +19,18 @@ def update
1919
else
2020
flash[:notice] = "This post could not be updated."
2121
end
22+
rescue ActiveRecord::RecordNotFound
23+
not_found
2224
end
2325

2426
def create
25-
@topic = Topic.find(params[:topic_id])
26-
@posts = @topic.posts
27+
@topic = Topic.find(params[:topic_id], :include => :posts)
28+
@posts = @topic.posts.find(:all, :order => "id DESC", :limit => 10)
2729
@post = @topic.posts.build(params[:post])
2830
if @post.save
2931
flash[:notice] = "Post has been created."
30-
redirect_to forum_topic_path(@post.forum.id,@post.topic.id)
32+
redirect_to forum_topic_path(@post.forum,@topic)
3133
else
32-
@posts = @topic.posts.find(:all, :order => "id DESC", :limit => 10)
3334
@quoting_post = Post.find(params[:quote]) unless params[:quote].blank?
3435
flash[:notice] = "This post could not be created."
3536
render :action => "../topics/reply"
@@ -41,8 +42,19 @@ def destroy
4142
flash[:notice] = "Post was deleted."
4243
if @post.topic.posts.size.zero?
4344
@post.topic.destroy
44-
flash[:notice] = "Post was deleted. This was the only post in the topic, so topic was deleted also."
45+
flash[:notice] += " This was the only post in the topic, so topic was deleted also."
46+
redirect_to forum_path(@post.forum)
47+
else
48+
redirect_to forum_topic_path(@post.forum, @post.topic)
4549
end
50+
rescue ActiveRecord::RecordNotFound
51+
not_found
4652
end
4753

54+
private
55+
def not_found
56+
flash[:notice] = "The post you were looking for could not be found."
57+
redirect_back_or_default(forums_path)
58+
end
59+
4860
end

app/controllers/topics_controller.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ def create
1313
Topic.transaction do
1414
@topic = current_user.topics.create!(params[:topic].merge(:forum_id => params[:forum_id]))
1515
@post = @topic.posts.create!(params[:post].merge(:user_id => current_user.id ))
16-
@topic.sticky = true if params[:topic][:sticky] == 1
16+
@topic.sticky = true if params[:topic][:sticky] == 1 && is_admin?
1717
end
1818
flash[:notice] = "Topic has been created."
19-
redirect_to forum_topic_path(@topic.forum.id,@topic.id)
19+
redirect_to forum_topic_path(@topic.forum, @topic)
2020
rescue ActiveRecord::RecordNotSaved, ActiveRecord::RecordInvalid => @e
2121
flash[:notice] = "Topic was not created."
2222
#So it doesn't forget the post text
23-
@post = Post.create(params[:post].merge(:user_id => current_user.id ))
23+
@post = current_user.posts.new(params[:post])
2424
render :template => "topics/new"
2525
end
2626

2727
def show
28-
@topic = Topic.find(params[:id])
28+
@topic ||= Topic.find(params[:id], :include => [:forum])
2929
@topic.increment!("views")
3030
end
3131

@@ -62,14 +62,14 @@ def lock
6262
topic = Topic.find(params[:id])
6363
topic.update_attribute("locked",true)
6464
flash[:notice] = "This topic has been locked."
65-
redirect_to topic_path(topic.id)
65+
redirect_to topic_path(topic)
6666
end
6767

6868
def unlock
6969
topic = Topic.find(params[:id])
7070
topic.update_attribute("locked",false)
7171
flash[:notice] = "This topic has been unlocked."
72-
redirect_to topic_path(topic.id)
72+
redirect_to topic_path(topic)
7373
end
7474

7575
private
@@ -79,8 +79,9 @@ def can_reply?
7979
end
8080

8181
def is_viewable?
82-
@topic = Topic.find(params[:id])
83-
unless (logged_in? && @topic.forum.is_visible_to < current_user.user_level_id) || @topic.forum.is_visible_to == 1
82+
@topic = Topic.find(params[:id], :include => [:forum])
83+
@forum = @topic.forum
84+
unless (logged_in? && @forum.is_visible_to < current_user.user_level_id) || @forum.is_visible_to == 1
8485
flash[:notice] = "You do not have the permissions to access that topic."
8586
redirect_back_or_default(forums_path)
8687
end

app/models/forum.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ class Forum < ActiveRecord::Base
66
validates_presence_of :title, :description
77
belongs_to :visible_to, :class_name => "UserLevel", :foreign_key => "is_visible_to"
88
belongs_to :creator_of_topics, :class_name => "UserLevel", :foreign_key => "topics_created_by"
9-
#has_one :last_post, :class_name => "Post"
9+
belongs_to :last_post, :class_name => "Post", :foreign_key => "last_post_id"
10+
belongs_to :last_post_forum, :class_name => "Forum", :foreign_key => "last_post_forum_id"
1011

1112
def to_s
1213
title
@@ -19,14 +20,6 @@ def topics
1920
old_topics.sort_by { |t| t.posts.last.created_at }.reverse
2021
end
2122

22-
def last_post
23-
Post.find_by_id(last_post_id)
24-
end
25-
26-
def last_post_forum
27-
Forum.find_by_id(last_post_forum_id)
28-
end
29-
3023
def descendants
3124
children.map { |f| !f.children.empty? ? f.children + [f]: f }.flatten
3225
end

app/models/message.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Message < ActiveRecord::Base
22
belongs_to :to, :class_name => "User", :foreign_key => :to_id
33
belongs_to :from, :class_name => "User", :foreign_key => :from_id
44

5-
def belongs_to_user(user_id)
5+
def belongs_to?(user_id)
66
to_id == user_id || from_id == user_id
77
end
88
end

app/models/post.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ class Post < ActiveRecord::Base
99
after_destroy :find_latest_post
1010

1111
def update_forum
12-
forum.last_post_id = id
12+
forum.last_post = self
1313
Post.update_latest_post(self)
1414
end
1515

1616
def self.update_latest_post(post)
17-
post.forum.last_post_id = post.id
17+
post.forum.last_post = post
1818
if post.forum.sub?
1919
for ancestor in post.forum.ancestors
20-
ancestor.last_post_id = post.id
21-
ancestor.last_post_forum_id = post.forum.id
20+
ancestor.last_post = post
21+
ancestor.last_post_forum = post.forum
2222
ancestor.save
2323
end
2424
end
25-
post.forum.last_post_id = post.id
26-
post.forum.last_post_forum_id = nil
25+
post.forum.last_post = post
26+
post.forum.last_post_forum = nil
2727
post.forum.save
2828
end
2929

@@ -32,8 +32,8 @@ def find_latest_post
3232
if !last.nil?
3333
update_latest_post(last)
3434
else
35-
forum.last_post_id = nil
36-
forum.last_post_forum_id = nil
35+
forum.last_post = nil
36+
forum.last_post_forum = nil
3737
forum.save
3838
end
3939

app/views/admin/forums/_forum.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<tr>
2-
<td class='icon'><%= link_to theme_image_tag("edit.jpg"), edit_admin_forum_path(forum.id) %>
3-
<%= link_to theme_image_tag("delete.jpg"), admin_forum_path(forum.id), :method => "delete", :confirm => "Are you sure you want to delete '#{forum.title}'" %></td>
2+
<td class='icon'><%= link_to theme_image_tag("edit.jpg"), edit_admin_forum_path(forum) %>
3+
<%= link_to theme_image_tag("delete.jpg"), admin_forum_path(forum), :method => "delete", :confirm => "Are you sure you want to delete '#{forum.title}'" %></td>
44
<td style='text-align:left !important'> <%= theme_image_tag("extender.jpg", :style => "padding-left:#{depth*20}") if extender %>
55
<%= link_to forum.title, forum_path(forum) %></td>
66
<td><%= forum.visible_to %></td>
77
<td><%= forum.creator_of_topics %></td>
88
<td><%= forum.topics.size %></td>
99
<td><%= forum.posts.size %></td>
1010
<td>
11-
<%= link_to theme_image_tag("top.jpg"), move_to_top_admin_forum_path(forum), :method => :put%>
11+
<%= link_to theme_image_tag("top.jpg"), move_to_top_admin_forum_path(forum), :method => :put %>
1212
<%= link_to theme_image_tag("up.jpg"), move_up_admin_forum_path(forum), :method => :put %>
1313
<%= forum.position %>
1414
<%= link_to theme_image_tag("down.jpg"), move_down_admin_forum_path(forum), :method => :put %>

app/views/forums/_forum.html.erb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
<tr><td align='left'><h1><%= link_to(forum.title, forum_path(forum.id)) %></h1><%= forum.description %></td>
2-
<td align='center'><%= forum.topics.size %></td>
3-
<td align='center'><%= forum.posts.size %></td>
4-
<td align='right'><% if !forum.last_post.nil? %>
5-
<%= time_ago_in_words(forum.last_post.created_at) %> ago
6-
by <%= link_to(forum.last_post.user.login, :controller => "accounts", :action => "user", :id => forum.last_post.user.login) %>
7-
in <%= link_to(forum.last_post.topic.subject, forum_topic_path(forum,forum.last_post.topic)) %>
8-
<% if !forum.last_post_forum.nil? %>
9-
in <%= link_to(forum.last_post_forum, forum_path(forum.last_post_forum)) %>
10-
<% end %>
11-
<% else %>
12-
None
13-
<% end %>
14-
</td></tr>
1+
<tr><td align='left'><h1><%= link_to(forum.title, forum_path(forum)) %></h1><%= forum.description %></td>
2+
<td align='center'><%= forum.topics.size %></td>
3+
<td align='center'><%= forum.posts.size %></td>
4+
<td align='right'><% if !forum.last_post.nil? %>
5+
<%= time_ago_in_words(forum.last_post.created_at) %> ago
6+
by <%= link_to(forum.last_post.user.login, :controller => "accounts", :action => "user", :id => forum.last_post.user.login) %>
7+
in <%= link_to(forum.last_post.topic.subject, forum_topic_path(forum,forum.last_post.topic)) %>
8+
<% if !forum.last_post_forum.nil? %>
9+
in <%= link_to(forum.last_post_forum, forum_path(forum.last_post_forum)) %>
10+
<% end %>
11+
<% else %>
12+
None
13+
<% end %>
14+
</td></tr>

app/views/messages/_message.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<tr>
2-
<td align='center'><%= link_to(message.to_read? ? style_image_tag("old.jpg") : style_image_tag("new.jpg"), message_path(message.id))%></td>
3-
<td align='center'><%= link_to(style_image_tag("delete.jpg"), message_path(message.id), :method => "delete", :confirm => "Are you sure you want to delete this message?") %></td>
2+
<td align='center'><%= link_to(message.to_read? ? theme_image_tag("old.jpg") : theme_image_tag("new.jpg"), message_path(message))%></td>
3+
<td align='center'><%= link_to(theme_image_tag("delete.jpg"), message_path(message), :method => "delete", :confirm => "Are you sure you want to delete this message?") %></td>
44
<td align='center'><%= message.from.login %></td>
55
<td align='center'><%= time_ago_in_words(message.created_at) %> ago</td>
66
</tr>

app/views/topics/show.html.erb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
21
<%= link_to("rBoard", forums_path) %> <%= breadcrumb(@topic.forum) %> -> <%= @topic.subject %>
32
<h2><%= @topic.subject %></h2>
43
<% if logged_in? %>
5-
<% if can_create_topics? %>
6-
<%= link_to "New Topic", new_forum_topic_path(@topic.forum) %> |
7-
<% end %>
8-
<% if can_reply? && !@topic.locked? %>
9-
10-
<%= link_to "New Reply", reply_topic_path(@topic) %> |
11-
<% end %>
12-
13-
<% if is_admin? %>
14-
<% if @topic.locked? %>
15-
<%= link_to "Unlock this topic", unlock_topic_path(@topic), :method => :put %>
16-
<% else %>
17-
<%= link_to "Lock this topic", lock_topic_path(@topic), :method => :put %>
18-
<% end %>
4+
<% if can_create_topics? %>
5+
<%= link_to "New Topic", new_forum_topic_path(@topic.forum) %> |
6+
<% end %>
7+
8+
<% if can_reply? && !@topic.locked? %>
9+
<%= link_to "New Reply", reply_topic_path(@topic) %>
1910
<% end %>
11+
12+
<% if is_admin? %>
13+
|
14+
<% if @topic.locked? %>
15+
<%= link_to "Unlock this topic", unlock_topic_path(@topic), :method => :put %>
16+
<% else %>
17+
<%= link_to "Lock this topic", lock_topic_path(@topic), :method => :put %>
18+
<% end %>
19+
<% end %>
2020
<% else %>
21-
Locked!
21+
| Locked!
2222
<% end %>
2323
<br />
2424
<%= render :partial => @topic.posts %>

0 commit comments

Comments
 (0)