* Updated gemfile

* Added forum post/topic unit tests
* Added forum post/topic controller tests
This commit is contained in:
albert
2011-01-12 18:00:07 -05:00
parent 46164eab4f
commit 668fbab77a
18 changed files with 342 additions and 105 deletions

View File

@@ -1,42 +1,53 @@
class ForumTopicsController < ApplicationController
respond_to :html, :xml, :json
before_filter :member_only, :except => [:index, :show]
rescue_from User::PrivilegeError, :with => "static/access_denied"
def new
@forum_topic = ForumTopic.new
respond_with(@forum_topic)
end
def edit
@forum_topic = ForumTopic.find(params[:id])
check_privilege(@forum_topic)
respond_with(@forum_topic)
end
def index
@search = ForumTopic.search(params[:search])
@forum_topics = @search.paginate(:page => params[:page], :order => "updated_at DESC")
respond_with(@forum_topics)
end
def show
@forum_topic = ForumTopic.find(params[:id])
respond_with(@forum_topic)
end
def create
@forum_topic = ForumTopic.new(params[:forum_topic])
if @forum_topic.save
redirect_to forum_topic_path(@forum_topic)
else
render :action => "new"
end
@forum_topic = ForumTopic.create(params[:forum_topic])
respond_with(@forum_topic)
end
def update
@forum_topic = ForumTopic.find(params[:id])
if @forum_topic.update_attributes(params[:forum_topic])
redirect_to forum_topic_path(@forum_topic)
else
render :action => "edit"
end
check_privilege(@forum_topic)
@forum_topic.update_attributes(params[:forum_topic])
respond_with(@forum_topic)
end
def destroy
@forum_topic = ForumTopic.find(params[:id])
check_privilege(@forum_topic)
@forum_topic.destroy
redirect_to forum_topics_path
end
respond_with(@forum_topic)
end
private
def check_privilege(forum_topic)
if !forum_topic.editable_by?(CurrentUser.user)
raise User::PrivilegeError
end
end
end

View File

@@ -2,11 +2,22 @@ class ForumPost < ActiveRecord::Base
attr_accessible :body, :topic_id
belongs_to :creator, :class_name => "User"
belongs_to :topic, :class_name => "ForumTopic"
before_validation :initialize_creator, :on => :create
before_validation :initialize_updater
after_save :update_topic_updated_at
validates_presence_of :body, :topic_id, :creator_id
scope :search_body, lambda {|body| where(["text_index @@ plainto_tsquery(?)", body])}
validates_presence_of :body, :creator_id
scope :body_matches, lambda {|body| where(["text_index @@ plainto_tsquery(?)", body])}
search_methods :body_matches
def update_topic_updated_at
topic.touch
topic.update_attributes(:updater_id => CurrentUser.id)
end
def initialize_creator
self.creator_id = CurrentUser.id
end
def initialize_updater
self.updater_id = CurrentUser.id
end
end

View File

@@ -1,8 +1,25 @@
class ForumTopic < ActiveRecord::Base
attr_accessible :title
attr_accessible :title, :original_post_attributes
belongs_to :creator, :class_name => "User"
has_many :posts, :class_name => "ForumPost", :order => "forum_posts.id asc"
belongs_to :updater, :class_name => "User"
has_many :posts, :class_name => "ForumPost", :order => "forum_posts.id asc", :foreign_key => "topic_id"
has_one :original_post, :class_name => "ForumPost", :order => "forum_posts.id asc", :foreign_key => "topic_id"
before_validation :initialize_creator, :on => :create
before_validation :initialize_updater
validates_presence_of :title, :creator_id
scope :search_title, lambda {|title| where(["text_index @@ plainto_tsquery(?)", title])}
accepts_nested_attributes_for :posts
scope :title_matches, lambda {|title| where(["text_index @@ plainto_tsquery(?)", title])}
search_methods :title_matches
accepts_nested_attributes_for :original_post
def editable_by?(user)
creator_id == user.id || user.is_moderator?
end
def initialize_creator
self.creator_id = CurrentUser.id
end
def initialize_updater
self.updater_id = CurrentUser.id
end
end

View File

@@ -0,0 +1,7 @@
<div>
<%= simple_form_for @search do |f| %>
<%= f.input :title_matches %>
<%= f.input :creator_name_equals %>
<%= f.button :submit %>
<% end %>
</div>

View File

@@ -0,0 +1,12 @@
<h1>Edit Topic</h1>
<%= simple_form_for(@forum_topic) do |f| %>
<%= f.input :title %>
<%= f.simple_fields_for :original_post do |pf| %>
<%= text_field_tag "forum_topic[original_post_attributes][topic_id]", @forum_topic.id %>
<%= pf.input :body %>
<% end %>
<%= f.button :submit %>
<% end %>

View File

@@ -0,0 +1,24 @@
<h1>Forum</h1>
<%= render "search" %>
<table>
<thead>
<tr>
<th>Creator</th>
<th>Title</th>
<th>Updated by</th>
<th>Updated at</th>
</tr>
</thead>
<tbody>
<% @forum_topics.each do |topic| %>
<tr>
<td><%= topic.creator.name %></td>
<td><%= topic.title %></td>
<td><%= topic.updater.name %></td>
<td><%= compact_time topic.updated_at %></td>
</tr>
<% end %>
</tbody>
</table>

View File

@@ -0,0 +1,11 @@
<h1>New Topic</h1>
<%= simple_form_for(@forum_topic) do |f| %>
<%= f.input :title %>
<%= f.simple_fields_for :original_post do |pf| %>
<%= pf.input :body %>
<% end %>
<%= f.button :submit %>
<% end %>