[feat]Implement FullCalendarFormatter#event_schedules_to_resources; Add tests

This commit is contained in:
Jimmy 2021-04-26 10:34:14 +08:00 committed by CactusPuppy
parent abe2ea9bb3
commit 4b83ae8be7
No known key found for this signature in database
GPG key ID: 4B33B0A3E15E2C82
2 changed files with 46 additions and 8 deletions

View file

@ -3,14 +3,28 @@ class FullCalendarFormatter
rooms.map { |room| room_to_resource(room) }.to_json rooms.map { |room| room_to_resource(room) }.to_json
end end
def self.event_schedules_to_resources(event_schedules); end def self.event_schedules_to_resources(event_schedules)
event_schedules.map { |event_schedule| event_schedule_to_resource(event_schedule) }.to_json
end
private_class_method class << self
private
def self.room_to_resource(room) def room_to_resource(room)
{ {
id: room.guid, id: room.guid,
title: room.name title: room.name
} }
end
def event_schedule_to_resource(event_schedule)
{
id: event_schedule.event.guid,
title: event_schedule.event.title,
start: event_schedule.start_time,
end: event_schedule.end_time,
source: event_schedule.room.guid
}
end
end end
end end

View file

@ -5,10 +5,13 @@ require 'spec_helper'
describe FullCalendarFormatter do describe FullCalendarFormatter do
let!(:room1) { create(:room) } let!(:room1) { create(:room) }
let!(:room2) { create(:room) } let!(:room2) { create(:room) }
let!(:rooms) { [room1, room2] }
let!(:event_schedule1) { create(:event_schedule, room: room1) }
let!(:event_schedule2) { create(:event_schedule, room: room2) }
let!(:event_schedules) { [event_schedule1, event_schedule2] }
describe 'JSON formatting for FullCalendar' do describe 'JSON formatting for FullCalendar' do
it 'translates rooms to resources' do it 'translates rooms to resources' do
rooms = [room1, room2]
resources = described_class.rooms_to_resources(rooms) resources = described_class.rooms_to_resources(rooms)
expected_json = [ expected_json = [
{ {
@ -22,5 +25,26 @@ describe FullCalendarFormatter do
].to_json ].to_json
expect(resources).to eq(expected_json) expect(resources).to eq(expected_json)
end end
it 'translates event schedules to resources' do
resources = described_class.event_schedules_to_resources(event_schedules)
expected_json = [
{
id: event_schedule1.event.guid,
title: event_schedule1.event.title,
start: event_schedule1.start_time,
end: event_schedule1.end_time,
source: room1.guid
},
{
id: event_schedule2.event.guid,
title: event_schedule2.event.title,
start: event_schedule2.start_time,
end: event_schedule2.end_time,
source: room2.guid
}
].to_json
expect(resources).to eq(expected_json)
end
end end
end end