Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display total months as years + months #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
17 changes: 17 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/views/category/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<ul>
<% @category_posts.each do |post| %>
<li>
<%= post.inability %>, <%= post.months_experience %> months later.
<%= post.inability %>, <%= humanize_months(post.months_experience) %> later.
</li>
<% end %>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<h3>All Posts</h3>
<ul>
<% @posts.each do |post| %>
<li>Someone in the <strong><%= link_to "#{post.category.name.titleize}", category_path(post.category) %></strong> field for <strong><%= post.months_experience %> months</strong> still can't <em><%= link_to post.inability, post_path(post) %></em>.</li>
<li>Someone in the <strong><%= link_to "#{post.category.name.titleize}", category_path(post.category) %></strong> field for <strong><%= humanize_months(post.months_experience) %></strong> still can't <em><%= link_to post.inability, post_path(post) %></em>.</li>

<% end %>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= link_to "< All Posts", posts_path %>
<h2>"I'm in the <strong><%= link_to "#{@post.category.name.titleize}", category_path(@post.category) %></strong> field for <strong><%= @post.months_experience %> months</strong> and I still can't <em><%= @post.inability %></em>.</li>"</h2>
<h2>"I'm in the <strong><%= link_to "#{@post.category.name.titleize}", category_path(@post.category) %></strong> field for <strong><%= humanize_months(@post.months_experience) %></strong> and I still can't <em><%= @post.inability %></em>.</li>"</h2>


<div id="disqus_thread" style="margin-top: 200px;"></div>
Expand Down