ensures only one banner topic at all time

This commit is contained in:
Régis Hanol 2014-06-16 19:21:21 +02:00
parent 5238a95efb
commit 30611c343c
5 changed files with 68 additions and 26 deletions

View File

@ -177,12 +177,7 @@ class TopicsController < ApplicationController
topic = Topic.find_by(id: params[:topic_id].to_i)
guardian.ensure_can_moderate!(topic)
# TODO: only one banner at the same time
topic.archetype = Archetype.banner
topic.add_moderator_post(current_user, I18n.t("archetypes.banner.message.make"))
topic.save
topic.make_banner!(current_user)
render nothing: true
end
@ -191,10 +186,7 @@ class TopicsController < ApplicationController
topic = Topic.find_by(id: params[:topic_id].to_i)
guardian.ensure_can_moderate!(topic)
topic.archetype = Archetype.default
topic.add_moderator_post(current_user, I18n.t("archetypes.banner.message.remove"))
topic.save
topic.remove_banner!(current_user)
render nothing: true
end

View File

@ -626,6 +626,22 @@ class Topic < ActiveRecord::Base
end
end
def make_banner!(user)
# only one banner at the same time
previous_banner = Topic.where(archetype: Archetype.banner).first
previous_banner.remove_banner!(user) if previous_banner.present?
self.archetype = Archetype.banner
self.add_moderator_post(user, I18n.t("archetypes.banner.message.make"))
self.save
end
def remove_banner!(user)
self.archetype = Archetype.default
self.add_moderator_post(user, I18n.t("archetypes.banner.message.remove"))
self.save
end
def self.starred_counts_per_day(sinceDaysAgo=30)
TopicUser.starred_since(sinceDaysAgo).by_date_starred.count
end

View File

@ -857,15 +857,11 @@ describe TopicsController do
describe 'when logged in' do
before do
@admin = log_in(:admin)
@topic = Fabricate(:topic, user: @admin)
end
it "changes the topic archetype to 'banner'" do
Topic.any_instance.expects(:archetype=).with(Archetype.banner)
Topic.any_instance.expects(:add_moderator_post)
xhr :put, :make_banner, topic_id: @topic.id
topic = Fabricate(:topic, user: log_in(:admin))
Topic.any_instance.expects(:make_banner!)
xhr :put, :make_banner, topic_id: topic.id
response.should be_success
end
@ -883,15 +879,11 @@ describe TopicsController do
describe 'when logged in' do
before do
@admin = log_in(:admin)
@topic = Fabricate(:topic, user: @admin)
end
it "resets the topic archetype" do
Topic.any_instance.expects(:archetype=).with(Archetype.default)
Topic.any_instance.expects(:add_moderator_post)
xhr :put, :remove_banner, topic_id: @topic.id
topic = Fabricate(:topic, user: log_in(:admin))
Topic.any_instance.expects(:remove_banner!)
xhr :put, :remove_banner, topic_id: topic.id
response.should be_success
end

View File

@ -11,6 +11,10 @@ end
Fabricator(:topic_allowed_user) do
end
Fabricator(:banner_topic, from: :topic) do
archetype Archetype.banner
end
Fabricator(:private_message_topic, from: :topic) do
user
title { sequence(:title) { |i| "This is a private message #{i}" } }

View File

@ -638,6 +638,44 @@ describe Topic do
end
end
describe "make_banner!" do
before do
@topic = Fabricate(:topic)
@user = @topic.user
end
it "changes the topic archetype to 'banner'" do
@topic.expects(:add_moderator_post)
@topic.make_banner!(@user)
@topic.archetype.should == Archetype.banner
end
it "ensures only one banner topic at all time" do
banner_topic = Fabricate(:banner_topic)
Topic.where(archetype: Archetype.banner).count.should == 1
@topic.make_banner!(@user)
Topic.where(archetype: Archetype.banner).count.should == 1
end
end
describe "remove_banner!" do
before do
@topic = Fabricate(:topic)
@user = @topic.user
end
it "resets the topic archetype" do
@topic.expects(:add_moderator_post)
@topic.remove_banner!(@user)
@topic.archetype.should == Archetype.default
end
end
context 'last_poster info' do
before do