Skip to content

Commit

Permalink
Topics can now be moved and leave a redirect in the forum they were m…
Browse files Browse the repository at this point in the history
…oved from.
  • Loading branch information
radar committed Dec 31, 2008
1 parent cbfb91f commit 08279d5
Show file tree
Hide file tree
Showing 17 changed files with 130 additions and 78 deletions.
32 changes: 13 additions & 19 deletions app/controllers/admin/forums_controller.rb
Expand Up @@ -11,12 +11,7 @@ def index
# Initializes a new forum.
def new
@forum = Forum.new
@forums, @categories = if @category
[@category.forums.find(:all, :order => "title ASC"), []]
else
[Forum.find(:all, :order => "title ASC"), Category.find(:all, :order => "name asc")]
end
@user_levels = UserLevel.find(:all, :order => "position ASC")
find_forums
end

# Creates a new forum.
Expand All @@ -31,8 +26,7 @@ def create
redirect
else
flash[:notice] = t(:forum_not_created)
@forums = Forum.find(:all, :order => "title")
@user_levels = UserLevel.find(:all, :order => "position ASC")
find_forums
render :action => "new"
end
end
Expand All @@ -41,15 +35,7 @@ def create
def edit
# We do this so we can't make a forum a sub of itself, or any of its descendants...
# As this would cause circular references which just aren't cool.

@forums, @categories = if @category
[@category.forums.find(:all, :order => "title ASC"), []]
else
[Forum.find(:all, :order => "title ASC"), Category.find(:all, :order => "name asc")]
end
@forums -= [@forum] + @forum.descendants
@user_levels = UserLevel.find(:all, :order => "position ASC")

find_forums
end

# Updates a forum.
Expand All @@ -58,9 +44,8 @@ def update
flash[:notice] = t(:forum_updated)
redirect
else
@forums = Forum.find(:all, :order => "title") - [@forum] - @forum.descendants
@user_levels = UserLevel.find(:all, :order => "position ASC")
flash[:notice] = t(:forum_not_updated)
find_forums
render :action => "edit"
end
end
Expand Down Expand Up @@ -119,6 +104,15 @@ def find_forum
not_found
end

def find_forums
@forums, @categories = if @category
[@category.forums.find(:all, :order => "title ASC"), []]
else
[Forum.find(:all, :order => "title ASC"), Category.find(:all, :order => "name asc")]
end
@user_levels = UserLevel.find(:all, :order => "position ASC")
end

# Called when the forum could not be found.
def not_found
flash[:notice] = t(:forum_not_found)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/application.rb
Expand Up @@ -24,7 +24,7 @@ class ApplicationController < ActionController::Base
def check_page_value
params[:page] = params[:page].to_i <= 0 ? "1" : params[:page]
end

def set_time_zone
Time.zone = current_user.time_zone if logged_in? && !current_user.time_zone.nil?
end
Expand Down
15 changes: 13 additions & 2 deletions app/controllers/forums_controller.rb
@@ -1,12 +1,17 @@
class ForumsController < ApplicationController
before_filter :store_location, :only => [:index, :show]
before_filter :find_category

# Shows all root forums.
# Limits this selection to forums the current user has access to.
# Also gathers stats for the Compulsory Stat Box.
def index
@categories = Category.without_parent.viewable_to(current_user)
@forums = Forum.without_category
if @category
@forums = @category.forums
else
@categories = Category.without_parent.viewable_to(current_user)
@forums = Forum.without_category
end
@lusers = User.recent.map { |u| u.to_s }.to_sentence
@users = User.count
@posts = Post.count
Expand All @@ -29,4 +34,10 @@ def show
end
end

private

def find_category
@category = Category.find(params[:category_id]) unless params[:category_id].blank?
end

end
2 changes: 1 addition & 1 deletion app/controllers/moderator/topics_controller.rb
Expand Up @@ -76,7 +76,7 @@ def merge

def move
if params[:new_forum_id]
@moderations_for_topics.each { |m| m.move!(params[:new_forum_id]) }
@moderations_for_topics.each { |m| m.move!(params[:new_forum_id], params[:leave_redirect] == '1') }
flash[:notice] = t(:topics_moved)
redirect_back_or_default(forum_path(params[:new_forum_id]))
end
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/forums_helper.rb
Expand Up @@ -2,7 +2,7 @@ module ForumsHelper
def breadcrumb(forum, breadcrumb='')
breadcrumb = ''
if forum.parent.nil?
breadcrumb += link_to(forum.category.name, category_path(forum.category)) + ' ->' if forum.category
breadcrumb += link_to(forum.category.name, category_forums_path(forum.category)) + ' ->' if forum.category
breadcrumb += ' ' + link_to(forum.title, forum_path(forum))
else
breadcrumb += " #{breadcrumb(forum.parent)} -> " + link_to(forum.title, forum_path(forum))
Expand Down
4 changes: 2 additions & 2 deletions app/models/moderation.rb
Expand Up @@ -34,8 +34,8 @@ def destroy!
destroy
end

def move!(new_forum_id)
moderated_object.move!(new_forum_id)
def move!(new_forum_id, leave_redirect=false)
moderated_object.move!(new_forum_id, leave_redirect)
destroy
end
end
2 changes: 1 addition & 1 deletion app/models/post.rb
Expand Up @@ -29,7 +29,7 @@ class Post < ActiveRecord::Base
attr_protected :forum_id, :user_id

def stop_spam
if user.posts.last.created_at > Time.now - TIME_BETWEEN_POSTS
if !user.posts.last.nil? && user.posts.last.created_at > Time.now - TIME_BETWEEN_POSTS
errors.add_to_base("You can only post once every #{distance_of_time_in_words(Time.now, Time.now - TIME_BETWEEN_POSTS)}") and return false
end
end
Expand Down
13 changes: 10 additions & 3 deletions app/models/topic.rb
Expand Up @@ -3,6 +3,7 @@ class Topic < ActiveRecord::Base
belongs_to :ip
belongs_to :forum
belongs_to :last_post, :class_name => "Post"
belongs_to :moved_to, :class_name => "Topic"

has_many :moderations, :as => :moderated_object, :dependent => :destroy
has_many :posts, :order => "posts.created_at asc"
Expand All @@ -16,7 +17,7 @@ class Topic < ActiveRecord::Base
validates_length_of :subject, :minimum => 4
validates_presence_of :subject, :forum_id, :user_id

attr_protected :sticky, :locked
attr_protected :sticky, :locked, :moved, :moved_to

# Instead of using a counter_cache on the belongs_to we do this
# because counter_cache doesn't take into account funky move! methods
Expand All @@ -40,7 +41,7 @@ def decrement_counters
end

def set_last_post
update_attribute("last_post_id", posts.last.id)
update_attribute("last_post_id", posts.last.id) unless moved
end

def increment_counters
Expand All @@ -52,7 +53,7 @@ def to_s
subject
end

def move!(new_forum_id)
def move!(new_forum_id, leave_redirect=false)
old_forum = Forum.find(forum_id)
was_old_last_post = old_forum.last_post == last_post
new_forum = Forum.find(new_forum_id)
Expand All @@ -61,6 +62,12 @@ def move!(new_forum_id)
new_forum.increment!(:posts_count, posts.count)
old_forum.decrement!(:topics_count)
old_forum.decrement!(:posts_count, posts.count)
if leave_redirect
redirect = old_forum.topics.build(:subject => subject, :created_at => created_at, :user => user)
redirect.moved = true
redirect.moved_to = self
redirect.save!
end
is_new_last_post = new_forum.last_post.nil? || (new_forum.last_post.created_at <= posts.last.created_at)
new_forum.update_last_post(new_forum, posts.last) if is_new_last_post

Expand Down
76 changes: 40 additions & 36 deletions app/views/forums/index.html.erb
@@ -1,4 +1,4 @@
<% if @categories.empty? && @forums.empty? %>
<% if @category.nil? && @categories.empty? && @forums.empty? %>
<table cellspacing='0' cellpadding='2' class='forums' width='100%' rules='groups'>
<thead>
<tr>
Expand All @@ -14,44 +14,48 @@
</thead>
</table>
<% end %>
<% for category in @categories %>
<% div_for category do %>
<h2><%= category.name %></h2>
<table cellspacing='0' cellpadding='2' class='forums' width='100%' rules='groups'>
<% unless category.forums.empty? %>
<thead>
<tr>
<td align='left' width='40%'><%= t(:Forum) %></td>
<td align='center' width='100px'><%= t(:Topics) %></td>
<td align='center' width='100px'><%= t(:Posts) %></td>
<td align='right'><%= t(:Last_Post) %></td>
</tr>
</thead>
<tbody>
<%= render :partial => category.forums %>
</tbody>
<% else %>
<thead>
<tr>
<td align='center'>
<b>
<%= t(:no_forums) %>
<% unless is_admin? %>
<%= t(:administrator_should_create_forum) %>
<% else %>
<%= link_to t(:you_should_create_forum), new_admin_forum_path %>
<% end %>
</b>
</td>
</tr>
</thead>
<% end %>
</table>
<% if @category.nil? %>
<% for category in @categories %>
<% div_for category do %>
<h2><%= link_to category.name, [category, :forums] %></h2>
<table cellspacing='0' cellpadding='2' class='forums' width='100%' rules='groups'>
<% unless category.forums.empty? %>
<thead>
<tr>
<td align='left' width='40%'><%= t(:Forum) %></td>
<td align='center' width='100px'><%= t(:Topics) %></td>
<td align='center' width='100px'><%= t(:Posts) %></td>
<td align='right'><%= t(:Last_Post) %></td>
</tr>
</thead>
<tbody>
<%= render :partial => category.forums %>
</tbody>
<% else %>
<thead>
<tr>
<td align='center'>
<b>
<%= t(:no_forums) %>
<% unless is_admin? %>
<%= t(:administrator_should_create_forum) %>
<% else %>
<%= link_to t(:you_should_create_forum), new_admin_forum_path %>
<% end %>
</b>
</td>
</tr>
</thead>
<% end %>
</table>
<% end %>
<br />
<% end %>
<% else %>
<%= link_to(t(:rBoard), forums_path) %> -> <%= link_to @category.name, [@category, :forums] %><br />
<h2><%= @category.name %></h2>
<% end %>
<br />
<% unless @forums.empty? %>
<table cellspacing='0' cellpadding='2' class='forums' width='100%' rules='groups'>
<thead>
Expand Down
26 changes: 21 additions & 5 deletions app/views/forums/show.html.erb
Expand Up @@ -45,11 +45,19 @@
<% for topic in @topics %>
<tr id='topic_<%= topic.id %>'>
<% if is_moderator? %>
<td align='center'><%= link_to_remote check_box_tag("moderated_topics[]", topic.id, !topic.moderations.for_user(current_user).empty?, :id => "topic_#{topic.id}_moderated"), :url => moderator_topic_moderations_path(topic) %></td>
<td align='center'>
<% if !topic.moved? %>
<%= link_to_remote check_box_tag("moderated_topics[]", topic.id, !topic.moderations.for_user(current_user).empty?, :id => "topic_#{topic.id}_moderated"), :url => moderator_topic_moderations_path(topic) %>
<% else %>
<%= link_to_remote check_box_tag("moderated_topics[]", topic.moved_to.id, !topic.moved_to.moderations.for_user(current_user).empty?, :id => "topic_#{topic.moved_to.id}_moderated"), :url => moderator_topic_moderations_path(topic.moved_to) %>
<% end %>
</td>
<% end %>
<td>
<% if topic.sticky? %>
<%= theme_image_tag("sticky.jpg") %>
<% elsif topic.moved? %>
<%= theme_image_tag("moved.jpg") %>
<% elsif logged_in? && !current_user.previous_login.nil? && topic.posts.last.created_at > current_user.previous_login && !topic.locked %>
<%= theme_image_tag("new.jpg") %>
<% elsif topic.locked? %>
Expand All @@ -59,13 +67,21 @@
<% end %>
</td>
<td class='topic_subject'>
<b><%= link_to(h(topic.subject), forum_topic_path(topic.forum, topic)) %>
<%= will_paginate(topic.posts.paginate(:per_page => per_page, :page => 1), :prev_label => nil, :next_label => "", :params => { :controller => "topics", :action => "show", :id => topic.id, :forum_id => @forum.id }) %></b>
<b>
<%= link_to(h(topic.subject), topic.moved? ? forum_topic_path(1, topic.moved_to) : forum_topic_path(topic.forum, topic)) %>
<%= will_paginate(topic.posts.paginate(:per_page => per_page, :page => 1), :prev_label => nil, :next_label => "", :params => { :controller => "topics", :action => "show", :id => topic.id, :forum_id => @forum.id }) %>
</b>
</td>
<td align='center'><%= topic.posts.size - 1 %></td>
<td align='center'><%= (topic.moved? ? topic.moved_to.posts.count : topic.posts.count) - 1 %></td>
<td align='center'><%= topic.views %></td>
<td align='center'><%= link_to(h(topic.user), topic.user) %></td>
<td align='right' width='15%'><%= time_ago_in_words(topic.posts.last.created_at) %> <%= t(:ago) %> <%= t(:by) %> <%= link_to h(topic.last_post.user), topic.last_post.user %></td>
<td align='right' width='15%'>
<% if topic.moved? %>
-
<% else %>
<%= time_ago_in_words(topic.posts.last.created_at) %> <%= t(:ago) %> <%= t(:by) %> <%= link_to h(topic.last_post.user), topic.last_post.user %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
Expand Down
2 changes: 2 additions & 0 deletions app/views/topics/_buttons.html.erb
@@ -1,6 +1,8 @@
<% unless @all_forums.empty? %>
<%= submit_tag(t(:Move)) %>
<%= select_tag("new_forum_id", options_from_collection_for_select(@all_forums, "id", "title")) %>
<%= check_box_tag "leave_redirect" %>
<%= label_tag "leave_redirect", t(:Leave_redirect?) %>
<% end %>
<%= submit_tag(t(:Lock)) %>
<%= submit_tag(t(:Unlock)) %>
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.rb
Expand Up @@ -221,6 +221,7 @@
:Items_per_page => "Items per page",
:Last_IP => "Last IP",
:Last_Post => "Last Post",
:Leave_redirect => "Leave redirect?",
:Lock => "Lock",
:Lock_this_topic => "Lock this topic",
:Locked! => "Locked!",
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20081231013302_add_redirect_to_topics.rb
@@ -0,0 +1,11 @@
class AddRedirectToTopics < ActiveRecord::Migration
def self.up
add_column :topics, :moved, :boolean, :default => false
add_column :topics, :moved_to_id, :integer
end

def self.down
remove_column :topics, :moved
remove_column :topics, :moved_to_id
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Expand Up @@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20081229035814) do
ActiveRecord::Schema.define(:version => 20081231013302) do

create_table "banned_ips", :force => true do |t|
t.string "ip"
Expand Down Expand Up @@ -144,6 +144,8 @@
t.boolean "delta"
t.boolean "deleted", :default => false
t.integer "ip_id"
t.boolean "moved", :default => false
t.integer "moved_to_id"
end

add_index "topics", ["id", "forum_id"], :name => "index_topics_on_id_and_forum_id"
Expand Down

0 comments on commit 08279d5

Please sign in to comment.