From a1b93f256dd6db22ef3c42dcd187b371c62b2b9f Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Thu, 1 Sep 2016 10:37:49 -0500 Subject: [PATCH] fix text length validation completes #64 --- app/assets/javascripts/textarea-limit.js | 44 +++++++++---------- app/validators/answer_format_validator.rb | 2 +- .../answer_format_validator/text_test.rb | 17 ++++++- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/app/assets/javascripts/textarea-limit.js b/app/assets/javascripts/textarea-limit.js index 6605f85..cf48d74 100644 --- a/app/assets/javascripts/textarea-limit.js +++ b/app/assets/javascripts/textarea-limit.js @@ -1,28 +1,24 @@ -$(document).ready(function() { - setTextAreaLimit(); - - function setTextAreaLimit() { - $.fn.extend({ - limiter: function(limit, elem) { - $('textarea').on("keyup focus show", function() { - setCount(this, elem); - }); - - function setCount(src, elem) { - if(src !== undefined) { - var chars = src.value.length; - if (chars > limit) { - src.value = src.value.substr(0, limit); - chars = limit; - } - elem.html(limit - chars); - } - } - setCount($(this)[0], elem); - } +$.fn.extend({ + characterLimiter: function(limit, $label) { + $(this).on("keyup focus show", function() { + setCount(this, $label); }); - var elem = $(".chars span"); - $('textarea').limiter(1000, elem); + // TODO: append label container after $this, instead of hard codeing + function setCount(src, $label) { + if(src !== undefined) { + var chars = src.value.length; + if (chars >= limit) { + src.value = src.value.substr(0, limit); + chars = limit; + } + $label.html(limit - chars); + } + } + setCount($(this)[0], $label); } }); + +$(document).ready(function() { + $('textarea').characterLimiter(1000, $(".chars span")); +}); diff --git a/app/validators/answer_format_validator.rb b/app/validators/answer_format_validator.rb index 8214b2b..99f7d49 100644 --- a/app/validators/answer_format_validator.rb +++ b/app/validators/answer_format_validator.rb @@ -6,7 +6,7 @@ class AnswerFormatValidator < ActiveModel::EachValidator private def text record, attribute, value - clean_val = value.to_s.strip + clean_val = value.to_s.strip.delete("\r") return if clean_val.length.between?(1, 1000) if clean_val.blank? diff --git a/test/validators/answer_format_validator/text_test.rb b/test/validators/answer_format_validator/text_test.rb index b0a8396..bd7e206 100644 --- a/test/validators/answer_format_validator/text_test.rb +++ b/test/validators/answer_format_validator/text_test.rb @@ -25,11 +25,24 @@ class AnswerFormatValidatorTest < ActiveSupport::TestCase assert_match(/enter.*answer/, obj.errors.messages[:answer][0]) end - test "text should FAIL with more than 1000 charactures" do + test "text should PASS with 999 character" do obj = AnswerValidatable.new('text') - obj.answer = SecureRandom.urlsafe_base64(1001) + obj.answer = long_string_with_returns + + assert obj.valid? + assert obj.errors.messages.empty? + end + + test "text should FAIL with more than 1000 character" do + obj = AnswerValidatable.new('text') + obj.answer = long_string_with_returns + " - to long now " refute obj.valid? assert_match(/char.*limit.*1000.$/, obj.errors.messages[:answer][0]) end + + def long_string_with_returns + # returns 999 chars, after \r is stripped. + "Some rando input\r\n\rYo. Making this up.\r\n" * 27 + end end