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

View File

@ -626,6 +626,22 @@ class Topic < ActiveRecord::Base
end end
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) def self.starred_counts_per_day(sinceDaysAgo=30)
TopicUser.starred_since(sinceDaysAgo).by_date_starred.count TopicUser.starred_since(sinceDaysAgo).by_date_starred.count
end end

View File

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

View File

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

View File

@ -638,6 +638,44 @@ describe Topic do
end end
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 context 'last_poster info' do
before do before do