diff --git a/app/controllers/forum_topics_controller.rb b/app/controllers/forum_topics_controller.rb index d865be07e..96a13dbe6 100644 --- a/app/controllers/forum_topics_controller.rb +++ b/app/controllers/forum_topics_controller.rb @@ -1,6 +1,7 @@ class ForumTopicsController < ApplicationController respond_to :html, :xml, :json before_filter :member_only, :except => [:index, :show] + before_filter :janitor_only, :only => [:new_merge, :create_merge] before_filter :normalize_search, :only => :index def new @@ -68,6 +69,17 @@ class ForumTopicsController < ApplicationController redirect_to forum_topics_path, :notice => "All topics marked as read" end + def new_merge + @forum_topic = ForumTopic.find(params[:id]) + end + + def create_merge + @forum_topic = ForumTopic.find(params[:id]) + @merged_topic = ForumTopic.find(params[:merged_id]) + @forum_topic.merge(@merged_topic) + redirect_to forum_topic_path(@forum_topic) + end + private def normalize_search if params[:title_matches] diff --git a/app/models/forum_topic.rb b/app/models/forum_topic.rb index d2dafbad3..4183bb32a 100644 --- a/app/models/forum_topic.rb +++ b/app/models/forum_topic.rb @@ -130,4 +130,9 @@ class ForumTopic < ActiveRecord::Base end result end + + def merge(topic) + ForumPost.update_all({:topic_id => id}, :id => topic.posts.map(&:id)) + update_attribute(:is_deleted, true) + end end diff --git a/app/views/forum_topics/_secondary_links.html.erb b/app/views/forum_topics/_secondary_links.html.erb index 3961594a9..722d10134 100644 --- a/app/views/forum_topics/_secondary_links.html.erb +++ b/app/views/forum_topics/_secondary_links.html.erb @@ -31,6 +31,7 @@ <% else %>
  • <%= link_to "Sticky", forum_topic_path(@forum_topic, :forum_topic => {:is_sticky => true}), :method => :put, :confirm => "Are you sure you want to sticky this forum topic?" %>
  • <% end %> +
  • <%= link_to "Merge", new_merge_forum_topic_path(@forum_topic) %>
  • <% end %> <% end %> <% end %> diff --git a/app/views/forum_topics/new_merge.html.erb b/app/views/forum_topics/new_merge.html.erb new file mode 100644 index 000000000..5bee2080e --- /dev/null +++ b/app/views/forum_topics/new_merge.html.erb @@ -0,0 +1,23 @@ +
    +
    +

    Merge Forum Topic

    + +

    Merge <%= @forum_topic.title %> into:

    + + <%= form_tag(create_merge_forum_topic_path(@forum_topic)) do %> +
    + +
    + <%= submit_tag "Merge" %> + <% end %> +
    +
    + +<%= render "secondary_links" %> + +<% content_for(:page_title) do %> + Merge Forum Topic - <%= Danbooru.config.app_name %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index f7017c5f0..d41b806d2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -113,6 +113,8 @@ Danbooru::Application.routes.draw do resources :forum_topics do member do post :undelete + get :new_merge + post :create_merge end collection do post :mark_all_as_read diff --git a/test/unit/forum_topic_test.rb b/test/unit/forum_topic_test.rb index b9cc9551c..a5a1dd8e7 100644 --- a/test/unit/forum_topic_test.rb +++ b/test/unit/forum_topic_test.rb @@ -14,6 +14,19 @@ class ForumTopicTest < ActiveSupport::TestCase CurrentUser.ip_addr = nil end + context "#merge" do + setup do + @topic2 = FactoryGirl.create(:forum_topic, :title => "yyy") + FactoryGirl.create(:forum_post, :topic_id => @topic.id, :body => "xxx") + FactoryGirl.create(:forum_post, :topic_id => @topic2.id, :body => "xxx") + end + + should "merge all the posts in one topic into the other" do + @topic.merge(@topic2) + assert_equal(2, @topic.posts.count) + end + end + context "#read_by?" do context "for a topic that was never read by the user" do should "return false" do