OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/lib/api/v3/queries/schemas/query_schema_representer.rb

241 lines
9.2 KiB

#-- encoding: UTF-8
Fix/bump representable (#5465) * bump reform and roar -> bumps representer * adapt to changed validation interface * disable initializer patch for now * adapt to changed representable attr interface * can no longer have private methods inside a representer * private no longer possible for representer * bump reform * wip - restyle validation * remove commented out patch * apply injection as prescribed * reactivate reform error symbols patch * remove patch to Hash superfluous wit ruby 2.3 * remove outdated human_attribute_name patch * whitespace fixes * adapt filter name after removal of human_attribute_name patch * adapt filter specs to no longer rely on human_attribute_name patch * fix version filter name * remove reliance on no longer existing human_attribute_name patch * use correct key in journal formatter * remove private from representer * adapt to altered setter interface * reenable i18n for error messages in contracts * no private methods in representer * defined model for contracts * fix validaton * instantiate correct Object * define model for contract * circumvent now existing render method on reform * replace deprecated constant * patch correct reform class - not the module - via prepend * refactor too complex method * replace deprations * remove remnants of parentId * prevent error symbols from existing twice * adapt user representer to altered setter interface * adapt watcher representer to altered setter interface * remove now unnessary patch * adapt setter to altered interface * adapt spec * fix custom field setters * remove parentId from wp representer As the parent is a wp resource, clients should use the parent link instead * adapt spec to changed valid? interface * remove parentId from wp schema * replace references of parentId in frontend * remove TODO [ci skip]
8 years ago
#-- 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 'roar/decorator'
require 'roar/json/hal'
module API
module V3
module Queries
module Schemas
class QuerySchemaRepresenter < ::API::Decorators::SchemaRepresenter
def initialize(represented, self_link = nil, current_user: nil, form_embedded: false)
super(represented,
self_link,
current_user: current_user,
form_embedded: form_embedded)
end
def self.filters_schema
->(*) do
{
'type': '[]QueryFilterInstance',
'name': Query.human_attribute_name('filters'),
'required': false,
'writable': true,
'hasDefault': true,
'_links': {
'allowedValuesSchemas': {
'href': filter_instance_schemas_href
}
}
}
end
end
schema :id,
type: 'Integer',
visibility: false
schema :name,
type: 'String',
writable: true,
min_length: 1,
max_length: 255,
visibility: false
schema :user,
type: 'User',
has_default: true,
visibility: false
schema_with_allowed_link :project,
type: 'Project',
required: false,
writable: true,
visibility: false,
href_callback: ->(*) {
api_v3_paths.query_available_projects
}
schema :public,
type: 'Boolean',
required: false,
writable: true,
has_default: true,
visibility: false
schema :sums,
type: 'Boolean',
required: false,
writable: true,
has_default: true,
visibility: false
schema :timeline_visible,
type: 'Boolean',
required: false,
writable: true,
has_default: true,
visibility: false
schema :timeline_zoom_level,
type: 'String',
required: false,
writable: true,
has_default: true,
visibility: false
schema :timeline_labels,
type: 'QueryTimelineLabels',
required: false,
writable: true,
has_default: true,
visibility: false
schema :show_hierarchies,
type: 'Boolean',
required: false,
writable: true,
has_default: true,
visibility: false
schema :starred,
type: 'Boolean',
required: false,
writable: false,
has_default: true,
visibility: false
schema_with_allowed_collection :columns,
type: '[]QueryColumn',
required: false,
writable: true,
has_default: true,
visibility: false,
values_callback: -> { represented.available_columns },
value_representer: ->(column) {
Columns::QueryColumnsFactory.representer(column)
},
link_factory: ->(column) {
converted_name = convert_attribute(column.name)
{
href: api_v3_paths.query_column(converted_name),
title: column.caption
}
}
schema_property :filters,
filters_schema,
true,
false,
true,
:filters
schema_with_allowed_collection :group_by,
type: '[]QueryGroupBy',
required: false,
writable: true,
visibility: false,
values_callback: -> { represented.groupable_columns },
value_representer: GroupBys::QueryGroupByRepresenter,
link_factory: ->(column) {
converted_name = convert_attribute(column.name)
{
href: api_v3_paths.query_group_by(converted_name),
title: column.caption
}
}
schema_with_allowed_collection :sort_by,
type: '[]QuerySortBy',
required: false,
writable: true,
has_default: true,
visibility: false,
values_callback: -> do
values = represented.sortable_columns.map do |column|
[SortBys::SortByDecorator.new(column, 'asc'),
SortBys::SortByDecorator.new(column, 'desc')]
end
values.flatten
end,
value_representer: SortBys::QuerySortByRepresenter,
link_factory: ->(sort_by) {
name = sort_by.converted_name
direction = sort_by.direction_name
{
href: api_v3_paths.query_sort_by(name, direction),
title: sort_by.name
}
}
schema :results,
type: 'WorkPackageCollection',
required: false,
writable: false,
visibility: false
property :filters_schemas,
embedded: true,
exec_context: :decorator
def self.represented_class
Query
end
def convert_attribute(attribute)
::API::Utilities::PropertyNameConverter.from_ar_name(attribute)
end
def filters_schemas
filters = represented.available_filters
QueryFilterInstanceSchemaCollectionRepresenter.new(filters,
filter_instance_schemas_href,
form_embedded: form_embedded,
current_user: current_user)
end
def filter_instance_schemas_href
if represented.project
api_v3_paths.query_project_filter_instance_schemas(represented.project.id)
else
api_v3_paths.query_filter_instance_schemas
end
end
end
end
end
end
end