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/query_representer.rb

246 lines
6.5 KiB

#-- 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 'roar/decorator'
require 'roar/json/hal'
module API
module V3
module Queries
class QueryRepresenter < ::API::Decorators::Single
self_link
prepend QuerySerialization
attr_accessor :results,
:params
def initialize(model,
current_user:,
results: nil,
embed_links: false,
params: {})
self.results = results
self.params = params
super(model, current_user: current_user, embed_links: embed_links)
end
link :results do
path = if represented.project
api_v3_paths.work_packages_by_project(represented.project.id)
else
api_v3_paths.work_packages
end
url_query = ::API::V3::Queries::QueryParamsRepresenter
.new(represented)
.to_h
.merge(params.slice(:offset, :pageSize))
{
href: [path, url_query.to_query].join('?')
}
end
link :star do
next if represented.starred || !allowed_to?(:star)
{
href: api_v3_paths.query_star(represented.id),
method: :patch
}
end
link :unstar do
next unless represented.starred && allowed_to?(:unstar)
{
href: api_v3_paths.query_unstar(represented.id),
method: :patch
}
end
links :columns do
represented.columns.map do |column|
{
href: api_v3_paths.query_column(convert_attribute(column.name)),
title: column.caption
}
end
end
8 years ago
link :groupBy do
column = represented.group_by_column
if column
{
href: api_v3_paths.query_group_by(convert_attribute(column.name)),
8 years ago
title: column.caption
}
else
{
href: nil,
title: nil
}
end
end
8 years ago
links :sortBy do
map_with_sort_by_as_decorated(represented.sort_criteria_columns) do |sort_by|
8 years ago
{
href: api_v3_paths.query_sort_by(sort_by.converted_name, sort_by.direction_name),
title: sort_by.name
}
end
end
link :schema do
href = if represented.project
api_v3_paths.query_project_schema(represented.project.identifier)
else
api_v3_paths.query_schema
end
{
href: href
}
end
link :update do
href = if represented.new_record?
api_v3_paths.create_query_form
else
api_v3_paths.query_form(represented.id)
end
{
href: href,
method: :post
}
end
link :updateImmediately do
next unless represented.new_record? && allowed_to?(:create) ||
represented.persisted? && allowed_to?(:update)
{
href: api_v3_paths.query(represented.id),
method: :patch
}
end
link :delete do
next if represented.new_record? ||
!allowed_to?(:destroy)
{
href: api_v3_paths.query(represented.id),
method: :delete
}
end
linked_property :user
linked_property :project
property :id
property :name
property :filters, exec_context: :decorator
property :is_public, as: :public
8 years ago
property :sort_by,
exec_context: :decorator,
8 years ago
embedded: true,
if: ->(*) {
embed_links
}
8 years ago
property :display_sums,
as: :sums
property :timeline_visible
property :timeline_zoom_level
property :show_hierarchies
property :starred
property :columns,
exec_context: :decorator,
embedded: true,
if: ->(*) {
embed_links
}
8 years ago
property :group_by,
exec_context: :decorator,
embedded: true,
if: ->(*) {
embed_links
},
render_nil: true
property :results,
exec_context: :decorator,
render_nil: true,
embedded: true,
if: ->(*) {
results
}
self.to_eager_load = [:query_menu_item,
:user,
project: :work_package_custom_fields]
def _type
'Query'
end
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
private
def allowed_to?(action)
@policy ||= QueryPolicy.new(current_user)
@policy.allowed?(represented, action)
end
def self_v3_path(*_args)
if represented.new_record? && represented.project
api_v3_paths.query_project_default(represented.project.id)
elsif represented.new_record?
api_v3_paths.query_default
else
super
end
end
end
end
end
end