Commit 03b2ac65 authored by Mike Ricketts's avatar Mike Ricketts Committed by Rémy Coutable

#13336 - display multiple messages in both the UI and git output

parent 039c3753
module BroadcastMessagesHelper module BroadcastMessagesHelper
def broadcast_message(message = BroadcastMessage.current) def broadcast_message(message)
return unless message.present? return unless message.present?
content_tag :div, class: 'broadcast-message', style: broadcast_message_style(message) do content_tag :div, class: 'broadcast-message', style: broadcast_message_style(message) do
......
...@@ -16,7 +16,7 @@ class BroadcastMessage < ActiveRecord::Base ...@@ -16,7 +16,7 @@ class BroadcastMessage < ActiveRecord::Base
def self.current def self.current
Rails.cache.fetch("broadcast_message_current", expires_in: 1.minute) do Rails.cache.fetch("broadcast_message_current", expires_in: 1.minute) do
where("ends_at > :now AND starts_at <= :now", now: Time.zone.now).last where('ends_at > :now AND starts_at <= :now', now: Time.zone.now).order([:created_at, :id]).to_a
end end
end end
......
= broadcast_message - BroadcastMessage.current.each do |message|
= broadcast_message(message)
---
title: Display all current broadcast messages, not just the last one
merge_request: 11113
author: rickettm
...@@ -86,8 +86,16 @@ module API ...@@ -86,8 +86,16 @@ module API
} }
end end
get "/broadcast_messages" do
if messages = BroadcastMessage.current
present messages, with: Entities::BroadcastMessage
else
[]
end
end
get "/broadcast_message" do get "/broadcast_message" do
if message = BroadcastMessage.current if message = BroadcastMessage.current.last
present message, with: Entities::BroadcastMessage present message, with: Entities::BroadcastMessage
else else
{} {}
......
...@@ -21,22 +21,29 @@ describe BroadcastMessage, models: true do ...@@ -21,22 +21,29 @@ describe BroadcastMessage, models: true do
end end
describe '.current' do describe '.current' do
it "returns last message if time match" do it 'returns message if time match' do
message = create(:broadcast_message) message = create(:broadcast_message)
expect(BroadcastMessage.current).to eq message expect(BroadcastMessage.current).to include(message)
end end
it "returns nil if time not come" do it 'returns multiple messages if time match' do
message1 = create(:broadcast_message)
message2 = create(:broadcast_message)
expect(BroadcastMessage.current).to contain_exactly(message1, message2)
end
it 'returns empty list if time not come' do
create(:broadcast_message, :future) create(:broadcast_message, :future)
expect(BroadcastMessage.current).to be_nil expect(BroadcastMessage.current).to be_empty
end end
it "returns nil if time has passed" do it 'returns empty list if time has passed' do
create(:broadcast_message, :expired) create(:broadcast_message, :expired)
expect(BroadcastMessage.current).to be_nil expect(BroadcastMessage.current).to be_empty
end end
end end
......
...@@ -15,21 +15,43 @@ describe API::Internal do ...@@ -15,21 +15,43 @@ describe API::Internal do
end end
end end
describe "GET /internal/broadcast_message" do describe 'GET /internal/broadcast_message' do
context "broadcast message exists" do context 'broadcast message exists' do
let!(:broadcast_message) { create(:broadcast_message, starts_at: Time.now.yesterday, ends_at: Time.now.tomorrow ) } let!(:broadcast_message) { create(:broadcast_message, starts_at: 1.day.ago, ends_at: 1.day.from_now ) }
it do it 'returns one broadcast message' do
get api("/internal/broadcast_message"), secret_token: secret_token get api('/internal/broadcast_message'), secret_token: secret_token
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json_response["message"]).to eq(broadcast_message.message) expect(json_response['message']).to eq(broadcast_message.message)
end end
end end
context "broadcast message doesn't exist" do context 'broadcast message does not exist' do
it do it 'returns nothing' do
get api("/internal/broadcast_message"), secret_token: secret_token get api('/internal/broadcast_message'), secret_token: secret_token
expect(response).to have_http_status(200)
expect(json_response).to be_empty
end
end
end
describe 'GET /internal/broadcast_messages' do
context 'broadcast message(s) exist' do
let!(:broadcast_message) { create(:broadcast_message, starts_at: 1.day.ago, ends_at: 1.day.from_now ) }
it 'returns active broadcast message(s)' do
get api('/internal/broadcast_messages'), secret_token: secret_token
expect(response).to have_http_status(200)
expect(json_response[0]['message']).to eq(broadcast_message.message)
end
end
context 'broadcast message does not exist' do
it 'returns nothing' do
get api('/internal/broadcast_messages'), secret_token: secret_token
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(json_response).to be_empty expect(json_response).to be_empty
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment