Refactor abstract_word_count method

This commit is contained in:
Björn Geuken 2015-04-17 15:37:21 +02:00
parent 9f3da955f0
commit 80f8bba951
2 changed files with 21 additions and 5 deletions

View file

@ -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

20
spec/models/event_spec.rb Normal file
View file

@ -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