Commit df496fca authored by Robert Speicher's avatar Robert Speicher

Update BroadcastMessage model

- Adds default values for `color` and `font` attributes
- Adds `active?`, `started?`, `ended?`, and 'status' methods
parent 5a170679
...@@ -22,7 +22,32 @@ class BroadcastMessage < ActiveRecord::Base ...@@ -22,7 +22,32 @@ class BroadcastMessage < ActiveRecord::Base
validates :color, allow_blank: true, color: true validates :color, allow_blank: true, color: true
validates :font, allow_blank: true, color: true validates :font, allow_blank: true, color: true
default_value_for :color, '#E75E40'
default_value_for :font, '#FFFFFF'
def self.current def self.current
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).last
end
def active?
started? && !ended?
end
def started?
Time.zone.now >= starts_at
end
def ended?
ends_at < Time.zone.now
end
def status
if active?
'Active'
elsif ended?
'Expired'
else
'Pending'
end
end end
end end
...@@ -24,5 +24,10 @@ FactoryGirl.define do ...@@ -24,5 +24,10 @@ FactoryGirl.define do
starts_at 5.days.ago starts_at 5.days.ago
ends_at 3.days.ago ends_at 3.days.ago
end end
trait :future do
starts_at 5.days.from_now
ends_at 6.days.from_now
end
end end
end end
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
require 'spec_helper' require 'spec_helper'
describe BroadcastMessage, models: true do describe BroadcastMessage, models: true do
include ActiveSupport::Testing::TimeHelpers
subject { create(:broadcast_message) } subject { create(:broadcast_message) }
it { is_expected.to be_valid } it { is_expected.to be_valid }
...@@ -34,20 +36,99 @@ describe BroadcastMessage, models: true do ...@@ -34,20 +36,99 @@ describe BroadcastMessage, models: true do
it { is_expected.not_to allow_value('000').for(:font) } it { is_expected.not_to allow_value('000').for(:font) }
end end
describe :current do describe '.current' do
it "should return last message if time match" do it "should return last message if time match" do
broadcast_message = create(:broadcast_message, starts_at: Time.now.yesterday, ends_at: Time.now.tomorrow) message = create(:broadcast_message)
expect(BroadcastMessage.current).to eq(broadcast_message)
expect(BroadcastMessage.current).to eq message
end end
it "should return nil if time not come" do it "should return nil if time not come" do
create(:broadcast_message, starts_at: Time.now.tomorrow, ends_at: Time.now + 2.days) create(:broadcast_message, :future)
expect(BroadcastMessage.current).to be_nil expect(BroadcastMessage.current).to be_nil
end end
it "should return nil if time has passed" do it "should return nil if time has passed" do
create(:broadcast_message, starts_at: Time.now - 2.days, ends_at: Time.now.yesterday) create(:broadcast_message, :expired)
expect(BroadcastMessage.current).to be_nil expect(BroadcastMessage.current).to be_nil
end end
end end
describe '#active?' do
it 'is truthy when started and not ended' do
message = build(:broadcast_message)
expect(message).to be_active
end
it 'is falsey when ended' do
message = build(:broadcast_message, :expired)
expect(message).not_to be_active
end
it 'is falsey when not started' do
message = build(:broadcast_message, :future)
expect(message).not_to be_active
end
end
describe '#started?' do
it 'is truthy when starts_at has passed' do
message = build(:broadcast_message)
travel_to(3.days.from_now) do
expect(message).to be_started
end
end
it 'is falsey when starts_at is in the future' do
message = build(:broadcast_message)
travel_to(3.days.ago) do
expect(message).not_to be_started
end
end
end
describe '#ended?' do
it 'is truthy when ends_at has passed' do
message = build(:broadcast_message)
travel_to(3.days.from_now) do
expect(message).to be_ended
end
end
it 'is falsey when ends_at is in the future' do
message = build(:broadcast_message)
travel_to(3.days.ago) do
expect(message).not_to be_ended
end
end
end
describe '#status' do
it 'returns Active' do
message = build(:broadcast_message)
expect(message.status).to eq 'Active'
end
it 'returns Expired' do
message = build(:broadcast_message, :expired)
expect(message.status).to eq 'Expired'
end
it 'returns Pending' do
message = build(:broadcast_message, :future)
expect(message.status).to eq 'Pending'
end
end
end end
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