From 22916971c79745a878286b35d35f209a3d288838 Mon Sep 17 00:00:00 2001 From: Ana Date: Wed, 27 Apr 2016 00:21:39 +0200 Subject: [PATCH] Word limit on user biography changes --- app/assets/javascripts/osem.js | 12 ++++++++++++ app/models/user.rb | 8 ++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/osem.js b/app/assets/javascripts/osem.js index 08c8e743..aede8208 100644 --- a/app/assets/javascripts/osem.js +++ b/app/assets/javascripts/osem.js @@ -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); } ); diff --git a/app/models/user.rb b/app/models/user.rb index 74554f9a..c595a563 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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