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 44c3e815..3586834e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index fd14ddbb..88bada1f 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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