event: refactor as_json method and add a test
This commit is contained in:
parent
80f8bba951
commit
82edf7870a
2 changed files with 26 additions and 17 deletions
|
|
@ -99,23 +99,9 @@ class Event < ActiveRecord::Base
|
||||||
def as_json(options)
|
def as_json(options)
|
||||||
json = super(options)
|
json = super(options)
|
||||||
|
|
||||||
if room.nil?
|
json[:room_guid] = room.try(:guid)
|
||||||
json[:room_guid] = nil
|
json[:track_color] = track.try(:color) || '#ffffff'
|
||||||
else
|
json[:length] = event_type.try(:length) || 25
|
||||||
json[:room_guid] = room.guid
|
|
||||||
end
|
|
||||||
|
|
||||||
if track.nil?
|
|
||||||
json[:track_color] = '#ffffff'
|
|
||||||
else
|
|
||||||
json[:track_color] = track.color
|
|
||||||
end
|
|
||||||
|
|
||||||
if event_type.nil?
|
|
||||||
json[:length] = 25
|
|
||||||
else
|
|
||||||
json[:length] = event_type.length
|
|
||||||
end
|
|
||||||
|
|
||||||
json
|
json
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -17,4 +17,27 @@ describe Event do
|
||||||
expect(event.abstract_word_count).to eq(0)
|
expect(event.abstract_word_count).to eq(0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'as_json' do
|
||||||
|
let(:event) { create(:event) }
|
||||||
|
|
||||||
|
it 'adds the event\'s room_guid, track_color and length' do
|
||||||
|
event.room = create(:room)
|
||||||
|
event.track = create(:track, color: '#efefef')
|
||||||
|
json_hash = event.as_json(nil)
|
||||||
|
|
||||||
|
expect(json_hash[:room_guid]).to eq(event.room.guid)
|
||||||
|
expect(json_hash[:track_color]).to eq('#efefef')
|
||||||
|
expect(json_hash[:length]).to eq(30)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'uses correct default values for room_guid, track_color and length' do
|
||||||
|
event.event_type = nil
|
||||||
|
json_hash = event.as_json(nil)
|
||||||
|
|
||||||
|
expect(json_hash[:room_guid]).to be_nil
|
||||||
|
expect(json_hash[:track_color]).to eq('#ffffff')
|
||||||
|
expect(json_hash[:length]).to eq(25)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue