Word limit on user biography

This commit is contained in:
Ana 2016-04-25 01:44:48 +02:00
parent 5770920270
commit 5c044a801d
2 changed files with 36 additions and 8 deletions

View file

@ -54,14 +54,8 @@ class User < ActiveRecord::Base
}, },
presence: true presence: true
## validate :check_biography_length
# Checkes if the user attended the event
# This is used for events that require registration
# The user must have registered to attend the event
# Gets an event
# === Returns
# * +true+ if the user attended the event
# * +false+ if the user did not attend the event
def attended_event? event def attended_event? event
event_registration = event.events_registrations.find_by(registration: self.registrations) event_registration = event.events_registrations.find_by(registration: self.registrations)
@ -192,4 +186,13 @@ class User < ActiveRecord::Base
self.is_admin = true self.is_admin = true
end end
end end
##
# Check if biography has an allowed number of words. Used as validation.
#
def check_biography_length
if self.biography.present?
errors.add(:biography, 'is limited to 150 words.') unless self.biography.split.length <= 150
end
end
end end

View file

@ -71,6 +71,31 @@ describe User do
end end
end end
describe '#check_biography_length' do
it 'biography can not have more than 150 words' do
# Text with 151 words
long_text = <<-EOS
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean
vestibulum, augue ut accumsan feugiat, mauris eros accumsan nunc,
volutpat vulputate eros orci quis nulla. Cum sociis natoque penatibus
et magnis dis parturient montes, nascetur ridiculus mus. Sed varius
orci ut lectus convallis, et ultrices ex finibus. Praesent orci augue,
aliquet at cursus at, placerat id ligula. Vestibulum a mauris non
felis pretium laoreet. Cras vel nisl convallis, pharetra ipsum at,
mattis erat. Praesent in lectus felis. Fusce eros mauris, euismod
lobortis metus id, tristique scelerisque nisl. Suspendisse potenti.
Suspendisse ac metus magna. Integer lobortis pharetra eros euismod
fringilla. Phasellus vitae orci vel magna laoreet mattis non eu neque.
Mauris ac dictum leo. Nullam dapibus convallis molestie. Integer
dignissim massa at odio feugiat tempus. Pellentesque ultrices rutrum
eros, a pellentesque lorem auctor in. Suspendisse sollicitudin dolor
vitae justo dignissim, a condimentum turpis molestie. Aenean
scelerisque, arcu eu congue mollis, nibh nulla finibus.
EOS
expect{create(:user, biography: long_text)}.to_not change { User.count }
end
end
describe '#subscribed?' do describe '#subscribed?' do
context 'user has subscribed to conference' do context 'user has subscribed to conference' do
before { create(:subscription, user: user, conference: conference) } before { create(:subscription, user: user, conference: conference) }