From 5c044a801d806cb1d56573d904ed6cdfa48bc5fd Mon Sep 17 00:00:00 2001 From: Ana Date: Mon, 25 Apr 2016 01:44:48 +0200 Subject: [PATCH 1/4] Word limit on user biography --- app/models/user.rb | 19 +++++++++++-------- spec/models/user_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) 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) } From e082e7599de82ffc374b66763f8f0b9a29a404ca Mon Sep 17 00:00:00 2001 From: Ana Date: Tue, 26 Apr 2016 00:21:05 +0200 Subject: [PATCH 2/4] biography test fixed --- spec/models/user_spec.rb | 48 +++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index a6597104..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 @@ -71,31 +94,6 @@ 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) } From 22916971c79745a878286b35d35f209a3d288838 Mon Sep 17 00:00:00 2001 From: Ana Date: Wed, 27 Apr 2016 00:21:39 +0200 Subject: [PATCH 3/4] 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 From a30893fae3050abe8edfde01e894357cc1d97eac Mon Sep 17 00:00:00 2001 From: Ana Date: Wed, 27 Apr 2016 00:41:40 +0200 Subject: [PATCH 4/4] test I removed accidentally while rebasing restored --- app/models/user.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index c595a563..3586834e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -56,6 +56,14 @@ class User < ActiveRecord::Base validate :biography_limit + ## + # 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 def attended_event? event event_registration = event.events_registrations.find_by(registration: self.registrations)