commit
5a5170455d
@ -0,0 +1,167 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
class ParseQueryParamsService |
||||
def call(params) |
||||
parsed_params = {} |
||||
|
||||
parsed_params[:group_by] = group_by_from_params(params) |
||||
|
||||
error_result = with_service_error_on_json_parse_error do |
||||
parsed_params[:filters] = filters_from_params(params) |
||||
|
||||
parsed_params[:sort_by] = sort_by_from_params(params) |
||||
end |
||||
return error_result if error_result |
||||
|
||||
parsed_params[:columns] = columns_from_params(params) |
||||
|
||||
parsed_params[:display_sums] = sums_from_params(params) |
||||
|
||||
ServiceResult.new(success: true, |
||||
result: without_empty(parsed_params)) |
||||
end |
||||
|
||||
def group_by_from_params(params) |
||||
convert_attribute(params[:group_by] || params[:groupBy] || params[:g]) |
||||
end |
||||
|
||||
def sort_by_from_params(params) |
||||
return unless params[:sortBy] |
||||
|
||||
parse_sorting_from_json(params[:sortBy]) |
||||
end |
||||
|
||||
# Expected format looks like: |
||||
# [ |
||||
# { |
||||
# "filtered_field_name": { |
||||
# "operator": "a name for a filter operation", |
||||
# "values": ["values", "for the", "operation"] |
||||
# } |
||||
# }, |
||||
# { /* more filters if needed */} |
||||
# ] |
||||
def filters_from_params(params) |
||||
return unless params[:filters] |
||||
|
||||
filters = JSON.parse(params[:filters]) |
||||
filters.each_with_object([]) do |filter, array| |
||||
attribute = filter.keys.first # there should only be one attribute per filter |
||||
operator = filter[attribute]['operator'] |
||||
values = filter[attribute]['values'] |
||||
ar_attribute = convert_filter_attribute attribute, append_id: true |
||||
|
||||
internal_representation = { field: ar_attribute, |
||||
operator: operator, |
||||
values: values } |
||||
array << internal_representation |
||||
end |
||||
end |
||||
|
||||
def columns_from_params(params) |
||||
columns = params[:columns] || params[:c] || params[:column_names] |
||||
|
||||
return unless columns |
||||
|
||||
columns.map do |column| |
||||
convert_attribute(column) |
||||
end |
||||
end |
||||
|
||||
def sums_from_params(params) |
||||
if params[:showSums] == 'true' |
||||
true |
||||
elsif params[:showSums] == 'false' |
||||
false |
||||
end |
||||
end |
||||
|
||||
## |
||||
# Maps given field names coming from the frontend to the actual names |
||||
# as expected by the query. This works slightly different to what happens |
||||
# in #column_names_from_params. For instance while they column name is |
||||
# :type the expected field name is :type_id. |
||||
# |
||||
# Examples: |
||||
# * status => status_id |
||||
# * progresssDone => done_ratio |
||||
# * assigned => assigned_to |
||||
# * customField1 => cf_1 |
||||
# |
||||
# @param query [Query] Query for which to get the correct field names. |
||||
# @param field_names [Array] Field names as read from the params. |
||||
# @return [Array] Returns a list of fixed field names. The list may contain nil values |
||||
# for fields which could not be found. |
||||
def fix_field_array(field_names) |
||||
return [] if field_names.nil? |
||||
|
||||
field_names |
||||
.map { |name| convert_attribute name, append_id: true } |
||||
end |
||||
|
||||
def parse_sorting_from_json(json) |
||||
JSON.parse(json).map do |order| |
||||
attribute, direction = if order.is_a?(Array) |
||||
[order.first, order.last] |
||||
elsif order.is_a?(String) |
||||
order.split(':') |
||||
end |
||||
|
||||
[convert_attribute(attribute), direction] |
||||
end |
||||
end |
||||
|
||||
def convert_attribute(attribute, append_id: false) |
||||
::API::Utilities::WpPropertyNameConverter.to_ar_name(attribute, |
||||
refer_to_ids: append_id) |
||||
end |
||||
|
||||
def convert_filter_attribute(attribute, append_id: false) |
||||
::API::Utilities::QueryFiltersNameConverter.to_ar_name(attribute, |
||||
refer_to_ids: append_id) |
||||
end |
||||
|
||||
def with_service_error_on_json_parse_error |
||||
yield |
||||
|
||||
nil |
||||
rescue ::JSON::ParserError => error |
||||
result = ServiceResult.new |
||||
result.errors.add(:base, error.message) |
||||
return result |
||||
end |
||||
|
||||
def without_empty(hash) |
||||
hash.select { |_, v| v.present? || v == false } |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,55 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
class UpdateQueryFromV3ParamsService |
||||
def initialize(query, user) |
||||
self.query = query |
||||
self.current_user = user |
||||
end |
||||
|
||||
def call(params) |
||||
parsed = ::API::V3::ParseQueryParamsService |
||||
.new |
||||
.call(params) |
||||
|
||||
if parsed.success? |
||||
::UpdateQueryFromParamsService |
||||
.new(query, current_user) |
||||
.call(parsed.result) |
||||
else |
||||
parsed |
||||
end |
||||
end |
||||
|
||||
attr_accessor :query, |
||||
:current_user |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,49 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
class WorkPackageCollectionFromQueryParamsService |
||||
def initialize(user) |
||||
self.current_user = user |
||||
end |
||||
|
||||
def call(params = {}) |
||||
query = Query.new(name: '_', project: params[:project], sort_criteria: [['parent', 'desc']]) |
||||
|
||||
WorkPackageCollectionFromQueryService |
||||
.new(query, current_user) |
||||
.call(params) |
||||
end |
||||
|
||||
private |
||||
|
||||
attr_accessor :current_user |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,155 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
class WorkPackageCollectionFromQueryService |
||||
include Utilities::PathHelper |
||||
|
||||
def initialize(query, user) |
||||
self.query = query |
||||
self.current_user = user |
||||
end |
||||
|
||||
def call(params = {}) |
||||
update = UpdateQueryFromV3ParamsService |
||||
.new(query, current_user) |
||||
.call(params) |
||||
|
||||
if update.success? |
||||
representer = results_to_representer(params) |
||||
|
||||
ServiceResult.new(success: true, result: representer) |
||||
else |
||||
update |
||||
end |
||||
end |
||||
|
||||
private |
||||
|
||||
def results_to_representer(params) |
||||
collection_representer(query.results.sorted_work_packages, |
||||
params: params, |
||||
project: query.project, |
||||
groups: generate_groups, |
||||
sums: generate_total_sums) |
||||
end |
||||
|
||||
attr_accessor :query, |
||||
:current_user |
||||
|
||||
def representer |
||||
::API::V3::WorkPackages::WorkPackageCollectionRepresenter |
||||
end |
||||
|
||||
def calculate_resulting_params(provided_params) |
||||
calculate_default_params |
||||
.merge(provided_params.slice('offset', 'pageSize').symbolize_keys) |
||||
end |
||||
|
||||
def calculate_default_params |
||||
::API::V3::Queries::QueryParamsRepresenter |
||||
.new(query) |
||||
.to_h |
||||
end |
||||
|
||||
def generate_groups |
||||
return unless query.grouped? |
||||
|
||||
results = query.results |
||||
|
||||
results.work_package_count_by_group.map do |group, count| |
||||
sums = if query.display_sums? |
||||
format_query_sums results.all_sums_for_group(group) |
||||
end |
||||
|
||||
::API::Decorators::AggregationGroup.new(group, count, sums: sums) |
||||
end |
||||
end |
||||
|
||||
def generate_total_sums |
||||
return unless query.display_sums? |
||||
|
||||
format_query_sums query.results.all_total_sums |
||||
end |
||||
|
||||
def format_query_sums(sums) |
||||
OpenStruct.new(format_column_keys(sums)) |
||||
end |
||||
|
||||
def format_column_keys(hash_by_column) |
||||
::Hash[ |
||||
hash_by_column.map do |column, value| |
||||
match = /cf_(\d+)/.match(column.name.to_s) |
||||
|
||||
column_name = if match |
||||
"custom_field_#{match[1]}" |
||||
else |
||||
column.name.to_s |
||||
end |
||||
|
||||
[column_name, value] |
||||
end |
||||
] |
||||
end |
||||
|
||||
def collection_representer(work_packages, params:, project:, groups:, sums:) |
||||
resulting_params = calculate_resulting_params(params) |
||||
|
||||
::API::V3::WorkPackages::WorkPackageCollectionRepresenter.new( |
||||
work_packages, |
||||
self_link(project), |
||||
project: project, |
||||
query: resulting_params, |
||||
page: to_i_or_nil(resulting_params[:offset]), |
||||
per_page: to_i_or_nil(resulting_params[:pageSize]), |
||||
groups: groups, |
||||
total_sums: sums, |
||||
embed_schemas: true, |
||||
current_user: current_user |
||||
) |
||||
end |
||||
|
||||
def to_i_or_nil(value) |
||||
value ? value.to_i : nil |
||||
end |
||||
|
||||
def self_link(project) |
||||
if project |
||||
api_v3_paths.work_packages_by_project(project.id) |
||||
else |
||||
api_v3_paths.work_packages |
||||
end |
||||
end |
||||
|
||||
def convert_to_v3(attribute) |
||||
::API::Utilities::PropertyNameConverter.from_ar_name(attribute).to_sym |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,84 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
class UpdateQueryFromParamsService |
||||
def initialize(query, user) |
||||
self.query = query |
||||
self.current_user = user |
||||
end |
||||
|
||||
def call(params) |
||||
apply_group_by(params) |
||||
|
||||
apply_sort_by(params) |
||||
|
||||
apply_filters(params) |
||||
|
||||
apply_columns(params) |
||||
|
||||
apply_sums(params) |
||||
|
||||
if query.valid? |
||||
ServiceResult.new(success: true, |
||||
result: query) |
||||
else |
||||
ServiceResult.new(errors: query.errors) |
||||
end |
||||
end |
||||
|
||||
private |
||||
|
||||
def apply_group_by(params) |
||||
query.group_by = params[:group_by] if params[:group_by] |
||||
end |
||||
|
||||
def apply_sort_by(params) |
||||
query.sort_criteria = params[:sort_by] if params[:sort_by] |
||||
end |
||||
|
||||
def apply_filters(params) |
||||
return unless params[:filters] |
||||
query.filters = [] |
||||
|
||||
params[:filters].each do |filter| |
||||
query.add_filter(filter[:field], filter[:operator], filter[:values]) |
||||
end |
||||
end |
||||
|
||||
def apply_columns(params) |
||||
query.column_names = params[:columns] if params[:columns] |
||||
end |
||||
|
||||
def apply_sums(params) |
||||
query.display_sums = params[:display_sums] if params[:display_sums] |
||||
end |
||||
|
||||
attr_accessor :query, |
||||
:current_user, |
||||
:params |
||||
end |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,38 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
# The sole purpose of this is to have a work package |
||||
# that is inexpensive to initialize by overriding the after_initialize hook |
||||
|
||||
module API |
||||
module Utilities |
||||
class PropertyNameConverterWorkPackageDummy < ::WorkPackage |
||||
def set_default_values; end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,47 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
require 'api/utilities/property_name_converter' |
||||
require 'api/utilities/query_filters_name_converter_context' |
||||
|
||||
module API |
||||
module Utilities |
||||
class QueryFiltersNameConverter |
||||
class << self |
||||
def to_ar_name(attribute, refer_to_ids: false) |
||||
conversion_wp = ::API::Utilities::QueryFiltersNameConverterContext.new |
||||
|
||||
::API::Utilities::PropertyNameConverter.to_ar_name(attribute, |
||||
context: conversion_wp, |
||||
refer_to_ids: refer_to_ids) |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,9 @@ |
||||
module API |
||||
module Utilities |
||||
class QueryFiltersNameConverterContext |
||||
def respond_to?(method_name, include_private = false) |
||||
Query.registered_filters.map(&:key).include?(method_name.to_sym) || super |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,47 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
require 'api/utilities/property_name_converter' |
||||
require 'api/utilities/property_name_converter_work_package_dummy' |
||||
|
||||
module API |
||||
module Utilities |
||||
class WpPropertyNameConverter |
||||
class << self |
||||
def to_ar_name(attribute, refer_to_ids: false) |
||||
conversion_wp = ::API::Utilities::PropertyNameConverterWorkPackageDummy.new |
||||
|
||||
::API::Utilities::PropertyNameConverter.to_ar_name(attribute, |
||||
context: conversion_wp, |
||||
refer_to_ids: refer_to_ids) |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,67 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
module Queries |
||||
module Columns |
||||
class QueryColumnRepresenter < ::API::Decorators::Single |
||||
self_link id_attribute: ->(*) { converted_name }, |
||||
title_getter: ->(*) { represented.caption } |
||||
|
||||
def initialize(model) |
||||
super(model, current_user: nil, embed_links: true) |
||||
end |
||||
|
||||
property :id, |
||||
exec_context: :decorator |
||||
|
||||
property :caption, |
||||
as: :name |
||||
|
||||
private |
||||
|
||||
def converted_name |
||||
convert_attribute(represented.name) |
||||
end |
||||
|
||||
alias :id :converted_name |
||||
|
||||
def _type |
||||
'QueryColumn' |
||||
end |
||||
|
||||
def convert_attribute(attribute) |
||||
::API::Utilities::PropertyNameConverter.from_ar_name(attribute) |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,66 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
module Queries |
||||
module Columns |
||||
class QueryColumnsAPI < ::API::OpenProjectAPI |
||||
resource :columns do |
||||
helpers do |
||||
def convert_to_ar(attribute) |
||||
::API::Utilities::WpPropertyNameConverter.to_ar_name(attribute) |
||||
end |
||||
end |
||||
|
||||
params do |
||||
requires :id, desc: 'Column id' |
||||
end |
||||
|
||||
before do |
||||
authorize(:view_work_packages, global: true, user: current_user) |
||||
end |
||||
|
||||
route_param :id do |
||||
get do |
||||
ar_id = convert_to_ar(params[:id]).to_sym |
||||
column = Query.all_columns.detect { |candidate| candidate.name == ar_id } |
||||
|
||||
if column |
||||
::API::V3::Queries::Columns::QueryColumnRepresenter.new(column) |
||||
else |
||||
raise API::Errors::NotFound |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,46 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
module Queries |
||||
module Filters |
||||
class QueryFilterDecorator |
||||
def initialize(filter) |
||||
self.filter = filter |
||||
end |
||||
|
||||
private |
||||
|
||||
attr_accessor :filter |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,96 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
module Queries |
||||
module Filters |
||||
class QueryFilterInstanceRepresenter < ::API::Decorators::Single |
||||
def initialize(model) |
||||
super(model, current_user: nil, embed_links: true) |
||||
end |
||||
|
||||
link :filter do |
||||
{ |
||||
href: api_v3_paths.query_filter(converted_name), |
||||
title: name |
||||
} |
||||
end |
||||
|
||||
link :operator do |
||||
{ |
||||
href: api_v3_paths.query_operator(represented.operator), |
||||
title: operator_name |
||||
} |
||||
end |
||||
|
||||
links :values do |
||||
next unless represented.ar_object_filter? |
||||
|
||||
represented.value_objects.map do |value_object| |
||||
{ |
||||
href: api_v3_paths.send(value_object.class.name.downcase, value_object.id), |
||||
title: value_object.name |
||||
} |
||||
end |
||||
end |
||||
|
||||
property :name, |
||||
exec_context: :decorator |
||||
|
||||
property :values, |
||||
if: ->(*) { !ar_object_filter? }, |
||||
show_nil: true |
||||
|
||||
private |
||||
|
||||
def name |
||||
represented.human_name |
||||
end |
||||
|
||||
def _type |
||||
"#{converted_name.camelize}QueryFilter" |
||||
end |
||||
|
||||
def converted_name |
||||
convert_attribute(represented.name) |
||||
end |
||||
|
||||
def operator_name |
||||
I18n.t(represented.class.operators[represented.operator.to_sym]) |
||||
end |
||||
|
||||
def convert_attribute(attribute) |
||||
::API::Utilities::PropertyNameConverter.from_ar_name(attribute) |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,65 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
module Queries |
||||
module Filters |
||||
class QueryFilterRepresenter < ::API::Decorators::Single |
||||
self_link id_attribute: ->(*) { converted_key }, |
||||
title_getter: ->(*) { represented.human_name }, |
||||
path: :query_filter |
||||
|
||||
def initialize(model) |
||||
super(model, current_user: nil, embed_links: true) |
||||
end |
||||
|
||||
property :id, |
||||
exec_context: :decorator |
||||
|
||||
private |
||||
|
||||
def converted_key |
||||
convert_attribute(represented.name) |
||||
end |
||||
|
||||
alias :id :converted_key |
||||
|
||||
def _type |
||||
'QueryFilter' |
||||
end |
||||
|
||||
def convert_attribute(attribute) |
||||
::API::Utilities::PropertyNameConverter.from_ar_name(attribute) |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,71 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
module Queries |
||||
module Filters |
||||
class QueryFiltersAPI < ::API::OpenProjectAPI |
||||
resource :filters do |
||||
helpers do |
||||
def convert_to_ar(attribute) |
||||
::API::Utilities::QueryFiltersNameConverter.to_ar_name(attribute, |
||||
refer_to_ids: true) |
||||
end |
||||
end |
||||
|
||||
params do |
||||
requires :id, desc: 'Filter id' |
||||
end |
||||
|
||||
before do |
||||
authorize(:view_work_packages, global: true, user: current_user) |
||||
end |
||||
|
||||
route_param :id do |
||||
get do |
||||
ar_id = convert_to_ar(params[:id]) |
||||
|
||||
filter_class = Query.find_registered_filter(ar_id) |
||||
|
||||
if filter_class |
||||
filter = filter_class.new |
||||
filter.name = ar_id |
||||
|
||||
::API::V3::Queries::Filters::QueryFilterRepresenter.new(filter) |
||||
else |
||||
raise API::Errors::NotFound |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,67 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
module Queries |
||||
module GroupBys |
||||
class QueryGroupByRepresenter < ::API::Decorators::Single |
||||
self_link id_attribute: ->(*) { converted_name }, |
||||
title_getter: ->(*) { represented.caption } |
||||
|
||||
def initialize(model) |
||||
super(model, current_user: nil, embed_links: true) |
||||
end |
||||
|
||||
property :id, |
||||
exec_context: :decorator |
||||
|
||||
property :caption, |
||||
as: :name |
||||
|
||||
private |
||||
|
||||
def converted_name |
||||
convert_attribute(represented.name) |
||||
end |
||||
|
||||
alias :id :converted_name |
||||
|
||||
def _type |
||||
'QueryGroupBy' |
||||
end |
||||
|
||||
def convert_attribute(attribute) |
||||
::API::Utilities::PropertyNameConverter.from_ar_name(attribute) |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,66 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
module Queries |
||||
module GroupBys |
||||
class QueryGroupBysAPI < ::API::OpenProjectAPI |
||||
resource :group_bys do |
||||
helpers do |
||||
def convert_to_ar(attribute) |
||||
::API::Utilities::WpPropertyNameConverter.to_ar_name(attribute) |
||||
end |
||||
end |
||||
|
||||
params do |
||||
requires :id, desc: 'Group by id' |
||||
end |
||||
|
||||
before do |
||||
authorize(:view_work_packages, global: true, user: current_user) |
||||
end |
||||
|
||||
route_param :id do |
||||
get do |
||||
ar_id = convert_to_ar(params[:id]).to_sym |
||||
column = Query.groupable_columns.detect { |candidate| candidate.name == ar_id } |
||||
|
||||
if column |
||||
::API::V3::Queries::GroupBys::QueryGroupByRepresenter.new(column) |
||||
else |
||||
raise API::Errors::NotFound |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,63 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
module Queries |
||||
module Operators |
||||
class QueryOperatorRepresenter < ::API::Decorators::Single |
||||
self_link id_attribute: ->(*) { represented }, |
||||
title_getter: ->(*) { name } |
||||
|
||||
def initialize(model) |
||||
super(model.to_sym, current_user: nil, embed_links: true) |
||||
end |
||||
|
||||
property :id, |
||||
exec_context: :decorator |
||||
|
||||
property :name, |
||||
exec_context: :decorator |
||||
|
||||
private |
||||
|
||||
def name |
||||
I18n.t(::Queries::BaseFilter.operators[represented]) |
||||
end |
||||
|
||||
alias :id :represented |
||||
|
||||
def _type |
||||
'QueryOperator' |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,57 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
module Queries |
||||
module Operators |
||||
class QueryOperatorsAPI < ::API::OpenProjectAPI |
||||
resource :operators do |
||||
params do |
||||
requires :id, desc: 'Operator id' |
||||
end |
||||
|
||||
before do |
||||
authorize(:view_work_packages, global: true, user: current_user) |
||||
end |
||||
|
||||
route_param :id do |
||||
get do |
||||
if ::Queries::BaseFilter.operators[params[:id].to_sym] |
||||
::API::V3::Queries::Operators::QueryOperatorRepresenter.new(params[:id]) |
||||
else |
||||
raise API::Errors::NotFound |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,90 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
# Other than the Roar based representers of the api v3, this |
||||
# representer is only responsible for transforming a query's |
||||
# attributes into a hash which in turn can be used e.g. to be displayed |
||||
# in a url |
||||
|
||||
module API |
||||
module V3 |
||||
module Queries |
||||
class QueryParamsRepresenter |
||||
def initialize(query) |
||||
self.query = query |
||||
end |
||||
|
||||
def to_h |
||||
p = default_hash |
||||
|
||||
p[:showSums] = 'true' if query.display_sums? |
||||
p[:groupBy] = query.group_by if query.group_by? |
||||
p[:sortBy] = sort_criteria_to_v3 if query.sorted? |
||||
p[:filters] = filters_to_v3 if query.filtered? |
||||
|
||||
p |
||||
end |
||||
|
||||
def self_link |
||||
if query.project |
||||
api_v3_paths.work_packages_by_project(query.project.id) |
||||
else |
||||
api_v3_paths.work_packages |
||||
end |
||||
end |
||||
|
||||
private |
||||
|
||||
def sort_criteria_to_v3 |
||||
converted = query.sort_criteria.map { |first, last| [convert_to_v3(first), last] } |
||||
|
||||
JSON::dump(converted) |
||||
end |
||||
|
||||
def filters_to_v3 |
||||
converted = query.filters.map do |filter| |
||||
{ convert_to_v3(filter.name) => { operator: filter.operator, values: filter.values } } |
||||
end |
||||
|
||||
JSON::dump(converted) |
||||
end |
||||
|
||||
def convert_to_v3(attribute) |
||||
::API::Utilities::PropertyNameConverter.from_ar_name(attribute).to_sym |
||||
end |
||||
|
||||
def default_hash |
||||
{ offset: 1, pageSize: Setting.per_page_options_array.first } |
||||
end |
||||
|
||||
attr_accessor :query |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,73 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
module Queries |
||||
module SortBys |
||||
class QuerySortByRepresenter < ::API::Decorators::Single |
||||
self_link id_attribute: ->(*) { self_link_params }, |
||||
title_getter: ->(*) { represented.name } |
||||
|
||||
def initialize(model) |
||||
super(model, current_user: nil, embed_links: true) |
||||
end |
||||
|
||||
link :column do |
||||
{ |
||||
href: api_v3_paths.query_column(represented.converted_name), |
||||
title: represented.column_caption |
||||
} |
||||
end |
||||
|
||||
link :direction do |
||||
{ |
||||
href: represented.direction_uri, |
||||
title: represented.direction_l10n |
||||
} |
||||
end |
||||
|
||||
property :id |
||||
|
||||
property :name |
||||
|
||||
private |
||||
|
||||
def self_link_params |
||||
[represented.converted_name, represented.direction_name] |
||||
end |
||||
|
||||
def _type |
||||
'QuerySortBy' |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,68 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
module Queries |
||||
module SortBys |
||||
class QuerySortBysAPI < ::API::OpenProjectAPI |
||||
resource :sort_bys do |
||||
helpers do |
||||
def convert_to_ar(attribute) |
||||
::API::Utilities::WpPropertyNameConverter.to_ar_name(attribute) |
||||
end |
||||
end |
||||
|
||||
params do |
||||
requires :id, desc: 'Group by id' |
||||
requires :direction, desc: 'Direction of sorting' |
||||
end |
||||
|
||||
before do |
||||
authorize(:view_work_packages, global: true, user: current_user) |
||||
end |
||||
|
||||
namespace ':id-:direction' do |
||||
get do |
||||
ar_id = convert_to_ar(params[:id]) |
||||
|
||||
begin |
||||
decorator = ::API::V3::Queries::SortBys::SortByDecorator.new(ar_id, |
||||
params[:direction]) |
||||
::API::V3::Queries::SortBys::QuerySortByRepresenter.new(decorator) |
||||
rescue ArgumentError |
||||
raise API::Errors::NotFound |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,99 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
module Queries |
||||
module SortBys |
||||
class SortByDecorator |
||||
def initialize(column_name, direction) |
||||
if !['asc', 'desc'].include?(direction) |
||||
raise ArgumentError, "Invalid direction. Only 'asc' and 'desc' are supported." |
||||
end |
||||
|
||||
self.direction = direction |
||||
|
||||
column_sym = column_name.to_sym |
||||
column = Query |
||||
.sortable_columns |
||||
.detect { |candidate| candidate.name == column_sym } |
||||
|
||||
if column.nil? |
||||
raise ArgumentError, "Invalid column name." |
||||
end |
||||
|
||||
self.column = column |
||||
end |
||||
|
||||
def id |
||||
"#{converted_name}-#{direction_name}" |
||||
end |
||||
|
||||
def name |
||||
I18n.t('query.attribute_and_direction', |
||||
attribute: column_caption, |
||||
direction: direction_l10n) |
||||
end |
||||
|
||||
def converted_name |
||||
convert_attribute(column_name) |
||||
end |
||||
|
||||
def direction_name |
||||
direction |
||||
end |
||||
|
||||
def direction_uri |
||||
"urn:openproject-org:api:v3:queries:directions:#{direction}" |
||||
end |
||||
|
||||
def direction_l10n |
||||
I18n.t(direction == 'desc' ? :label_descending : :label_ascending) |
||||
end |
||||
|
||||
def column_name |
||||
column.name |
||||
end |
||||
|
||||
def column_caption |
||||
column.caption |
||||
end |
||||
|
||||
private |
||||
|
||||
def convert_attribute(attribute) |
||||
::API::Utilities::PropertyNameConverter.from_ar_name(attribute) |
||||
end |
||||
|
||||
attr_accessor :direction, :column |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -1,210 +0,0 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
module API |
||||
module V3 |
||||
module WorkPackages |
||||
module WorkPackageListHelpers |
||||
extend Grape::API::Helpers |
||||
include QueriesHelper |
||||
|
||||
def work_packages_by_params(project: nil) |
||||
query = Query.new(name: '_', project: project) |
||||
query_params = {} |
||||
|
||||
begin |
||||
apply_filters query, query_params |
||||
apply_sorting query, query_params |
||||
groups = apply_and_generate_groups query, query_params |
||||
|
||||
total_sums = generate_total_sums query.results, query_params |
||||
rescue ::JSON::ParserError => error |
||||
raise ::API::Errors::InvalidQuery.new(error.message) |
||||
end |
||||
|
||||
work_packages = query |
||||
.results |
||||
.sorted_work_packages |
||||
|
||||
collection_representer(work_packages, |
||||
project: project, |
||||
query_params: query_params, |
||||
groups: groups, |
||||
sums: total_sums) |
||||
end |
||||
|
||||
def apply_filters(query, query_params) |
||||
if params[:filters] |
||||
filters = parse_filters_from_json(params[:filters]) |
||||
set_filters(query, filters) |
||||
query_params[:filters] = params[:filters] |
||||
end |
||||
end |
||||
|
||||
# Expected format looks like: |
||||
# [ |
||||
# { |
||||
# "filtered_field_name": { |
||||
# "operator": "a name for a filter operation", |
||||
# "values": ["values", "for the", "operation"] |
||||
# } |
||||
# }, |
||||
# { /* more filters if needed */} |
||||
# ] |
||||
def parse_filters_from_json(json) |
||||
filters = JSON.parse(json) |
||||
operators = {} |
||||
values = {} |
||||
filters.each do |filter| |
||||
attribute = filter.keys.first # there should only be one attribute per filter |
||||
operators[attribute] = filter[attribute]['operator'] |
||||
values[attribute] = filter[attribute]['values'] |
||||
end |
||||
|
||||
{ |
||||
fields: values.keys, |
||||
operators: operators, |
||||
values: values |
||||
} |
||||
end |
||||
|
||||
def set_filters(query, filters) |
||||
add_filter_from_params(query, filters: filters) |
||||
|
||||
bad_filter = query.filters.detect(&:invalid?) |
||||
if bad_filter |
||||
raise_invalid_query(bad_filter.errors) |
||||
end |
||||
end |
||||
|
||||
def apply_sorting(query, query_params) |
||||
if params[:sortBy] |
||||
query.sort_criteria = parse_sorting_from_json(params[:sortBy]) |
||||
query_params[:sortBy] = params[:sortBy] |
||||
else |
||||
query.sort_criteria = [['parent', 'desc']] |
||||
query_params[:sortBy] = 'parent:desc' |
||||
end |
||||
end |
||||
|
||||
def parse_sorting_from_json(json) |
||||
JSON.parse(json).map do |(attribute, order)| |
||||
[convert_attribute(attribute), order] |
||||
end |
||||
end |
||||
|
||||
def apply_and_generate_groups(query, query_params) |
||||
if params[:groupBy] |
||||
query.group_by = convert_attribute params[:groupBy] |
||||
query_params[:groupBy] = params[:groupBy] |
||||
|
||||
generate_groups query.results |
||||
end |
||||
end |
||||
|
||||
def generate_groups(results) |
||||
results.work_package_count_by_group.map do |group, count| |
||||
sums = nil |
||||
if params[:showSums] == 'true' |
||||
sums = format_query_sums results.all_sums_for_group(group) |
||||
end |
||||
|
||||
::API::Decorators::AggregationGroup.new(group, count, sums: sums) |
||||
end |
||||
end |
||||
|
||||
def generate_total_sums(results, query_params) |
||||
if params[:showSums] == 'true' |
||||
query_params[:showSums] = 'true' |
||||
format_query_sums results.all_total_sums |
||||
end |
||||
end |
||||
|
||||
def format_query_sums(sums) |
||||
OpenStruct.new(format_column_keys(sums)) |
||||
end |
||||
|
||||
def format_column_keys(hash_by_column) |
||||
::Hash[ |
||||
hash_by_column.map do |column, value| |
||||
match = /cf_(\d+)/.match(column.name.to_s) |
||||
|
||||
column_name = if match |
||||
"custom_field_#{match[1]}" |
||||
else |
||||
column.name.to_s |
||||
end |
||||
|
||||
[column_name, value] |
||||
end |
||||
] |
||||
end |
||||
|
||||
def collection_representer(work_packages, project:, query_params:, groups:, sums:) |
||||
self_link = if project |
||||
api_v3_paths.work_packages_by_project(project.id) |
||||
else |
||||
api_v3_paths.work_packages |
||||
end |
||||
|
||||
::API::V3::WorkPackages::WorkPackageCollectionRepresenter.new( |
||||
work_packages, |
||||
self_link, |
||||
project: project, |
||||
query: query_params, |
||||
page: to_i_or_nil(params[:offset]), |
||||
per_page: to_i_or_nil(params[:pageSize]), |
||||
groups: groups, |
||||
total_sums: sums, |
||||
embed_schemas: true, |
||||
current_user: current_user |
||||
) |
||||
end |
||||
|
||||
def convert_attribute(attribute, append_id: false) |
||||
@@conversion_wp ||= ::API::Utilities::PropertyNameConverterQueryContext.new |
||||
::API::Utilities::PropertyNameConverter.to_ar_name(attribute, |
||||
context: @@conversion_wp, |
||||
refer_to_ids: append_id) |
||||
end |
||||
|
||||
def raise_invalid_query(errors) |
||||
api_errors = errors.full_messages.map do |message| |
||||
::API::Errors::InvalidQuery.new(message) |
||||
end |
||||
|
||||
raise ::API::Errors::MultipleErrors.create_if_many api_errors |
||||
end |
||||
|
||||
def to_i_or_nil(value) |
||||
value ? value.to_i : nil |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,90 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
require 'spec_helper' |
||||
|
||||
describe ::API::V3::Queries::Columns::QueryColumnRepresenter do |
||||
include ::API::V3::Utilities::PathHelper |
||||
|
||||
let(:column) { Query.available_columns.detect { |column| column.name == :status } } |
||||
let(:representer) { described_class.new(column) } |
||||
|
||||
subject { representer.to_json } |
||||
|
||||
describe 'generation' do |
||||
describe '_links' do |
||||
it_behaves_like 'has a titled link' do |
||||
let(:link) { 'self' } |
||||
let(:href) { api_v3_paths.query_column 'status' } |
||||
let(:title) { 'Status' } |
||||
end |
||||
end |
||||
|
||||
it 'has _type QueryColumn' do |
||||
is_expected |
||||
.to be_json_eql('QueryColumn'.to_json) |
||||
.at_path('_type') |
||||
end |
||||
|
||||
it 'has id attribute' do |
||||
is_expected |
||||
.to be_json_eql('status'.to_json) |
||||
.at_path('id') |
||||
end |
||||
|
||||
it 'has name attribute' do |
||||
is_expected |
||||
.to be_json_eql('Status'.to_json) |
||||
.at_path('name') |
||||
end |
||||
|
||||
context 'for a translated column' do |
||||
let(:column) { Query.available_columns.detect { |column| column.name == :assigned_to } } |
||||
|
||||
describe '_links' do |
||||
it_behaves_like 'has a titled link' do |
||||
let(:link) { 'self' } |
||||
let(:href) { api_v3_paths.query_column 'assignee' } |
||||
let(:title) { 'Assignee' } |
||||
end |
||||
end |
||||
|
||||
it 'has id attribute' do |
||||
is_expected |
||||
.to be_json_eql('assignee'.to_json) |
||||
.at_path('id') |
||||
end |
||||
|
||||
it 'has name attribute' do |
||||
is_expected |
||||
.to be_json_eql('Assignee'.to_json) |
||||
.at_path('name') |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,111 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
require 'spec_helper' |
||||
|
||||
describe ::API::V3::Queries::Filters::QueryFilterInstanceRepresenter do |
||||
include ::API::V3::Utilities::PathHelper |
||||
|
||||
let(:operator) { '=' } |
||||
let(:values) { [status.id.to_s] } |
||||
|
||||
let(:status) { FactoryGirl.build_stubbed(:status) } |
||||
|
||||
let(:filter) do |
||||
Queries::WorkPackages::Filter::StatusFilter.new(operator: operator, values: values) |
||||
end |
||||
|
||||
let(:representer) { described_class.new(filter) } |
||||
|
||||
before do |
||||
allow(filter) |
||||
.to receive(:value_objects) |
||||
.and_return([status]) |
||||
end |
||||
|
||||
describe 'generation' do |
||||
subject { representer.to_json } |
||||
|
||||
describe '_links' do |
||||
it_behaves_like 'has a titled link' do |
||||
let(:link) { 'filter' } |
||||
let(:href) { api_v3_paths.query_filter 'status' } |
||||
let(:title) { 'Status' } |
||||
end |
||||
|
||||
it_behaves_like 'has a titled link' do |
||||
let(:link) { 'operator' } |
||||
let(:href) { api_v3_paths.query_operator '=' } |
||||
let(:title) { 'is' } |
||||
end |
||||
|
||||
it "has a 'values' collection" do |
||||
expected = { |
||||
href: api_v3_paths.status(status.id.to_s), |
||||
title: status.name |
||||
} |
||||
|
||||
is_expected |
||||
.to be_json_eql([expected].to_json) |
||||
.at_path('_links/values') |
||||
end |
||||
end |
||||
|
||||
it 'has _type StatusQueryFilter' do |
||||
is_expected |
||||
.to be_json_eql('StatusQueryFilter'.to_json) |
||||
.at_path('_type') |
||||
end |
||||
|
||||
it 'has name Status' do |
||||
is_expected |
||||
.to be_json_eql('Status'.to_json) |
||||
.at_path('name') |
||||
end |
||||
|
||||
context 'with a non ar object filter' do |
||||
let(:values) { ['lorem ipsum'] } |
||||
let(:filter) do |
||||
Queries::WorkPackages::Filter::SubjectFilter.new(operator: operator, values: values) |
||||
end |
||||
|
||||
describe '_links' do |
||||
it 'has no values link' do |
||||
is_expected |
||||
.not_to have_json_path('_links/values') |
||||
end |
||||
end |
||||
|
||||
it "has a 'values' array property" do |
||||
is_expected |
||||
.to be_json_eql(values.to_json) |
||||
.at_path('values') |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,106 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
require 'spec_helper' |
||||
|
||||
describe ::API::V3::Queries::Filters::QueryFilterRepresenter do |
||||
include ::API::V3::Utilities::PathHelper |
||||
|
||||
let(:filter) { Queries::WorkPackages::Filter::SubjectFilter.new } |
||||
let(:representer) { described_class.new(filter) } |
||||
|
||||
subject { representer.to_json } |
||||
|
||||
describe 'generation' do |
||||
describe '_links' do |
||||
it_behaves_like 'has a titled link' do |
||||
let(:link) { 'self' } |
||||
let(:href) { api_v3_paths.query_filter 'subject' } |
||||
let(:title) { 'Subject' } |
||||
end |
||||
end |
||||
|
||||
it 'has _type QueryFilter' do |
||||
is_expected |
||||
.to be_json_eql('QueryFilter'.to_json) |
||||
.at_path('_type') |
||||
end |
||||
|
||||
it 'has id attribute' do |
||||
is_expected |
||||
.to be_json_eql('subject'.to_json) |
||||
.at_path('id') |
||||
end |
||||
|
||||
context 'for a translated filter' do |
||||
let(:filter) { Queries::WorkPackages::Filter::AssignedToFilter.new } |
||||
|
||||
describe '_links' do |
||||
it_behaves_like 'has a titled link' do |
||||
let(:link) { 'self' } |
||||
let(:href) { api_v3_paths.query_filter 'assignee' } |
||||
let(:title) { 'Assignee' } |
||||
end |
||||
end |
||||
|
||||
it 'has id attribute' do |
||||
is_expected |
||||
.to be_json_eql('assignee'.to_json) |
||||
.at_path('id') |
||||
end |
||||
end |
||||
|
||||
context 'for a custom field filter' do |
||||
let(:custom_field) { FactoryGirl.build_stubbed(:list_wp_custom_field) } |
||||
let(:filter) { Queries::WorkPackages::Filter::CustomFieldFilter.new } |
||||
|
||||
before do |
||||
allow(WorkPackageCustomField) |
||||
.to receive(:find_by_id) |
||||
.with(custom_field.id) |
||||
.and_return custom_field |
||||
|
||||
filter.name = "cf_#{custom_field.id}" |
||||
end |
||||
|
||||
describe '_links' do |
||||
it_behaves_like 'has a titled link' do |
||||
let(:link) { 'self' } |
||||
let(:href) { api_v3_paths.query_filter "customField#{custom_field.id}" } |
||||
let(:title) { custom_field.name } |
||||
end |
||||
end |
||||
|
||||
it 'has id attribute' do |
||||
is_expected |
||||
.to be_json_eql("customField#{custom_field.id}".to_json) |
||||
.at_path('id') |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,90 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
require 'spec_helper' |
||||
|
||||
describe ::API::V3::Queries::GroupBys::QueryGroupByRepresenter do |
||||
include ::API::V3::Utilities::PathHelper |
||||
|
||||
let(:column) { Query.available_columns.detect { |column| column.name == :status } } |
||||
let(:representer) { described_class.new(column) } |
||||
|
||||
subject { representer.to_json } |
||||
|
||||
describe 'generation' do |
||||
describe '_links' do |
||||
it_behaves_like 'has a titled link' do |
||||
let(:link) { 'self' } |
||||
let(:href) { api_v3_paths.query_group_by 'status' } |
||||
let(:title) { 'Status' } |
||||
end |
||||
end |
||||
|
||||
it 'has _type QueryGroupBy' do |
||||
is_expected |
||||
.to be_json_eql('QueryGroupBy'.to_json) |
||||
.at_path('_type') |
||||
end |
||||
|
||||
it 'has id attribute' do |
||||
is_expected |
||||
.to be_json_eql('status'.to_json) |
||||
.at_path('id') |
||||
end |
||||
|
||||
it 'has name attribute' do |
||||
is_expected |
||||
.to be_json_eql('Status'.to_json) |
||||
.at_path('name') |
||||
end |
||||
|
||||
context 'for a translated column' do |
||||
let(:column) { Query.available_columns.detect { |column| column.name == :assigned_to } } |
||||
|
||||
describe '_links' do |
||||
it_behaves_like 'has a titled link' do |
||||
let(:link) { 'self' } |
||||
let(:href) { api_v3_paths.query_group_by 'assignee' } |
||||
let(:title) { 'Assignee' } |
||||
end |
||||
end |
||||
|
||||
it 'has id attribute' do |
||||
is_expected |
||||
.to be_json_eql('assignee'.to_json) |
||||
.at_path('id') |
||||
end |
||||
|
||||
it 'has name attribute' do |
||||
is_expected |
||||
.to be_json_eql('Assignee'.to_json) |
||||
.at_path('name') |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,66 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
require 'spec_helper' |
||||
|
||||
describe ::API::V3::Queries::Operators::QueryOperatorRepresenter do |
||||
include ::API::V3::Utilities::PathHelper |
||||
|
||||
let(:operator) { '!~' } |
||||
let(:representer) { described_class.new(operator) } |
||||
|
||||
subject { representer.to_json } |
||||
|
||||
describe 'generation' do |
||||
describe '_links' do |
||||
it_behaves_like 'has a titled link' do |
||||
let(:link) { 'self' } |
||||
let(:href) { api_v3_paths.query_operator operator } |
||||
let(:title) { I18n.t(:label_not_contains) } |
||||
end |
||||
end |
||||
|
||||
it 'has _type QueryOperator' do |
||||
is_expected |
||||
.to be_json_eql('QueryOperator'.to_json) |
||||
.at_path('_type') |
||||
end |
||||
|
||||
it 'has id attribute' do |
||||
is_expected |
||||
.to be_json_eql(operator.to_json) |
||||
.at_path('id') |
||||
end |
||||
|
||||
it 'has name attribute' do |
||||
is_expected |
||||
.to be_json_eql(I18n.t(:label_not_contains).to_json) |
||||
.at_path('name') |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,130 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2017 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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
require 'spec_helper' |
||||
|
||||
describe ::API::V3::Queries::SortBys::QuerySortByRepresenter do |
||||
include ::API::V3::Utilities::PathHelper |
||||
|
||||
let(:column) { 'status' } |
||||
let(:direction) { 'desc' } |
||||
let(:representer) do |
||||
described_class |
||||
.new(::API::V3::Queries::SortBys::SortByDecorator.new(column, direction)) |
||||
end |
||||
|
||||
subject { representer.to_json } |
||||
|
||||
describe 'generation' do |
||||
describe '_links' do |
||||
it_behaves_like 'has a titled link' do |
||||
let(:link) { 'self' } |
||||
let(:href) { api_v3_paths.query_sort_by 'status', 'desc' } |
||||
let(:title) { 'Status (Descending)' } |
||||
end |
||||
end |
||||
|
||||
it 'has _type QuerySortBy' do |
||||
is_expected |
||||
.to be_json_eql('QuerySortBy'.to_json) |
||||
.at_path('_type') |
||||
end |
||||
|
||||
it 'has id attribute' do |
||||
is_expected |
||||
.to be_json_eql('status-desc'.to_json) |
||||
.at_path('id') |
||||
end |
||||
|
||||
it 'has name attribute' do |
||||
is_expected |
||||
.to be_json_eql('Status (Descending)'.to_json) |
||||
.at_path('name') |
||||
end |
||||
|
||||
it_behaves_like 'has a titled link' do |
||||
let(:link) { 'column' } |
||||
let(:href) { api_v3_paths.query_column 'status' } |
||||
let(:title) { 'Status' } |
||||
end |
||||
|
||||
it_behaves_like 'has a titled link' do |
||||
let(:link) { 'direction' } |
||||
let(:href) { "urn:openproject-org:api:v3:queries:directions:#{direction}" } |
||||
let(:title) { 'Descending' } |
||||
end |
||||
|
||||
context 'when providing an unsupported sort direction' do |
||||
let(:direction) { 'bogus' } |
||||
|
||||
it 'raises error' do |
||||
expect { subject }.to raise_error(ArgumentError) |
||||
end |
||||
end |
||||
|
||||
context 'when sorting differently' do |
||||
let(:direction) { 'asc' } |
||||
|
||||
it 'has id attribute' do |
||||
is_expected |
||||
.to be_json_eql('status-asc'.to_json) |
||||
.at_path('id') |
||||
end |
||||
|
||||
it 'has name attribute' do |
||||
is_expected |
||||
.to be_json_eql('Status (Ascending)'.to_json) |
||||
.at_path('name') |
||||
end |
||||
end |
||||
|
||||
context 'for a translated column' do |
||||
let(:column) { 'assigned_to' } |
||||
|
||||
describe '_links' do |
||||
it_behaves_like 'has a titled link' do |
||||
let(:link) { 'self' } |
||||
let(:href) { api_v3_paths.query_sort_by 'assignee', 'desc' } |
||||
let(:title) { 'Assignee (Descending)' } |
||||
end |
||||
end |
||||
|
||||
it 'has id attribute' do |
||||
is_expected |
||||
.to be_json_eql('assignee-desc'.to_json) |
||||
.at_path('id') |
||||
end |
||||
|
||||
it 'has name attribute' do |
||||
is_expected |
||||
.to be_json_eql('Assignee (Descending)'.to_json) |
||||
.at_path('name') |
||||
end |
||||
end |
||||
end |
||||
end |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue