Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

rely on api for configuration instead of gon

ulferts8d35a9b5f025
diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb
index 65a2ccb0884..c6d03c192d1 100644
--- a/app/controllers/forums_controller.rb
+++ b/app/controllers/forums_controller.rb
@@ -38,7 +38,6 @@ class ForumsController < ApplicationController
include SortHelper
include WatchersHelper
include PaginationHelper
- include OpenProject::ClientPreferenceExtractor
def index
@forums = @project.forums
diff --git a/app/controllers/timelog_controller.rb b/app/controllers/timelog_controller.rb
index ad8cc7d8f67..58489ce69ad 100644
--- a/app/controllers/timelog_controller.rb
+++ b/app/controllers/timelog_controller.rb
@@ -40,7 +40,6 @@ class TimelogController < ApplicationController
include CustomFieldsHelper
include PaginationHelper
include Concerns::Layout
- include OpenProject::ClientPreferenceExtractor
menu_item :time_entries
diff --git a/app/controllers/work_packages_controller.rb b/app/controllers/work_packages_controller.rb
index e86efd714d0..4f110e16602 100644
--- a/app/controllers/work_packages_controller.rb
+++ b/app/controllers/work_packages_controller.rb
@@ -31,19 +31,17 @@
class WorkPackagesController < ApplicationController
include QueriesHelper
include PaginationHelper
- include OpenProject::ClientPreferenceExtractor
include Concerns::Layout
accept_key_auth :index, :show
- before_action :authorize_on_work_package, only: :show
+ before_action :authorize_on_work_package,
+ :project, only: :show
before_action :find_optional_project,
:protect_from_unauthorized_export, only: :index
- before_action :load_and_validate_query, only: :index, unless: ->() { request.format.html? }
- before_action :load_work_packages, only: :index, if: ->() { request.format.atom? }
-
- before_action :set_gon_settings
+ before_action :load_and_validate_query, only: :index, unless: -> { request.format.html? }
+ before_action :load_work_packages, only: :index, if: -> { request.format.atom? }
def show
respond_to do |format|
@@ -84,11 +82,6 @@ class WorkPackagesController < ApplicationController
protected
- def set_gon_settings
- gon.settings = client_preferences
- gon.settings[:enabled_modules] = project ? project.enabled_modules.collect(&:name) : []
- end
-
def export_list(mime_type)
exporter = WorkPackage::Exporter.for_list(mime_type)
exporter.list(@query, params) do |export|
@@ -145,7 +138,7 @@ class WorkPackagesController < ApplicationController
def protect_from_unauthorized_export
if supported_export_formats.include?(params[:format]) &&
- !User.current.allowed_to?(:export_work_packages, @project, global: @project.nil?)
+ !User.current.allowed_to?(:export_work_packages, @project, global: @project.nil?)
deny_access
false
@@ -164,7 +157,6 @@ class WorkPackagesController < ApplicationController
request.format = 'html'
return render_400(message: @query.errors.full_messages.join(". "))
end
-
rescue ActiveRecord::RecordNotFound
render_404
end
diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb
index cdfebdc4595..9c6e53706d6 100644
--- a/app/models/user_preference.rb
+++ b/app/models/user_preference.rb
@@ -98,9 +98,7 @@ class UserPreference < ActiveRecord::Base
return if time_zone.nil?
zone = ActiveSupport::TimeZone.new(time_zone)
- unless zone.nil?
- zone.tzinfo.canonical_identifier
- end
+ zone&.tzinfo&.canonical_identifier
end
private
diff --git a/frontend/src/app/components/table-pagination/pagination-instance.ts b/frontend/src/app/components/table-pagination/pagination-instance.ts
index b94ca2a4c72..537f37a679e 100644
--- a/frontend/src/app/components/table-pagination/pagination-instance.ts
+++ b/frontend/src/app/components/table-pagination/pagination-instance.ts
@@ -27,7 +27,6 @@
//++
-import {IPaginationOptions} from 'core-components/table-pagination/pagination-service';
export class PaginationInstance {
constructor(public page:number,
diff --git a/frontend/src/app/components/table-pagination/pagination-service.ts b/frontend/src/app/components/table-pagination/pagination-service.ts
index d027a38e9c1..a8442c01e74 100644
--- a/frontend/src/app/components/table-pagination/pagination-service.ts
+++ b/frontend/src/app/components/table-pagination/pagination-service.ts
@@ -27,8 +27,7 @@
//++
import {Injectable} from '@angular/core';
-import {ConfigurationDmService} from 'core-app/modules/hal/dm-services/configuration-dm.service';
-import {take} from "rxjs/operators";
+import {ConfigurationService} from "core-app/modules/common/config/configuration.service";
export const DEFAULT_PAGINATION_OPTIONS = {
maxVisiblePageOptions: 6,
@@ -46,35 +45,15 @@ export interface IPaginationOptions {
export class PaginationService {
private paginationOptions:IPaginationOptions;
- constructor(private ConfigurationDm:ConfigurationDmService) {
- const gonPaginationOptions = this.getInitialPageOptions();
-
- this.paginationOptions = {
- perPage: this.getCachedPerPage(gonPaginationOptions),
- perPageOptions: gonPaginationOptions,
- maxVisiblePageOptions: DEFAULT_PAGINATION_OPTIONS.maxVisiblePageOptions,
- optionsTruncationSize: DEFAULT_PAGINATION_OPTIONS.optionsTruncationSize
- };
- }
-
- public getInitialPageOptions():number[] {
- try {
- return (window as any).gon.settings.pagination.per_page_options;
- } catch (e) {
- console.log("Can't load initial page options from gon: " + e);
- return [];
- }
+ constructor(private configuration:ConfigurationService) {
+ this.loadPaginationOptions();
}
public getCachedPerPage(initialPageOptions:number[]):number {
- const value = window.OpenProject.guardedLocalStorage('pagination.perPage') as string;
-
- if (value !== undefined) {
- const perPage = parseInt(value, 10);
+ const value = this.localStoragePerPage;
- if (perPage > 0 && (initialPageOptions.length === 0 || initialPageOptions.indexOf(perPage) !== -1)) {
- return perPage;
- }
+ if (value !== null && value > 0 && (initialPageOptions.length === 0 || initialPageOptions.indexOf(value) !== -1)) {
+ return value;
}
if (initialPageOptions.length > 0) {
@@ -84,12 +63,26 @@ export class PaginationService {
return 20;
}
+ private get localStoragePerPage() {
+ const value = window.OpenProject.guardedLocalStorage('pagination.perPage') as string;
+
+ if (value !== undefined) {
+ return parseInt(value, 10);
+ } else {
+ return null;
+ }
+ }
+
public getPaginationOptions() {
return this.paginationOptions;
}
+ public get isPerPageKnown() {
+ return !!(this.localStoragePerPage || this.paginationOptions);
+ }
+
public getPerPage() {
- return this.paginationOptions.perPage;
+ return this.localStoragePerPage || this.paginationOptions.perPage;
}
public getMaxVisiblePageOptions() {
@@ -114,12 +107,15 @@ export class PaginationService {
}
public loadPaginationOptions() {
- return this.ConfigurationDm
- .load()
- .toPromise()
- .then((configuration:any) => {
- this.setPerPageOptions(configuration.perPageOptions);
- return this.paginationOptions;
- });
+ return this.configuration.initialized.then(() => {
+ this.paginationOptions = {
+ perPage: this.getCachedPerPage(this.configuration.perPageOptions),
+ perPageOptions: this.configuration.perPageOptions,
+ maxVisiblePageOptions: DEFAULT_PAGINATION_OPTIONS.maxVisiblePageOptions,
+ optionsTruncationSize: DEFAULT_PAGINATION_OPTIONS.optionsTruncationSize
+ };
+
+ return this.paginationOptions;
+ });
}
}
diff --git a/frontend/src/app/components/wp-edit-form/work-package-filter-values.spec.ts b/frontend/src/app/components/wp-edit-form/work-package-filter-values.spec.ts
index eb07d40f41f..7a638ac6164 100644
--- a/frontend/src/app/components/wp-edit-form/work-package-filter-values.spec.ts
+++ b/frontend/src/app/components/wp-edit-form/work-package-filter-values.spec.ts
@@ -55,6 +55,7 @@ import {IsolatedQuerySpace} from "core-app/modules/work_packages/query-space/iso
import {HalEventsService} from "core-app/modules/hal/services/hal-events.service";
import {TimezoneService} from "core-components/datetime/timezone.service";
import {WorkPackageChangeset} from "core-components/wp-edit/work-package-changeset";
+import {ConfigurationDmService} from "core-app/modules/hal/dm-services/configuration-dm.service";
import {WorkPackageNotificationService} from "core-app/modules/work_packages/notifications/work-package-notification.service";
describe('WorkPackageFilterValues', () => {
@@ -82,6 +83,7 @@ describe('WorkPackageFilterValues', () => {
TimezoneService,
PathHelperService,
ConfigurationService,
+ ConfigurationDmService,
CurrentUserService,
HookService,
OpenProjectFileUploadService,
diff --git a/frontend/src/app/components/wp-list/wp-list.service.ts b/frontend/src/app/components/wp-list/wp-list.service.ts
index 9f9ca26c1ac..146db518bfa 100644
--- a/frontend/src/app/components/wp-list/wp-list.service.ts
+++ b/frontend/src/app/components/wp-list/wp-list.service.ts
@@ -35,7 +35,6 @@ import {WorkPackagesListInvalidQueryService} from './wp-list-invalid-query.servi
import {WorkPackageStatesInitializationService} from './wp-states-initialization.service';
import {AuthorisationService} from 'core-app/modules/common/model-auth/model-auth.service';
import {StateService} from '@uirouter/core';
-import {WorkPackagesListChecksumService} from 'core-components/wp-list/wp-list-checksum.service';
import {IsolatedQuerySpace} from "core-app/modules/work_packages/query-space/isolated-query-space";
import {Injectable} from '@angular/core';
import {QueryFormDmService} from 'core-app/modules/hal/dm-services/query-form-dm.service';
@@ -43,10 +42,12 @@ import {PaginationObject, QueryDmService} from 'core-app/modules/hal/dm-services
import {UrlParamsHelperService} from 'core-components/wp-query/url-params-helper';
import {NotificationsService} from 'core-app/modules/common/notifications/notifications.service';
import {I18nService} from "core-app/modules/common/i18n/i18n.service";
-import {BehaviorSubject, from, Observable, of} from 'rxjs';
+import {from, Observable, of} from 'rxjs';
import {input} from "reactivestates";
-import {catchError, mergeMap, share, switchMap, take, tap} from "rxjs/operators";
+import {catchError, mergeMap, share, switchMap, take, delay, combineLatest} from "rxjs/operators";
import {WorkPackageViewPaginationService} from "core-app/modules/work_packages/routing/wp-view-base/view-services/wp-view-pagination.service";
+import {ConfigurationService} from "core-app/modules/common/config/configuration.service";
+import {PaginationService} from "core-components/table-pagination/pagination-service";
export interface QueryDefinition {
queryParams:{ query_id?:string, query_props?:string };
@@ -63,8 +64,13 @@ export class WorkPackagesListService {
private queryLoading = this.queryRequests
.values$()
.pipe(
+ switchMap((q:QueryDefinition) => {
+ return from(this.ensurePerPageKnown().then(() => q));
+ }),
// Stream the query request, switchMap will call previous requests to be cancelled
- switchMap((q:QueryDefinition) => this.streamQueryRequest(q.queryParams, q.projectIdentifier)),
+ switchMap((q:QueryDefinition) =>
+ this.streamQueryRequest(q.queryParams, q.projectIdentifier)
+ ),
// Map the observable from the stream to a new one that completes when states are loaded
mergeMap((query:QueryResource) => {
// load the form if needed
@@ -88,6 +94,8 @@ export class WorkPackagesListService {
protected QueryFormDm:QueryFormDmService,
protected states:States,
protected querySpace:IsolatedQuerySpace,
+ protected pagination:PaginationService,
+ protected configuration:ConfigurationService,
protected wpTablePagination:WorkPackageViewPaginationService,
protected wpStatesInitialization:WorkPackageStatesInitializationService,
protected wpListInvalidQueryService:WorkPackagesListInvalidQueryService) {
@@ -351,4 +359,12 @@ export class WorkPackagesListService {
.catch(reject);
});
}
+
+ private async ensurePerPageKnown() {
+ if (this.pagination.isPerPageKnown) {
+ return true;
+ } else {
+ return this.configuration.initialized;
+ }
+ }
}
diff --git a/frontend/src/app/components/wp-table/table-pagination/wp-table-pagination.component.spec.ts b/frontend/src/app/components/wp-table/table-pagination/wp-table-pagination.component.spec.ts
index 478f91dacef..492ef4ce0af 100644
--- a/frontend/src/app/components/wp-table/table-pagination/wp-table-pagination.component.spec.ts
+++ b/frontend/src/app/components/wp-table/table-pagination/wp-table-pagination.component.spec.ts
@@ -29,7 +29,6 @@
import {HttpClientModule} from '@angular/common/http';
import {IsolatedQuerySpace} from "core-app/modules/work_packages/query-space/isolated-query-space";
-import {ConfigurationDmService} from 'core-app/modules/hal/dm-services/configuration-dm.service';
import {async, inject, TestBed} from '@angular/core/testing';
import {States} from 'core-components/states.service';
import {PaginationInstance} from 'core-components/table-pagination/pagination-instance';
@@ -42,15 +41,26 @@ import {I18nService} from "core-app/modules/common/i18n/i18n.service";
import {OpenProject} from "core-app/globals/openproject";
import {OpIcon} from "core-app/modules/common/icon/op-icon";
import {WorkPackageViewSortByService} from "core-app/modules/work_packages/routing/wp-view-base/view-services/wp-view-sort-by.service";
+import {ConfigurationService} from "core-app/modules/common/config/configuration.service";
+import {ConfigurationDmService} from "core-app/modules/hal/dm-services/configuration-dm.service";
function setupMocks(paginationService:PaginationService) {
+ const options:IPaginationOptions = {
+ perPage: 0,
+ perPageOptions: [10, 50],
+ maxVisiblePageOptions: 1,
+ optionsTruncationSize: 6
+ };
+
+ spyOn(paginationService, 'getMaxVisiblePageOptions').and.callFake(() => {
+ return options.maxVisiblePageOptions;
+ });
+
+ spyOn(paginationService, 'getOptionsTruncationSize').and.callFake(() => {
+ return options.optionsTruncationSize;
+ });
+
spyOn(paginationService, 'loadPaginationOptions').and.callFake(() => {
- const options:IPaginationOptions = {
- perPage: 0,
- perPageOptions: [10, 100, 500, 1000],
- maxVisiblePageOptions: 0,
- optionsTruncationSize: 0
- };
return Promise.resolve(options);
});
}
@@ -63,7 +73,6 @@ describe('wpTablePagination Directive', () => {
beforeEach(async(() => {
window.OpenProject = new OpenProject();
- (window as any).gon = { settings: { pagination: { per_page_options: [20, 50] } } };
// noinspection JSIgnoredPromiseFromCall
TestBed.configureTestingModule({
@@ -81,6 +90,7 @@ describe('wpTablePagination Directive', () => {
PathHelperService,
WorkPackageViewPaginationService,
HalResourceService,
+ ConfigurationService,
ConfigurationDmService,
IsolatedQuerySpace,
I18nService
diff --git a/frontend/src/app/modules/attachments/attachments-upload/attachments-upload.component.ts b/frontend/src/app/modules/attachments/attachments-upload/attachments-upload.component.ts
index 0f54901dc53..e14d8929f81 100644
--- a/frontend/src/app/modules/attachments/attachments-upload/attachments-upload.component.ts
+++ b/frontend/src/app/modules/attachments/attachments-upload/attachments-upload.component.ts
@@ -65,9 +65,9 @@ export class AttachmentsUploadComponent implements OnInit {
ngOnInit() {
this.$element = jQuery(this.elementRef.nativeElement);
- this.ConfigurationService.api().then((settings:any) => {
- this.maxFileSize = settings.maximumAttachmentFileSize;
- });
+ this.ConfigurationService.initialized.then(() =>
+ this.maxFileSize = this.ConfigurationService.maximumAttachmentFileSize
+ );
}
public triggerFileInput(event:MouseEvent) {
diff --git a/frontend/src/app/modules/common/config/configuration.service.ts b/frontend/src/app/modules/common/config/configuration.service.ts
index 708277c5bec..77e55ec561d 100644
--- a/frontend/src/app/modules/common/config/configuration.service.ts
+++ b/frontend/src/app/modules/common/config/configuration.service.ts
@@ -26,134 +26,97 @@
// See doc/COPYRIGHT.rdoc for more details.
// ++
-import {PathHelperService} from 'core-app/modules/common/path-helper/path-helper.service';
import {Injectable} from '@angular/core';
import {I18nService} from 'core-app/modules/common/i18n/i18n.service';
-import {HttpClient} from '@angular/common/http';
+import {ConfigurationDmService} from "core-app/modules/hal/dm-services/configuration-dm.service";
+import {ConfigurationResource} from "core-app/modules/hal/resources/configuration-resource";
@Injectable()
export class ConfigurationService {
// fetches configuration from the ApiV3 endpoint
// TODO: this currently saves the request between page reloads,
// but could easily be stored in localStorage
- private cache:any;
- private path:string = this.PathHelper.api.v3.configuration.toString();
- public settings = this.initSettings();
+ private configuration:ConfigurationResource;
+ public initialized:Promise<Boolean>;
- public constructor(readonly http:HttpClient,
- readonly PathHelper:PathHelperService,
- readonly I18n:I18nService) {
-
- }
-
- public fetchSettings () {
- return this.http.get<any>(this.path).toPromise();
- }
-
- public api() {
- return new Promise((resolve, reject) => {
- if (this.cache) {
- resolve(this.cache);
- } else {
- this.fetchSettings()
- .then((data:any) => {
- this.cache = data;
- resolve(data);
- })
- .catch(reject);
- }
- });
- }
-
- public initSettings() {
-
- var settings:any = {},
- defaults:any = {
- enabled_modules: [],
- display: {},
- user_preferences: {
- time_zone: '',
- others: {
- comments_sorting: 'asc',
- warn_on_leaving_unsaved: true,
- auto_hide_popups: false
- }
- }
- };
-
- var gon = (window as any).gon;
- if (gon !== undefined) {
- settings = gon.settings;
- }
-
- return _.merge(defaults, settings);
- }
-
- public displaySettingPresent(setting:any) {
- return this.settings.display.hasOwnProperty(setting) &&
- this.settings.display[setting] !== false;
+ public constructor(readonly I18n:I18nService,
+ readonly configurationDm:ConfigurationDmService) {
+ this.initialized = this.loadConfiguration().then(() => true).catch(() => false);
}
public commentsSortedInDescendingOrder() {
- return this.settings.user_preferences.others.comments_sorting === 'desc';
+ return this.userPreference('commentSortDescending');
}
public warnOnLeavingUnsaved() {
- return this.settings.user_preferences.others.warn_on_leaving_unsaved === true;
+ return this.userPreference('warnOnLeavingUnsaved');
}
public autoHidePopups() {
- return this.settings.user_preferences.others.auto_hide_popups === true;
+ return this.userPreference('autoHidePopups');
}
public isTimezoneSet() {
- return this.settings.user_preferences.time_zone !== '';
+ return !!this.timezone();
}
public timezone() {
- return this.settings.user_preferences.time_zone;
+ return this.userPreference('timeZone');
+ }
+
+ public get maximumAttachmentFileSize() {
+ return this.systemPreference('maximumAttachmentFileSize');
+ }
+
+ public get perPageOptions() {
+ return this.systemPreference('perPageOptions');
}
public dateFormatPresent() {
- return this.displaySettingPresent('date_format') &&
- this.settings.display.date_format !== '';
+ return !!this.systemPreference('dateFormat');
}
public dateFormat() {
- return this.settings.display.date_format;
+ return this.systemPreference('dateFormat');
}
public timeFormatPresent() {
- return this.displaySettingPresent('time_format') &&
- this.settings.display.time_format !== '';
+ return !!this.systemPreference('timeFormat');
}
public timeFormat() {
- return this.settings.display.time_format;
- }
-
- public isModuleEnabled(module:string) {
- return this.settings.enabled_modules.indexOf(module) >= 0;
+ return this.systemPreference('timeFormat');
}
public startOfWeekPresent() {
- return this.displaySettingPresent('start_of_week') &&
- this.settings.display.start_of_week !== '';
+ return !!this.systemPreference('startOfWeek');
}
public startOfWeek() {
if (this.startOfWeekPresent()) {
- return this.settings.display.start_of_week;
- }
-
- // This if/else statement is used because
- // jquery regionals have different start day for German locale
- if (I18n.locale === 'en') {
+ return this.systemPreference('startOfWeek');
+ } else if (I18n.locale === 'en') {
return 1;
} else if (I18n.locale === 'de') {
+ // This if/else statement is used because
+ // jquery regionals have different start day for German locale
return 0;
+ } else {
+ return null;
}
+ }
+
+ private loadConfiguration() {
+ return this.configurationDm.load().toPromise().then((configuration) => {
+ this.configuration = configuration;
+ });
+ }
+
+ private userPreference(pref:string) {
+ return this.configuration.userPreferences[pref];
+ }
- return '';
+ private systemPreference(pref:string) {
+ return this.configuration[pref];
}
}
diff --git a/lib/api/v3/configuration/configuration_representer.rb b/lib/api/v3/configuration/configuration_representer.rb
index afdfbc65803..0b322052255 100644
--- a/lib/api/v3/configuration/configuration_representer.rb
+++ b/lib/api/v3/configuration/configuration_representer.rb
@@ -52,6 +52,20 @@ module API
property :per_page_options,
getter: ->(*) { per_page_options_array }
+ property :date_format,
+ exec_context: :decorator,
+ render_nil: true
+
+ property :time_format,
+ exec_context: :decorator,
+ render_nil: true
+
+ property :start_of_week,
+ getter: ->(*) {
+ Setting.start_of_week.to_i unless Setting.start_of_week.blank?
+ },
+ render_nil: true
+
property :user_preferences,
embedded: true,
exec_context: :decorator,
@@ -67,6 +81,58 @@ module API
UserPreferences::UserPreferencesRepresenter.new(current_user.pref,
current_user: current_user)
end
+
+ def date_format
+ reformated(Setting.date_format) do |directive|
+ case directive
+ when '%Y'
+ 'YYYY'
+ when '%y'
+ 'YY'
+ when '%m'
+ 'MM'
+ when '%B'
+ 'MMMM'
+ when '%b', '%h'
+ 'MMM'
+ when '%d'
+ 'DD'
+ when '%e'
+ 'D'
+ when '%j'
+ 'DDDD'
+ end
+ end
+ end
+
+ def time_format
+ reformated(Setting.time_format) do |directive|
+ case directive
+ when '%H'
+ 'HH'
+ when '%k'
+ 'H'
+ when '%I'
+ 'hh'
+ when '%l'
+ 'h'
+ when '%P'
+ 'A'
+ when '%p'
+ 'a'
+ when '%M'
+ 'mm'
+ end
+ end
+ end
+
+ def reformated(setting)
+ format = setting.gsub(/%\w/) do |directive|
+ yield directive
+ end
+
+ format.blank? ? nil : format
+ end
end
end
end
diff --git a/lib/open_project/client_preference_extractor.rb b/lib/open_project/client_preference_extractor.rb
deleted file mode 100644
index 0dcc609c52f..00000000000
--- a/lib/open_project/client_preference_extractor.rb
+++ /dev/null
@@ -1,106 +0,0 @@
-#-- encoding: UTF-8
-#-- copyright
-# OpenProject is a project management system.
-# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2017 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See docs/COPYRIGHT.rdoc for more details.
-#++
-
-module OpenProject
- module ClientPreferenceExtractor
- def client_preferences
- gon.settings = {
- user_preferences: user_preferences,
- display: {
- date_format: momentjstify_date_format(Setting.date_format),
- time_format: momentjstify_time_format(Setting.time_format),
- start_of_week: Setting.start_of_week
- },
- pagination: {
- per_page_options: Setting.per_page_options_array
- }
- }
- end
-
- def user_preferences(user = User.current)
- pref = user.pref.clone
-
- pref[:comments_sorting] = user.pref.comments_sorting
- pref[:auto_hide_popups] = user.pref.auto_hide_popups?
- map_timezone_to_tz!(pref)
- end
-
- def map_timezone_to_tz!(pref)
- unless pref.time_zone.blank?
- pref.time_zone = ActiveSupport::TimeZone::MAPPING[pref.time_zone]
- end
-
- pref
- end
-
- def momentjstify_date_format(date_format)
- date_format.gsub(/%\w/) do |directive|
- case directive
- when '%Y'
- 'YYYY'
- when '%y'
- 'YY'
- when '%m'
- 'MM'
- when '%B'
- 'MMMM'
- when '%b', '%h'
- 'MMM'
- when '%d'
- 'DD'
- when '%e'
- 'D'
- when '%j'
- 'DDDD'
- end
- end
- end
-
- def momentjstify_time_format(time_format)
- time_format.gsub(/%\w/) do |directive|
- case directive
- when '%H'
- 'HH'
- when '%k'
- 'H'
- when '%I'
- 'hh'
- when '%l'
- 'h'
- when '%P'
- 'A'
- when '%p'
- 'a'
- when '%M'
- 'mm'
- end
- end
- end
- end
-end
diff --git a/modules/boards/app/controllers/boards/boards_controller.rb b/modules/boards/app/controllers/boards/boards_controller.rb
index 6294e61a1da..1a1947be925 100644
--- a/modules/boards/app/controllers/boards/boards_controller.rb
+++ b/modules/boards/app/controllers/boards/boards_controller.rb
@@ -1,7 +1,5 @@
module ::Boards
class BoardsController < BaseController
- include OpenProject::ClientPreferenceExtractor
-
before_action :find_optional_project
before_action :authorize
@@ -9,10 +7,6 @@ module ::Boards
# to view work packages
before_action :authorize_work_package_permission
- # Pass some visibility settings via gon that are not
- # available through the global grids API
- before_action :pass_gon
-
menu_item :board_view
def index
@@ -25,10 +19,6 @@ module ::Boards
private
- def pass_gon
- gon.settings = client_preferences
- end
-
def authorize_work_package_permission
unless current_user.allowed_to?(:view_work_packages, @project, global: @project.nil?)
deny_access
diff --git a/modules/grids/app/controllers/grids/base_in_project_controller.rb b/modules/grids/app/controllers/grids/base_in_project_controller.rb
index cdf3c6d23d9..0a10e6ff760 100644
--- a/modules/grids/app/controllers/grids/base_in_project_controller.rb
+++ b/modules/grids/app/controllers/grids/base_in_project_controller.rb
@@ -1,11 +1,9 @@
module ::Grids
class BaseInProjectController < ::ApplicationController
- include OpenProject::ClientPreferenceExtractor
before_action :find_project_by_project_id
before_action :authorize
def show
- gon.settings = client_preferences
render layout: 'angular'
end
end
diff --git a/modules/my_page/app/controllers/my_page/angular_controller.rb b/modules/my_page/app/controllers/my_page/angular_controller.rb
index 18f72b0b255..010cdf4606c 100644
--- a/modules/my_page/app/controllers/my_page/angular_controller.rb
+++ b/modules/my_page/app/controllers/my_page/angular_controller.rb
@@ -30,10 +30,8 @@
class MyPage::AngularController < ::ApplicationController
before_action :require_login
- include OpenProject::ClientPreferenceExtractor
def no_menu
- gon.settings = client_preferences
render layout: 'no_menu'
end
end
diff --git a/spec/features/search_spec.rb b/spec/features/search_spec.rb
index 3c40917a7da..2cae768b8c9 100644
--- a/spec/features/search_spec.rb
+++ b/spec/features/search_spec.rb
@@ -109,7 +109,7 @@ describe 'Search', type: :feature, js: true do
sleep 1
- expect(current_path).to match /work_packages\/#{target_work_package.id}\//
+ expect(current_path).to eql project_work_package_path(target_work_package.project, target_work_package, state: 'activity')
first_wp = work_packages.first
diff --git a/spec/lib/api/v3/configuration/configuration_representer_spec.rb b/spec/lib/api/v3/configuration/configuration_representer_spec.rb
index 41e05976122..c0f83f95fda 100644
--- a/spec/lib/api/v3/configuration/configuration_representer_spec.rb
+++ b/spec/lib/api/v3/configuration/configuration_representer_spec.rb
@@ -86,6 +86,132 @@ describe ::API::V3::Configuration::ConfigurationRepresenter do
is_expected.to be_json_eql([1, 50, 100].to_json).at_path('perPageOptions')
end
+ context 'timeFormat' do
+ context 'with time format', with_settings: { time_format: '%I:%M %p' } do
+ it 'indicates the timeFormat' do
+ is_expected
+ .to be_json_eql('hh:mm a'.to_json)
+ .at_path('timeFormat')
+ end
+ end
+
+ context 'with time format', with_settings: { time_format: '%H:%M' } do
+ it 'indicates the timeFormat' do
+ is_expected
+ .to be_json_eql('HH:mm'.to_json)
+ .at_path('timeFormat')
+ end
+ end
+
+ context 'with a time format', with_settings: { time_format: '' } do
+ it 'indicates the timeFormat' do
+ is_expected
+ .to be_json_eql(nil.to_json)
+ .at_path('timeFormat')
+ end
+ end
+ end
+
+ context 'dateFormat' do
+ context 'without a date format', with_settings: { date_format: '' } do
+ it 'indicates the dateFormat' do
+ is_expected
+ .to be_json_eql(nil.to_json)
+ .at_path('dateFormat')
+ end
+ end
+
+ context 'with date format (%Y-%m-%d)', with_settings: { date_format: '%Y-%m-%d' } do
+ it 'indicates the dateFormat' do
+ is_expected
+ .to be_json_eql('YYYY-MM-DD'.to_json)
+ .at_path('dateFormat')
+ end
+ end
+
+ context 'with date format (%d/%m/%Y)', with_settings: { date_format: '%Y-%m-%d' } do
+ it 'indicates the dateFormat' do
+ is_expected
+ .to be_json_eql('YYYY-MM-DD'.to_json)
+ .at_path('dateFormat')
+ end
+ end
+
+ context 'with date format (%d.%m.%Y)', with_settings: { date_format: '%d.%m.%Y' } do
+ it 'indicates the dateFormat' do
+ is_expected
+ .to be_json_eql('DD.MM.YYYY'.to_json)
+ .at_path('dateFormat')
+ end
+ end
+
+ context 'with date format (%d-%m-%Y)', with_settings: { date_format: '%d-%m-%Y' } do
+ it 'indicates the dateFormat' do
+ is_expected
+ .to be_json_eql('DD-MM-YYYY'.to_json)
+ .at_path('dateFormat')
+ end
+ end
+
+ context 'with date format (%m/%d/%Y)', with_settings: { date_format: '%m/%d/%Y' } do
+ it 'indicates the dateFormat' do
+ is_expected
+ .to be_json_eql('MM/DD/YYYY'.to_json)
+ .at_path('dateFormat')
+ end
+ end
+
+ context 'with date format (%d %b %Y)', with_settings: { date_format: '%d %b %Y' } do
+ it 'indicates the dateFormat' do
+ is_expected
+ .to be_json_eql('DD MMM YYYY'.to_json)
+ .at_path('dateFormat')
+ end
+ end
+
+ context 'with date format (%d %B %Y)', with_settings: { date_format: '%d %B %Y' } do
+ it 'indicates the dateFormat' do
+ is_expected
+ .to be_json_eql('DD MMMM YYYY'.to_json)
+ .at_path('dateFormat')
+ end
+ end
+
+ context 'with date format (%b %d, %Y)', with_settings: { date_format: '%b %d, %Y' } do
+ it 'indicates the dateFormat' do
+ is_expected
+ .to be_json_eql('MMM DD, YYYY'.to_json)
+ .at_path('dateFormat')
+ end
+ end
+
+ context 'with date format (%B %d, %Y)', with_settings: { date_format: '%B %d, %Y' } do
+ it 'indicates the dateFormat' do
+ is_expected
+ .to be_json_eql('MMMM DD, YYYY'.to_json)
+ .at_path('dateFormat')
+ end
+ end
+ end
+
+ context 'startOfWeek' do
+ context 'without a setting', with_settings: { start_of_week: '' } do
+ it 'is null' do
+ is_expected
+ .to be_json_eql(nil.to_json)
+ .at_path('startOfWeek')
+ end
+ end
+
+ context 'with `Monday` being set', with_settings: { start_of_week: '1' } do
+ it 'indicates the dateFormat' do
+ is_expected
+ .to be_json_eql(1.to_json)
+ .at_path('startOfWeek')
+ end
+ end
+ end
+
describe '_embedded' do
context 'userPreferences' do
context 'if embedding' do
diff --git a/spec/lib/open_project/client_preference_extractor_spec.rb b/spec/lib/open_project/client_preference_extractor_spec.rb
deleted file mode 100644
index e6f914b61ed..00000000000
--- a/spec/lib/open_project/client_preference_extractor_spec.rb
+++ /dev/null
@@ -1,79 +0,0 @@
-#-- copyright
-# OpenProject is a project management system.
-# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2017 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See docs/COPYRIGHT.rdoc for more details.
-#++
-
-require 'spec_helper'
-
-describe OpenProject::ClientPreferenceExtractor do
- let(:extractor) { (Class.new { include OpenProject::ClientPreferenceExtractor }).new }
-
- describe 'user preferences' do
- let(:user) { FactoryBot.build(:user) }
-
- shared_examples_for 'preference with timezone' do
- before do
- allow(user).to receive(:pref) { pref }
- end
-
- subject { extractor.user_preferences(user) }
-
- it { expect(subject).not_to be_nil }
-
- it { expect(subject.time_zone).to eq(time_zone) }
- end
-
- context 'without timezone' do
- let(:pref) { UserPreference.new(time_zone: nil) }
-
- it_behaves_like 'preference with timezone' do
- let(:time_zone) { nil }
- end
- end
-
- context 'with timezone' do
- let(:pref) { UserPreference.new(time_zone: 'Berlin') }
-
- it_behaves_like 'preference with timezone' do
- let(:time_zone) { 'Europe/Berlin' }
- end
- end
- end
-
- describe 'date format' do
- let(:ruby_date_format) { '%Y-%y-%m-%B-%b-%h ? %d:%e:%j' }
- let(:expected_date_format) { 'YYYY-YY-MM-MMMM-MMM-MMM ? DD:D:DDDD' }
-
- it { expect(extractor.momentjstify_date_format(ruby_date_format)).to eq(expected_date_format) }
- end
-
- describe 'time format' do
- let(:ruby_time_format) { '%H:%k:%I:%l ? %P+%p+%M' }
- let(:expected_time_format) { 'HH:H:hh:h ? A+a+mm' }
-
- it { expect(extractor.momentjstify_time_format(ruby_time_format)).to eq(expected_time_format) }
- end
-end