Skip to content

Commit

Permalink
Merge pull request #136 from GQuirino/translate-dates-on-workers-table
Browse files Browse the repository at this point in the history
Improve translations for pt-br dates
  • Loading branch information
wenderjean authored Aug 9, 2019
2 parents 5de955f + 0d58929 commit 5394d4c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/sidekiq/statistic/locales/pt-br.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ pt-br: # <---- change this to your locale code
LastRun: Última execução
EmptyLogs: Nenhum resultado
SearchLogs: Pesquisar no arquivo de logs
date:
formats:
default: "%d/%m/%Y"
datetime: "%d/%m/%Y - %T"
2 changes: 1 addition & 1 deletion lib/sidekiq/statistic/views/statistic.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<td class="worker">
<a href="<%= root_path %>statistic/<%= worker[:name] %>"><%= worker[:name] %></a>
</td>
<td class="worker"><%= format_date worker[:runtime][:last] %></td>
<td class="worker"><%= format_date worker[:runtime][:last], 'datetime' %></td>
<td class="worker"><%= worker[:queue] %></td>
<td class="worker"><%= worker[:number_of_calls][:success] %></td>
<td class="worker"><%= worker[:number_of_calls][:failure] %></td>
Expand Down
4 changes: 2 additions & 2 deletions lib/sidekiq/statistic/views/worker.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
</thead>
<% @worker_statistic.each do |worker| %>
<tr>
<td class="worker"><%= format_date worker[:date], '%e %B %Y' %></td>
<td class="worker"><%= format_date worker[:runtime][:last] %></td>
<td class="worker"><%= format_date worker[:date] %></td>
<td class="worker"><%= format_date worker[:runtime][:last], 'datetime' %></td>
<td class="worker"><%= worker[:success] %></td>
<td class="worker"><%= worker[:failure] %></td>
<td class="worker"><%= worker[:total] %></td>
Expand Down
8 changes: 6 additions & 2 deletions lib/sidekiq/statistic/web_extension_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module WebExtensionHelper

def format_date(date_to_format, format = nil)
time = date_to_format ? convert_to_date_object(date_to_format) : Time.now
time.strftime(format || '%T, %e %B %Y')
time.strftime(date_format(format))
end

def calculate_date_range(params)
Expand All @@ -24,9 +24,13 @@ def calculate_date_range(params)

private

def date_format(format)
get_locale.dig('date', 'formats', format || 'default') || '%m/%d/%Y'
end

def convert_to_date_object(date)
date.is_a?(String) ? Time.parse(date) : Time.at(date)
end
end
end
end
end
41 changes: 41 additions & 0 deletions test/test_sidekiq/web_extension_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

# encoding: utf-8

require 'minitest_helper'

module Sidekiq
class Helper < Sidekiq::WebAction
include Sidekiq::Statistic::WebExtensionHelper
end

describe 'WebExtensionHelper' do
include Rack::Test::Methods

describe '.format_date' do
let(:header) { { 'HTTP_ACCEPT_LANGUAGE' => 'pt-br' } }
let(:datetime) { Time.now }
let(:helper) { Helper.new(header, {}) }

describe "when doesn't have translation" do
before { header['HTTP_ACCEPT_LANGUAGE'] = 'xx-xx' }

it 'return date with en format' do
assert_equal helper.format_date(datetime), datetime.strftime('%m/%d/%Y')
end
end

describe 'when have translation' do
it 'return date with default format' do
default_format = helper.get_locale.dig('date', 'formats', 'default')
assert_equal helper.format_date(datetime), datetime.strftime(default_format)
end

it 'return date with datetime format' do
datetime_format = helper.get_locale.dig('date', 'formats', 'datetime')
assert_equal helper.format_date(datetime, 'datetime'), datetime.strftime(datetime_format)
end
end
end
end
end

0 comments on commit 5394d4c

Please sign in to comment.