Add time format selection to settings [SCI-2778]

This commit is contained in:
Oleksii Kriuchykhin 2018-11-09 15:58:08 +01:00
parent 039faf2964
commit 8d0dbc7076
26 changed files with 139 additions and 31 deletions

View file

@ -628,7 +628,7 @@ DEPENDENCIES
yomu
RUBY VERSION
ruby 2.4.3p205
ruby 2.4.4p296
BUNDLED WITH
1.16.3

View file

@ -34,12 +34,12 @@ class ActivitiesController < ApplicationController
end
# send last activity date of the previus batch
previous_activity = Activity.find_by_id(params[:last_activity])
previus_date = previous_activity.created_at.to_date if previous_activity
previus_date = previous_activity.created_at if previous_activity
{
activities: activities,
more_activities_url: more_url,
page: page,
previous_activity_created_at: previus_date
previous_activity_created_at: I18n.l(previus_date, format: :full_date)
}
end
end

View file

@ -6,6 +6,7 @@ class ApplicationController < ActionController::Base
before_action :authenticate_user!
helper_method :current_team
before_action :update_current_team, if: :user_signed_in?
before_action :set_date_format, if: :user_signed_in?
around_action :set_time_zone, if: :current_user
layout 'main'
@ -81,4 +82,9 @@ class ApplicationController < ActionController::Base
def set_time_zone(&block)
Time.use_zone(current_user.settings[:time_zone], &block)
end
def set_date_format
I18n.backend.date_format =
current_user.settings[:date_format] || Constants::DEFAULT_DATE_FORMAT
end
end

View file

@ -75,7 +75,7 @@ class MyModuleCommentsController < ApplicationController
comment: @comment
}
),
date: @comment.created_at.strftime('%d.%m.%Y'),
date: I18n.l(@comment.created_at, format: :full_date),
linked_id: @my_module.id, # Used for counter badge
counter: @my_module.task_comments.count # Used for counter badge
},

View file

@ -144,7 +144,13 @@ class MyModulesController < ApplicationController
end
def update
@my_module.assign_attributes(my_module_params)
update_params = my_module_params
if update_params[:due_date].present?
update_params[:due_date] = Time.strptime(
update_params[:due_date], I18n.backend.date_format.dup.delete('-')
)
end
@my_module.assign_attributes(update_params)
@my_module.last_modified_by = current_user
description_changed = @my_module.description_changed?

View file

@ -71,7 +71,7 @@ class ProjectCommentsController < ApplicationController
comment: @comment
}
),
date: @comment.created_at.strftime('%d.%m.%Y'),
date: I18n.l(@comment.created_at, format: :full_date),
linked_id: @project.id,
counter: @project.project_comments.count
}, status: :created

View file

@ -71,7 +71,7 @@ class ResultCommentsController < ApplicationController
comment: @comment
}
),
date: @comment.created_at.strftime('%d.%m.%Y')
date: I18n.l(@comment.created_at, format: :full_date)
},
status: :created
}

View file

@ -73,7 +73,7 @@ class StepCommentsController < ApplicationController
comment: @comment
}
),
date: @comment.created_at.strftime('%d.%m.%Y')
date: I18n.l(@comment.created_at, format: :full_date)
},
status: :created
}

View file

@ -68,9 +68,7 @@ module Users
end
def update_params
params.require(:user).permit(
:time_zone
)
params.require(:user).permit(:time_zone, :date_format)
end
end
end

View file

@ -37,4 +37,17 @@ class CustomDatatable < AjaxDatatablesRails::Base
end
@sortable_displayed_columns
end
def formated_date
f_date = I18n.backend.date_format.dup
f_date.gsub!(/%-d/, 'FMDD')
f_date.gsub!(/%d/, 'DD')
f_date.gsub!(/%-m/, 'FMMM')
f_date.gsub!(/%m/, 'MM')
f_date.gsub!(/%b/, 'Mon')
f_date.gsub!(/%B/, 'Month')
f_date.gsub!('%Y', 'YYYY')
f_date += ' HH24:MI'
f_date
end
end

View file

@ -36,7 +36,6 @@ class LoadFromRepositoryProtocolsDatatable < CustomDatatable
def new_search_condition(column, value)
model, column = column.split('.')
model = model.constantize
formated_date = (I18n.t 'time.formats.datatables_date').gsub!(/^\"|\"?$/, '')
case column
when 'published_on'
casted_column = ::Arel::Nodes::NamedFunction.new('CAST',

View file

@ -58,7 +58,6 @@ class ProtocolsDatatable < CustomDatatable
def new_search_condition(column, value)
model, column = column.split('.')
model = model.constantize
formated_date = (I18n.t 'time.formats.datatables_date').gsub!(/^\"|\"?$/, '')
case column
when 'published_on'
casted_column = ::Arel::Nodes::NamedFunction.new('CAST',

View file

@ -36,8 +36,6 @@ class TeamUsersDatatable < CustomDatatable
def new_search_condition(column, value)
model, column = column.split('.')
model = model.constantize
formated_date = (I18n.t 'time.formats.datatables_date')
.gsub!(/^\"|\"?$/, '')
if column == 'created_at'
casted_column = ::Arel::Nodes::NamedFunction.new(
'CAST',

View file

@ -158,7 +158,7 @@ module ApplicationHelper
#{I18n.t('atwho.users.popover_html',
role: user_t.role.capitalize,
team: user_t.team.name,
time: user_t.created_at.strftime('%B %Y'))}
time: I18n.l(user_t.created_at, format: :full_date))}
</p></div></div></div>)
else
user_description += %(<p></p></div></div></div>)

View file

@ -24,7 +24,14 @@ module BootstrapFormHelper
input_name = "#{@object_name}[#{name.to_s}]"
timestamp = @object[name] ? "#{@object[name].to_i}000" : ""
js_locale = I18n.locale.to_s
js_format = I18n.t("time.formats.full_js")
js_format = I18n.backend.date_format.dup
js_format.gsub!(/%-d/, 'D')
js_format.gsub!(/%d/, 'DD')
js_format.gsub!(/%-m/, 'M')
js_format.gsub!(/%m/, 'MM')
js_format.gsub!(/%b/, 'MMM')
js_format.gsub!(/%B/, 'MMMM')
js_format.gsub!('%Y', 'YYYY')
label = name.to_s.humanize
if options[:label] then

View file

@ -41,6 +41,7 @@ class User < ApplicationRecord
default_settings(
time_zone: 'UTC',
date_format: Constants::DEFAULT_DATE_FORMAT,
notifications_settings: {
assignments: true,
assignments_email: false,
@ -233,6 +234,18 @@ class User < ApplicationRecord
@avatar_remote_url = url_value
end
def date_format
settings[:date_format] || Constants::DEFAULT_DATE_FORMAT
end
def date_format=(date_format)
return if settings[:date_format] == date_format
if Constants::SUPPORTED_DATE_FORMATS.include?(date_format)
settings[:date_format] = date_format
clear_view_cache
end
end
def current_team
Team.find_by_id(self.current_team_id)
end
@ -524,4 +537,8 @@ class User < ApplicationRecord
# Now, simply destroy all user notification relations left
user_notifications.destroy_all
end
def clear_view_cache
Rails.cache.delete_matched(%r{^views\/users\/#{id}-})
end
end

View file

@ -47,6 +47,8 @@ class ZipExport < ApplicationRecord
end
def generate_exportable_zip(user, data, type, options = {})
I18n.backend.date_format =
user.settings[:date_format] || Constants::DEFAULT_DATE_FORMAT
FileUtils.mkdir_p(File.join(Rails.root, 'tmp/zip-ready'))
dir_to_zip = FileUtils.mkdir_p(
File.join(Rails.root, "tmp/temp-zip-#{Time.now.to_i}")

View file

@ -0,0 +1,10 @@
# frozen_string_literal: true
class CustomI18nBackend < I18n::Backend::Simple
attr_accessor :date_format
def localize(locale, object, format = :default, options = {})
options[:date_format] ||= @date_format || Constants::DEFAULT_DATE_FORMAT
super(locale, object, format, options)
end
end

View file

@ -6,7 +6,7 @@
<% if comment_day <= current_day && comment_day > day %>
<% day = days_since_1970(comment.created_at) %>
<li class="comment-date-separator">
<p class="text-center"><%= comment.created_at.strftime('%d.%m.%Y') %></p>
<p class="text-center"><%= l(comment.created_at, format: :full_date) %></p>
</li>
<% end %>
<li class="comment">

View file

@ -5,7 +5,7 @@
<% if comment_day <= current_day && comment_day > day %>
<% day = days_since_1970(comment.created_at) %>
<li class="comment-date-separator">
<p class="text-center"><%= comment.created_at.strftime('%d.%m.%Y') %></p>
<p class="text-center"><%= l(comment.created_at, format: :full_date) %></p>
</li>
<% end %>
<li class="comment">

View file

@ -6,7 +6,7 @@
<% if comment_day <= current_day && comment_day > day %>
<% day = days_since_1970(comment.created_at) %>
<li class="comment-date-separator">
<p class="text-center"><%= comment.created_at.strftime('%d.%m.%Y') %></p>
<p class="text-center"><%= l(comment.created_at, format: :full_date) %></p>
</li>
<% end %>
<li class="comment">

View file

@ -6,7 +6,7 @@
<% if comment_day <= current_day && comment_day > day %>
<% day = days_since_1970(comment.created_at) %>
<li class="comment-date-separator">
<p class="text-center"><%= comment.created_at.strftime('%d.%m.%Y') %></p>
<p class="text-center"><%= l(comment.created_at, format: :full_date) %></p>
</li>
<% end %>
<li class="comment">

View file

@ -49,6 +49,48 @@
</div>
</div>
<% end %>
<%= form_for(@user,
url: update_preferences_path(format: :json),
remote: true,
html: {
method: :put,
'data-for' => 'date_format',
'data-turbolinks' => false
}) do |f| %>
<div data-part="view">
<div class="form-group">
<%= f.label t("users.settings.account.preferences.edit.date_format_label") %>
<div class="input-group" style="max-width: 500px;">
<input class="form-control"
disabled="disabled"
autocomplete="off"
type="text"
value="<%= l(Time.now, format: :full_date) %>"
name="fake_user[settings][time_zone]"
id="fake_user_settings_date_format">
<span class="input-group-btn">
<a href="#" class="btn btn-default" data-action="edit"><%=t "general.edit" %></a>
</span>
</div>
<small><%= t("users.settings.account.preferences.edit.date_format_sublabel") %></small>
</div>
</div>
<div data-part="edit" style="display: none;">
<div class="well">
<h4><%=t "users.settings.account.preferences.edit.date_format_title" %></h4>
<div class="form-group" style="max-width: 500px;">
<%= f.select :date_format, Constants::SUPPORTED_DATE_FORMATS.collect { |df|
["#{l(Time.now, format: :full_date, date_format: df)}", df]
}, {}, { class: 'form-control selectpicker', 'data-role': 'clear' } %>
<small><%= t("users.settings.account.preferences.edit.date_format_sublabel") %></small>
</div>
<div>
<a href="#" class="btn btn-default" data-action="cancel"><%=t "general.cancel" %></a>
<%= f.submit t("general.save"), class: "btn btn-success" %>
</div>
</div>
</div>
<% end %>
<hr>
<div class="notification-settings-container">
<h4><%= t('notifications.title') %></h4>

View file

@ -115,7 +115,18 @@ class Constants
# URL); it expires in exactly one day
URL_LONG_EXPIRE_TIME = 86_400
DATE_FORMAT = '%d.%m.%Y %H:%M'.freeze
DEFAULT_DATE_FORMAT = '%m/%d/%Y'.freeze
SUPPORTED_DATE_FORMATS = [
# US formats
'%m/%d/%Y', '%m.%d.%Y', '%m. %d. %Y', '%m-%d-%Y', '%-m/%-d/%Y',
'%-m.%-d.%Y', '%-m. %-d. %Y', '%-m-%-d-%Y',
# European formats
'%d/%m/%Y', '%d.%m.%Y', '%d. %m. %Y', '%d-%b-%Y', '%Y-%m-%d',
'%d.%b.%Y', '%Y/%b/%d', '%d, %B, %Y', '%B, %d, %Y', '%-d/%-m/%Y',
'%-d.%-m.%Y', '%-d. %-m. %Y', '%d-%m-%Y', '%Y-%-m-%-d', '%-d-%b-%Y',
'%Y-%b-%-d', '%-d, %B, %Y', '%B, %d, %Y'
].freeze
#=============================================================================
# Application colors

View file

@ -0,0 +1,3 @@
# frozen_string_literal: true
I18n.backend = CustomI18nBackend.new

View file

@ -1437,6 +1437,9 @@ en:
time_zone_label: "Time zone"
time_zone_sublabel: "Time zone setting affects all time & date fields throughout application."
time_zone_title: "Time zone"
date_format_label: "Date format"
date_format_sublabel: "Date format setting affects all date display throughout application."
date_format_title: "Date format"
update_flash: "Preferences successfully updated."
addons:
head_title: "Settings | Add-ons"
@ -1810,16 +1813,10 @@ en:
time:
formats:
full: "%d.%m.%Y %H:%M"
# This format is used for JS datetimepicker,
# only in different notation;
# it should be the same as the above full format
full_js: "D.M.YYYY HH:mm"
# This format is used only in datatables
# to format the timestamp in SQL so it can
# be easily searched by input param
datatables_date: 'DD.MM.YYYY HH24:MI'
full_date: "%d.%m.%Y"
full: "%{date_format} %H:%M"
full_with_tz: "%{date_format} %H:%M %z"
full_date: "%{date_format}"
time: "%H:%M"
short: "%H"