diff --git a/app/models/user.rb b/app/models/user.rb index 44c3e815..74554f9a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -54,14 +54,8 @@ class User < ActiveRecord::Base }, presence: true - ## - # 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 + validate :check_biography_length + def attended_event? event event_registration = event.events_registrations.find_by(registration: self.registrations) @@ -192,4 +186,13 @@ class User < ActiveRecord::Base self.is_admin = true 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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index fd14ddbb..a6597104 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -71,6 +71,31 @@ describe User do 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 context 'user has subscribed to conference' do before { create(:subscription, user: user, conference: conference) }