From 9f3da955f0a605a6f44a9935431a206ce5ee31fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Geuken?= Date: Fri, 17 Apr 2015 15:33:57 +0200 Subject: [PATCH 1/5] Move default value into else branch and remove superfluous variable --- app/models/event.rb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/app/models/event.rb b/app/models/event.rb index a325b290..22689fe5 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -167,23 +167,22 @@ class Event < ActiveRecord::Base end def self.get_state_color(state) - # default azure - result = '#00FFFF' case state when 'new' # blue - result = '#0000FF' + '#0000FF' when 'withdrawn' # orange - result = '#FF8000' + '#FF8000' when 'confirmed' # green - result = '#00FF00' + '#00FF00' when 'unconfirmed' # yellow - result = '#FFFF00' + '#FFFF00' when 'rejected' # red - result = '#FF0000' + '#FF0000' when 'canceled' # grey - result = '#848484' + '#848484' + else + '#00FFFF' # azure end - result end def update_state(transition, mail = false, subject = false, send_mail = false, send_mail_param) From 80f8bba951e4bf2c4b1125e7b58bdfd83e4c38a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Geuken?= Date: Fri, 17 Apr 2015 15:37:21 +0200 Subject: [PATCH 2/5] Refactor abstract_word_count method --- app/models/event.rb | 6 +----- spec/models/event_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 spec/models/event_spec.rb diff --git a/app/models/event.rb b/app/models/event.rb index 22689fe5..99d13287 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -155,11 +155,7 @@ class Event < ActiveRecord::Base end def abstract_word_count - if abstract.nil? - 0 - else - abstract.split.size - end + abstract.to_s.split.size end def week diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb new file mode 100644 index 00000000..b777308e --- /dev/null +++ b/spec/models/event_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe Event do + + describe 'abstract_word_count' do + it 'counts words in abstract' do + event = build(:event) + expect(event.abstract_word_count).to eq(233) + event.update_attributes!(abstract: "abstract.") + expect(event.abstract_word_count).to eq(1) + end + + it 'counts 0 when abstract is empty' do + event = build(:event, abstract: nil) + expect(event.abstract_word_count).to eq(0) + event.abstract = "" + expect(event.abstract_word_count).to eq(0) + end + end +end From 82edf7870a33cfb7647e6671941cb9fd3e376dea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Geuken?= Date: Fri, 17 Apr 2015 16:20:39 +0200 Subject: [PATCH 3/5] event: refactor as_json method and add a test --- app/models/event.rb | 20 +++----------------- spec/models/event_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/app/models/event.rb b/app/models/event.rb index 99d13287..e5c69c54 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -99,23 +99,9 @@ class Event < ActiveRecord::Base def as_json(options) json = super(options) - if room.nil? - json[:room_guid] = nil - else - 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[:room_guid] = room.try(:guid) + json[:track_color] = track.try(:color) || '#ffffff' + json[:length] = event_type.try(:length) || 25 json end diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb index b777308e..33b59c81 100644 --- a/spec/models/event_spec.rb +++ b/spec/models/event_spec.rb @@ -17,4 +17,27 @@ describe Event do expect(event.abstract_word_count).to eq(0) 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 From 8250e1305ead8d0ad9134eddb2ef72909b37f4ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Geuken?= Date: Fri, 17 Apr 2015 16:33:30 +0200 Subject: [PATCH 4/5] event: refactor get_state_color method --- app/models/event.rb | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/app/models/event.rb b/app/models/event.rb index e5c69c54..f8c07ade 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -149,22 +149,16 @@ class Event < ActiveRecord::Base end def self.get_state_color(state) - case state - when 'new' # blue - '#0000FF' - when 'withdrawn' # orange - '#FF8000' - when 'confirmed' # green - '#00FF00' - when 'unconfirmed' # yellow - '#FFFF00' - when 'rejected' # red - '#FF0000' - when 'canceled' # grey - '#848484' - else - '#00FFFF' # azure - end + color = { + new: '#0000FF', # blue + withdrawn: '#FF8000', # orange + confirmed: '#00FF00', # green + unconfirmed: '#FFFF00', # yellow + rejected: '#FF0000', # red + canceled: '#848484' # grey + }[state.to_sym] + + color || '#00FFFF' # azure end def update_state(transition, mail = false, subject = false, send_mail = false, send_mail_param) From 68bbdb9fc9a54484aa7894628641fb98ee0d707e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Geuken?= Date: Tue, 5 May 2015 14:04:26 +0200 Subject: [PATCH 5/5] Fix styling issue: " -> ' --- spec/models/event_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb index 33b59c81..1c0865aa 100644 --- a/spec/models/event_spec.rb +++ b/spec/models/event_spec.rb @@ -6,14 +6,14 @@ describe Event do it 'counts words in abstract' do event = build(:event) expect(event.abstract_word_count).to eq(233) - event.update_attributes!(abstract: "abstract.") + event.update_attributes!(abstract: 'abstract.') expect(event.abstract_word_count).to eq(1) end it 'counts 0 when abstract is empty' do event = build(:event, abstract: nil) expect(event.abstract_word_count).to eq(0) - event.abstract = "" + event.abstract = '' expect(event.abstract_word_count).to eq(0) end end