From 50af8e50dd47ae60b09a62cff301f8d2d1c36509 Mon Sep 17 00:00:00 2001 From: Jens Ulferts Date: Thu, 3 Mar 2016 17:13:22 +0100 Subject: [PATCH] move v3 naming into own module --- .../api/experimental/concerns/column_data.rb | 6 +- .../experimental/concerns/query_loading.rb | 30 ------- .../api/experimental/concerns/v3_naming.rb | 79 +++++++++++++++++++ .../api/experimental/queries_controller.rb | 3 +- .../experimental/work_packages_controller.rb | 22 +----- .../api-work-packages.service.ts | 7 +- 6 files changed, 90 insertions(+), 57 deletions(-) create mode 100644 app/controllers/api/experimental/concerns/v3_naming.rb diff --git a/app/controllers/api/experimental/concerns/column_data.rb b/app/controllers/api/experimental/concerns/column_data.rb index da427f288b..105faa1597 100644 --- a/app/controllers/api/experimental/concerns/column_data.rb +++ b/app/controllers/api/experimental/concerns/column_data.rb @@ -29,7 +29,7 @@ module Api::Experimental::Concerns::ColumnData def get_columns_for_json(columns) columns.map do |column| - { name: v3_name(column.name), + { name: internal_to_v3_name(column.name), title: column.caption, sortable: column.sortable?, groupable: column.groupable?, @@ -42,10 +42,6 @@ module Api::Experimental::Concerns::ColumnData private - def v3_name(string) - API::Utilities::PropertyNameConverter.from_ar_name(string) - end - def get_column_meta(column) # This is where we want to add column specific behaviour to instruct the # front end how to deal with it. Needs to be things like user link, project diff --git a/app/controllers/api/experimental/concerns/query_loading.rb b/app/controllers/api/experimental/concerns/query_loading.rb index 5c73640e7a..631e6ce2cc 100644 --- a/app/controllers/api/experimental/concerns/query_loading.rb +++ b/app/controllers/api/experimental/concerns/query_loading.rb @@ -47,36 +47,6 @@ module Api::Experimental::Concerns::QueryLoading @query end - def external_to_internal_name(string, append_id: true) - API::Utilities::PropertyNameConverter.to_ar_name(string, context: WorkPackage.new, refer_to_ids: append_id) - end - - def internal_to_external_name(string) - API::Utilities::PropertyNameConverter.from_ar_name(string) - end - - def translate_query_params - params[:c] = (params[:c] || []).map { |column| - external_to_internal_name(column, append_id: false) - } - params[:f] = (params[:f] || []).map { |column| - external_to_internal_name(column) - } - params[:op] = (params[:op] || []).each_with_object({}) { |(column, operator), hash| - hash[external_to_internal_name(column)] = operator - } - params[:v] = (params[:v] || []).each_with_object({}) { |(column, value), hash| - hash[external_to_internal_name(column)] = value - } - params[:sort] = begin - (params[:sort] || '').split(',').map { |sort| - criteria = sort.split(':') - - "#{external_to_internal_name(criteria.first, append_id: false)}:#{criteria.last}" - }.join(',') - end - end - def prepare_query # Set the query properties only if a query property string exists in the # URL. This assumes that if 'accept_empty_query_fields' or 'is_public' diff --git a/app/controllers/api/experimental/concerns/v3_naming.rb b/app/controllers/api/experimental/concerns/v3_naming.rb new file mode 100644 index 0000000000..4e6bfb95d4 --- /dev/null +++ b/app/controllers/api/experimental/concerns/v3_naming.rb @@ -0,0 +1,79 @@ +#-- copyright +# OpenProject is a project management system. +# Copyright (C) 2012-2015 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-2013 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 doc/COPYRIGHT.rdoc for more details. +#++ + +module Api::Experimental::Concerns::V3Naming + def v3_to_internal_name(string, append_id: true) + API::Utilities::PropertyNameConverter.to_ar_name(string, + context: WorkPackage.new, + refer_to_ids: append_id) + end + + def internal_to_v3_name(string) + API::Utilities::PropertyNameConverter.from_ar_name(string) + end + + def v3_params_as_internal + params[:c] = params[:c].map { |column| + v3_to_internal_name(column, append_id: false) + } if params[:c] + params[:f] = params[:f].map { |column| + v3_to_internal_name(column) + } if params[:f] + params[:op] = params[:op].each_with_object({}) { |(column, operator), hash| + hash[v3_to_internal_name(column)] = operator + } if params[:op] + params[:v] = params[:v].each_with_object({}) { |(column, value), hash| + hash[v3_to_internal_name(column)] = value + } if params[:v] + params[:sort] = begin + (params[:sort] || '').split(',').map { |sort| + criteria = sort.split(':') + + "#{v3_to_internal_name(criteria.first, append_id: false)}:#{criteria.last}" + }.join(',') + end if params[:sort] + end + + def json_query_as_v3(json_query) + json_query['column_names'] = (json_query['column_names'] || []).map { |column| + internal_to_v3_name(column) + } + + json_query['sort_criteria'] = (json_query['sort_criteria'] || []).map { |criteria| + [internal_to_v3_name(criteria.first), criteria.last] + } + + json_query['group_by'] = internal_to_v3_name(json_query['group_by']) + + json_query['filters'].each do |filter| + filter[:name] = internal_to_v3_name(filter[:name]) + end + + json_query + end +end diff --git a/app/controllers/api/experimental/queries_controller.rb b/app/controllers/api/experimental/queries_controller.rb index df1a64d986..31a00d6fc4 100644 --- a/app/controllers/api/experimental/queries_controller.rb +++ b/app/controllers/api/experimental/queries_controller.rb @@ -32,13 +32,14 @@ module Api::Experimental include Api::Experimental::Concerns::GrapeRouting include Api::Experimental::Concerns::ColumnData include Api::Experimental::Concerns::QueryLoading + include Api::Experimental::Concerns::V3Naming include QueriesHelper include ExtendedHTTP before_filter :find_optional_project before_filter :setup_query_for_create, only: [:create] - before_filter :translate_query_params, only: [:create, :update] + before_filter :v3_params_as_internal, only: [:create, :update] before_filter :setup_existing_query, only: [:update, :destroy] before_filter :authorize_on_query, only: [:create, :destroy] before_filter :authorize_update_on_query, only: [:update] diff --git a/app/controllers/api/experimental/work_packages_controller.rb b/app/controllers/api/experimental/work_packages_controller.rb index 4833c2a022..2281d98073 100644 --- a/app/controllers/api/experimental/work_packages_controller.rb +++ b/app/controllers/api/experimental/work_packages_controller.rb @@ -35,6 +35,7 @@ module Api include ::Api::Experimental::Concerns::GrapeRouting include ::Api::Experimental::Concerns::ColumnData include ::Api::Experimental::Concerns::QueryLoading + include ::Api::Experimental::Concerns::V3Naming include PaginationHelper include QueriesHelper @@ -42,7 +43,7 @@ module Api include ExtendedHTTP before_filter :find_optional_project, - :translate_query_params, + :v3_params_as_internal, :load_query def index @@ -88,24 +89,7 @@ module Api json_query[:_links] = allowed_links_on_query(query, user) - if json_query['column_names'] - json_query['column_names'] = json_query['column_names'].map { |column| - internal_to_external_name(column) - } - end - if json_query['sort_criteria'] - json_query['sort_criteria'] = json_query['sort_criteria'].map { |criteria| - [internal_to_external_name(criteria.first), criteria.last] - } - end - - json_query['group_by'] = internal_to_external_name(json_query['group_by']) - - json_query['filters'].each do |filter| - filter[:name] = internal_to_external_name(filter[:name]) - end - - json_query + json_query_as_v3(json_query) end def export_formats diff --git a/frontend/app/components/api/api-work-packages/api-work-packages.service.ts b/frontend/app/components/api/api-work-packages/api-work-packages.service.ts index ca4caaca39..d019c57663 100644 --- a/frontend/app/components/api/api-work-packages/api-work-packages.service.ts +++ b/frontend/app/components/api/api-work-packages/api-work-packages.service.ts @@ -59,8 +59,7 @@ export class ApiWorkPackagesService { const params = { offset: offset, pageSize: pageSize, - filters: [v3Filters], - sortBy: [query.sort_criteria], + filters: [v3Filters] }; if (query.group_by) { @@ -71,6 +70,10 @@ export class ApiWorkPackagesService { params['showSums'] = query.display_sums; } + if (query.sort_criteria) { + params['sortBy'] = [query.sort_criteria]; + } + return params; } }