Word limit on user biography changes

This commit is contained in:
Ana 2016-04-27 00:21:39 +02:00
parent e082e7599d
commit 22916971c7
2 changed files with 16 additions and 4 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,8 +54,8 @@ class User < ActiveRecord::Base
},
presence: true
validate :check_biography_length
validate :biography_limit
def attended_event? event
event_registration = event.events_registrations.find_by(registration: self.registrations)
@ -190,9 +190,9 @@ class User < ActiveRecord::Base
##
# Check if biography has an allowed number of words. Used as validation.
#
def check_biography_length
def biography_limit
if self.biography.present?
errors.add(:biography, 'is limited to 150 words.') unless self.biography.split.length <= 150
errors.add(:biography, 'is limited to 150 words.') if self.biography.split.length > 150
end
end
end