mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-10-01 17:34:59 +08:00
Merge pull request #3370 from okriuchykhin/ok_SCI_5785
Fix date formatting for the API [SCI-5785]
This commit is contained in:
commit
a3312d22ad
7 changed files with 43 additions and 28 deletions
|
@ -5,7 +5,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_date_format, if: :user_signed_in?
|
||||
around_action :set_time_zone, if: :current_user
|
||||
layout 'main'
|
||||
|
||||
|
@ -102,7 +102,9 @@ class ApplicationController < ActionController::Base
|
|||
end
|
||||
|
||||
def set_date_format
|
||||
I18n.backend.date_format =
|
||||
current_user.settings[:date_format] || Constants::DEFAULT_DATE_FORMAT
|
||||
I18n.backend.date_format = current_user.settings[:date_format]
|
||||
yield
|
||||
ensure
|
||||
I18n.backend.date_format = nil
|
||||
end
|
||||
end
|
||||
|
|
|
@ -37,7 +37,7 @@ module Reports
|
|||
report = Report.find(report_id)
|
||||
file = Tempfile.new(['report', '.docx'])
|
||||
begin
|
||||
I18n.backend.date_format = user.settings[:date_format] || Constants::DEFAULT_DATE_FORMAT
|
||||
I18n.backend.date_format = user.settings[:date_format]
|
||||
docx = Caracal::Document.new(file.path)
|
||||
Reports::Docx.new(report, docx, user: user, scinote_url: root_url).draw
|
||||
docx.save
|
||||
|
@ -54,7 +54,7 @@ module Reports
|
|||
)
|
||||
notification.create_user_notification(user)
|
||||
ensure
|
||||
I18n.backend.date_format = Constants::DEFAULT_DATE_FORMAT
|
||||
I18n.backend.date_format = nil
|
||||
file.close
|
||||
file.unlink
|
||||
end
|
||||
|
|
|
@ -51,7 +51,7 @@ module Reports
|
|||
|
||||
raise StandardError, 'Report template not found!' if template.blank?
|
||||
|
||||
I18n.backend.date_format = user.settings[:date_format] || Constants::DEFAULT_DATE_FORMAT
|
||||
I18n.backend.date_format = user.settings[:date_format]
|
||||
ActionController::Renderer::RACK_KEY_TRANSLATION['warden'] ||= 'warden'
|
||||
proxy = Warden::Proxy.new({}, Warden::Manager.new({}))
|
||||
proxy.set_user(user, scope: :user, store: false)
|
||||
|
@ -91,7 +91,7 @@ module Reports
|
|||
)
|
||||
notification.create_user_notification(user)
|
||||
ensure
|
||||
I18n.backend.date_format = Constants::DEFAULT_DATE_FORMAT
|
||||
I18n.backend.date_format = nil
|
||||
file.close(true)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -9,7 +9,7 @@ module Api
|
|||
belongs_to :project_folder, serializer: ProjectFolderSerializer
|
||||
|
||||
def start_date
|
||||
I18n.l(object.created_at, format: :full)
|
||||
object.created_at
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,8 +3,18 @@
|
|||
class CustomI18nBackend < I18n::Backend::Simple
|
||||
attr_accessor :date_format
|
||||
|
||||
# Gets I18n configuration object.
|
||||
def date_format
|
||||
Thread.current[:i18n_date_format] ||= Constants::DEFAULT_DATE_FORMAT
|
||||
end
|
||||
|
||||
# Sets I18n configuration object.
|
||||
def date_format=(value)
|
||||
Thread.current[:i18n_date_format] = value
|
||||
end
|
||||
|
||||
def localize(locale, object, format = :default, options = {})
|
||||
options[:date_format] ||= @date_format || Constants::DEFAULT_DATE_FORMAT
|
||||
options[:date_format] = date_format
|
||||
super(locale, object, format, options)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -33,10 +33,11 @@ RSpec.describe 'Api::V1::ProjectsController', type: :request do
|
|||
headers: @valid_headers
|
||||
expect { hash_body = json }.not_to raise_exception
|
||||
expect(hash_body[:data]).to match(
|
||||
ActiveModelSerializers::SerializableResource
|
||||
.new(@teams.first.projects,
|
||||
each_serializer: Api::V1::ProjectSerializer)
|
||||
.as_json[:data]
|
||||
JSON.parse(
|
||||
ActiveModelSerializers::SerializableResource
|
||||
.new(@teams.first.projects, each_serializer: Api::V1::ProjectSerializer)
|
||||
.to_json
|
||||
)['data']
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -66,10 +67,11 @@ RSpec.describe 'Api::V1::ProjectsController', type: :request do
|
|||
headers: @valid_headers
|
||||
expect { hash_body = json }.not_to raise_exception
|
||||
expect(hash_body[:data]).to match(
|
||||
ActiveModelSerializers::SerializableResource
|
||||
.new(@teams.first.projects.first,
|
||||
serializer: Api::V1::ProjectSerializer)
|
||||
.as_json[:data]
|
||||
JSON.parse(
|
||||
ActiveModelSerializers::SerializableResource
|
||||
.new(@teams.first.projects.first, serializer: Api::V1::ProjectSerializer)
|
||||
.to_json
|
||||
)['data']
|
||||
)
|
||||
end
|
||||
|
||||
|
|
|
@ -20,10 +20,9 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
|
|||
last_modified_by: @user, project: @valid_project)
|
||||
@unaccessible_experiment = create(:experiment, created_by: @user,
|
||||
last_modified_by: @user, project: @unaccessible_project)
|
||||
|
||||
create_list(:my_module, 3, created_by: @user,
|
||||
create_list(:my_module, 3, :with_due_date, created_by: @user,
|
||||
last_modified_by: @user, experiment: @valid_experiment)
|
||||
create_list(:my_module, 3, created_by: @user,
|
||||
create_list(:my_module, 3, :with_due_date, created_by: @user,
|
||||
last_modified_by: @user, experiment: @unaccessible_experiment)
|
||||
|
||||
@valid_headers =
|
||||
|
@ -40,10 +39,11 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
|
|||
), headers: @valid_headers
|
||||
expect { hash_body = json }.not_to raise_exception
|
||||
expect(hash_body[:data]).to match(
|
||||
ActiveModelSerializers::SerializableResource
|
||||
.new(@valid_experiment.my_modules,
|
||||
each_serializer: Api::V1::TaskSerializer)
|
||||
.as_json[:data]
|
||||
JSON.parse(
|
||||
ActiveModelSerializers::SerializableResource
|
||||
.new(@valid_experiment.my_modules, each_serializer: Api::V1::TaskSerializer)
|
||||
.to_json
|
||||
)['data']
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -95,10 +95,11 @@ RSpec.describe 'Api::V1::TasksController', type: :request do
|
|||
), headers: @valid_headers
|
||||
expect { hash_body = json }.not_to raise_exception
|
||||
expect(hash_body[:data]).to match(
|
||||
ActiveModelSerializers::SerializableResource
|
||||
.new(@valid_experiment.my_modules.first,
|
||||
serializer: Api::V1::TaskSerializer)
|
||||
.as_json[:data]
|
||||
JSON.parse(
|
||||
ActiveModelSerializers::SerializableResource
|
||||
.new(@valid_experiment.my_modules.first, serializer: Api::V1::TaskSerializer)
|
||||
.to_json
|
||||
)['data']
|
||||
)
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue