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

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

parent 039c3753
module BroadcastMessagesHelper
def broadcast_message(message = BroadcastMessage.current)
def broadcast_message(message)
return unless message.present?
content_tag :div, class: 'broadcast-message', style: broadcast_message_style(message) do
......
......@@ -16,7 +16,7 @@ class BroadcastMessage < ActiveRecord::Base
def self.current
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
......
= 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 @@ class Internal < Grape::API
}
end
get "/broadcast_messages" do
if messages = BroadcastMessage.current
present messages, with: Entities::BroadcastMessage
else
[]
end
end
get "/broadcast_message" do
if message = BroadcastMessage.current
if message = BroadcastMessage.current.last
present message, with: Entities::BroadcastMessage
else
{}
......
......@@ -21,22 +21,29 @@
end
describe '.current' do
it "returns last message if time match" do
it 'returns message if time match' do
message = create(:broadcast_message)
expect(BroadcastMessage.current).to eq message
expect(BroadcastMessage.current).to include(message)
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)
expect(BroadcastMessage.current).to be_nil
expect(BroadcastMessage.current).to be_empty
end
it "returns nil if time has passed" do
it 'returns empty list if time has passed' do
create(:broadcast_message, :expired)
expect(BroadcastMessage.current).to be_nil
expect(BroadcastMessage.current).to be_empty
end
end
......
......@@ -15,21 +15,43 @@
end
end
describe "GET /internal/broadcast_message" do
context "broadcast message exists" do
let!(:broadcast_message) { create(:broadcast_message, starts_at: Time.now.yesterday, ends_at: Time.now.tomorrow ) }
describe 'GET /internal/broadcast_message' do
context 'broadcast message exists' do
let!(:broadcast_message) { create(:broadcast_message, starts_at: 1.day.ago, ends_at: 1.day.from_now ) }
it do
get api("/internal/broadcast_message"), secret_token: secret_token
it 'returns one broadcast message' do
get api('/internal/broadcast_message'), secret_token: secret_token
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
context "broadcast message doesn't exist" do
it do
get api("/internal/broadcast_message"), secret_token: secret_token
context 'broadcast message does not exist' do
it 'returns nothing' do
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(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