diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 8e6ba70..d24fe55 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -31,12 +31,33 @@ $(document).ready(function() { inabilityHints[Math.floor(Math.random()*inabilityHints.length)] ); + function humanizeMonths(months) { + months = Number(months); + if (months === 0) { + return "less then a month"; + } + var years = Math.floor(months / 12); + var remainder = months % 12; + var result = []; + if (years > 1) { + result += years + " years"; + } else if (years > 0) { + result += "1 year"; + } + if (remainder > 1) { + result += remainder + " months"; + } else if (remainder > 0) { + result += "1 month"; + } + return result.join(" "); + } + $(".new-submission form input[type=submit]").click(function(event) { event.preventDefault(); var field = $(".new-submission input[name='post[category]']").val(); - var months = $(".new-submission input[name='post[months_experience]']").val(); + var months = Number($(".new-submission input[name='post[months_experience]']").val()); var inability = $(".new-submission input[name='post[inability]']").val(); - var post = "Someone in the " + field + " field for " + months + " months still can't " + inability; + var post = "Someone in the " + field + " field for " + humanizeMonths(months) + " still can't " + inability; if(confirm("Your post will appear like this:\n'" + post + "'.\nOk? Or do you need to fix it?")) { $(".new-submission form").submit(); diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be79..83ce5c7 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,2 +1,19 @@ module ApplicationHelper + def humanize_months(months) + return "less then a month" if months == 0 + years = (months / 12).floor + remainder = months % 12 + result = [] + if years > 1 + result << "#{years} years" + elsif years > 0 + result << "1 year"; + end + if remainder > 1 + result << "#{remainder} months" + elsif remainder > 0 + result << "1 month" + end + result.join(" ") + end end diff --git a/app/views/category/show.html.erb b/app/views/category/show.html.erb index 1265c79..5741bdf 100644 --- a/app/views/category/show.html.erb +++ b/app/views/category/show.html.erb @@ -3,7 +3,7 @@