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

Fix relative time indication for submission history #4498

Merged
merged 2 commits into from
Mar 21, 2023
Merged
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
10 changes: 8 additions & 2 deletions app/assets/javascripts/i18n/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@
"with": "with"
},
"time": {
"ago": "%{time} ago",
"am": "am",
"formats": {
"annotation": "MMMM DD, YYYY HH:mm",
Expand All @@ -445,13 +446,15 @@
"us": "%m/%d/%Y %I:%M %p"
},
"pm": "pm",
"today": "today",
"units": {
"day": "%{smart_count} day |||| %{smart_count} days",
"hour": "%{smart_count} hour |||| %{smart_count} hours",
"min": "%{smart_count} minute |||| %{smart_count} minutes",
"sec": "%{smart_count} second |||| %{smart_count} seconds",
"week": "%{smart_count} week |||| %{smart_count} weeks"
}
},
"yesterday": "yesterday"
}
},
"nl": {
Expand Down Expand Up @@ -884,6 +887,7 @@
"with": "met"
},
"time": {
"ago": "%{time} geleden",
"am": "'s ochtends",
"formats": {
"annotation": "D MMMM YYYY, HH:mm",
Expand All @@ -899,13 +903,15 @@
"submission": "%d %B %Y %H:%M:%S"
},
"pm": "'s middags",
"today": "vandaag",
"units": {
"day": "%{smart_count} dag |||| %{smart_count} dagen",
"hour": "%{smart_count} uur |||| %{smart_count} uren",
"min": "%{smart_count} minuut |||| %{smart_count} minuten",
"sec": "%{smart_count} seconde |||| %{smart_count} seconden",
"week": "%{smart_count} week |||| %{smart_count} weken"
}
},
"yesterday": "gisteren"
}
}
}
3 changes: 2 additions & 1 deletion app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class SubmissionsController < ApplicationController
include SeriesHelper
include TimeHelper
include ActionView::Helpers::DateHelper

before_action :set_submission, only: %i[show download evaluate edit media]
Expand Down Expand Up @@ -83,7 +84,7 @@ def show
@submissions_time_stamps = []
prev = nil
@submissions.each do |s|
current = s.created_at.before?(1.day.ago) ? "#{time_ago_in_words(s.created_at)} #{t '.ago'}" : (t '.today')
current = days_ago_in_words(s.created_at)
if current == prev
@submissions_time_stamps.push nil
else
Expand Down
13 changes: 13 additions & 0 deletions app/helpers/time_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module TimeHelper
include ActionView::Helpers::DateHelper
def days_ago_in_words(time)
if time.today?
t 'time.today'
elsif time.yesterday?
t 'time.yesterday'
else
# need to use I18n.t here because the minitest helper dos not support kwargs yet for t
I18n.t 'time.ago', time: time_ago_in_words(time)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made a pr on minitest-utils to fix the need for this: fnando/minitest-utils#15

end
end
end
3 changes: 3 additions & 0 deletions config/locales/defaults/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ en:
read_state: '%B %d, %Y %H:%M:%S'
question: '%B %d, %Y %H:%M:%S'
plain_time: '%H:%M'
yesterday: "yesterday"
today: "today"
ago: "%{time} ago"
controllers:
created: "%{model} was successfully created."
updated: "%{model} was successfully updated."
Expand Down
3 changes: 3 additions & 0 deletions config/locales/defaults/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ nl:
read_state: '%d %B %Y %H:%M:%S'
question: '%d %B %Y %H:%M:%S'
plain_time: '%H:%M'
yesterday: "gisteren"
today: "vandaag"
ago: "%{time} geleden"
controllers:
created: "%{model} werd succesvol aangemaakt."
updated: "%{model} werd succesvol aangepast."
Expand Down
1 change: 0 additions & 1 deletion config/locales/views/submissions/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ en:
course: Course
handed_in: Handed in
ago: ago
today: today
status: Status
result: Result
code: Code
Expand Down
1 change: 0 additions & 1 deletion config/locales/views/submissions/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ nl:
course: Cursus
handed_in: Ingediend
ago: geleden
today: vandaag
status: Status
result: Resultaat
code: Code
Expand Down
14 changes: 14 additions & 0 deletions test/helpers/time_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'test_helper'

class TimeHelperTest < ActiveSupport::TestCase
include TimeHelper

test 'days ago in words' do
I18n.with_locale(:en) do
time = Time.zone.now
assert_equal 'today', days_ago_in_words(time)
assert_equal 'yesterday', days_ago_in_words(1.day.ago)
assert_equal '2 days ago', days_ago_in_words(2.days.ago)
end
end
end