Merge pull request #968 from Ana06/biography

Word limit on user biography
This commit is contained in:
Henne Vogelsang 2016-04-27 10:02:28 +02:00
commit 5b9da3ef22
3 changed files with 46 additions and 0 deletions

View file

@ -1,5 +1,17 @@
$(function () {
/**
* Show the number of words in the biography text field when opening the
* profile edit page
*/
$(document).ready(function(){
word_count($("#user_biography")[0], 'bio_length', 150);
});
/**
* Update the number of words in the biography text field every time the user
* releases a key on the keyboard
*/
$("#user_biography").bind('keyup', function() {
word_count(this, 'bio_length', 150);
} );

View file

@ -54,6 +54,8 @@ class User < ActiveRecord::Base
},
presence: true
validate :biography_limit
##
# Checkes if the user attended the event
# This is used for events that require registration
@ -192,4 +194,13 @@ class User < ActiveRecord::Base
self.is_admin = true
end
end
##
# Check if biography has an allowed number of words. Used as validation.
#
def biography_limit
if self.biography.present?
errors.add(:biography, 'is limited to 150 words.') if self.biography.split.length > 150
end
end
end

View file

@ -23,6 +23,29 @@ describe User do
it { is_expected.to validate_presence_of(:email) }
it { is_expected.to validate_presence_of(:username) }
it { is_expected.to validate_uniqueness_of(:username) }
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(build(:user, biography: long_text)).to_not be_valid
end
end
describe 'association' do