OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Replace find_json! in API with separate representers
Oliver Günther5c9221258ab4
diff --git a/modules/bcf/app/controllers/bcf/api/v2_1/viewpoints/api.rb b/modules/bcf/app/controllers/bcf/api/v2_1/viewpoints/api.rbindex d08a67ec391..9f197811088 100644--- a/modules/bcf/app/controllers/bcf/api/v2_1/viewpoints/api.rb+++ b/modules/bcf/app/controllers/bcf/api/v2_1/viewpoints/api.rb@@ -39,45 +39,15 @@ module Bcf::API::V2_1endroute_param :viewpoint_uuid, regexp: /\A[a-f0-9\-]+\z/ do+ %i[/ selection coloring visibility].each do |key|+ namespace = key == :/ ? :Full : key.to_s.camelize- helpers do-- ##- # Extract raw json from database- # in an optional subpath- def find_json!(uuid, slice: nil)- query = @issue.viewpoints- .where(uuid: uuid)-- query =- if slice- query.select("json_viewpoint #> '{#{slice.join(',')}}' as json_viewpoint")- else- query.select(:json_viewpoint)- end-- row = query.first- raise ActiveRecord::RecordNotFound unless row-- # If we access row.json, Rails is going to cast the json to ruby hash for us- row.raw_json_viewpoint- end- end-- get do- find_json!(params[:viewpoint_uuid])- end-- get :selection do- find_json!(params[:viewpoint_uuid], slice: %w[components selection])- end-- get :coloring do- find_json!(params[:viewpoint_uuid], slice: %w[components coloring])- end-- get :visibility do- find_json!(params[:viewpoint_uuid], slice: %w[components visibility])+ get key, &::Bcf::API::V2_1::Endpoints::Show+ .new(model: Bcf::Issue,+ api_name: 'Viewpoints',+ render_representer: "::Bcf::API::V2_1::Viewpoints::#{namespace}Representer".constantize,+ instance_generator: ->(*) { @issue.viewpoints.where(uuid: params[:viewpoint_uuid]) })+ .mountendget :bitmaps dodiff --git a/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/base_representer.rb b/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/base_representer.rbnew file mode 100644index 00000000000..4bc44ce400a--- /dev/null+++ b/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/base_representer.rb@@ -0,0 +1,53 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++module Bcf::API::V2_1+ class Viewpoints::BaseRepresenter < BaseRepresenter+ attr_reader :base_scope++ def initialize(base_scope)+ @base_scope = base_scope+ end++ def to_json+ row = scope.first+ raise ::ActiveRecord::RecordNotFound unless row++ # If we access row.json, Rails is going to cast the json to ruby hash for us+ row.raw_json_viewpoint+ end++ protected++ def scope+ raise NotImplementedError+ end+ end+enddiff --git a/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/coloring_representer.rb b/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/coloring_representer.rbnew file mode 100644index 00000000000..cf963b5beda--- /dev/null+++ b/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/coloring_representer.rb@@ -0,0 +1,42 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++require_relative 'base_representer'++module Bcf::API::V2_1::Viewpoints+ class ColoringRepresenter < BaseRepresenter+ protected++ def scope+ base_scope+ .select "jsonb_build_object('coloring', json_viewpoint #> '{components, coloring}') as json_viewpoint"+ end+ end+enddiff --git a/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/full_representer.rb b/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/full_representer.rbnew file mode 100644index 00000000000..c7447343ec8--- /dev/null+++ b/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/full_representer.rb@@ -0,0 +1,40 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#+++require_relative 'base_representer'++module Bcf::API::V2_1::Viewpoints+ class FullRepresenter < BaseRepresenter+ protected++ def scope+ base_scope.select(:json_viewpoint)+ end+ end+enddiff --git a/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/selection_representer.rb b/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/selection_representer.rbnew file mode 100644index 00000000000..8dae24002ea--- /dev/null+++ b/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/selection_representer.rb@@ -0,0 +1,42 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++require_relative 'base_representer'++module Bcf::API::V2_1::Viewpoints+ class SelectionRepresenter < BaseRepresenter+ protected++ def scope+ base_scope+ .select "jsonb_build_object('selection', json_viewpoint #> '{components, selection}') as json_viewpoint"+ end+ end+enddiff --git a/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/visibility_representer.rb b/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/visibility_representer.rbnew file mode 100644index 00000000000..5daa02e5664--- /dev/null+++ b/modules/bcf/app/representers/bcf/api/v2_1/viewpoints/visibility_representer.rb@@ -0,0 +1,42 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++require_relative 'base_representer'++module Bcf::API::V2_1::Viewpoints+ class VisibilityRepresenter < BaseRepresenter+ protected++ def scope+ base_scope+ .select "jsonb_build_object('visibility', json_viewpoint #> '{components, visibility}') as json_viewpoint"+ end+ end+enddiff --git a/modules/bcf/spec/requests/api/bcf/v2_1/viewpoints_api_spec.rb b/modules/bcf/spec/requests/api/bcf/v2_1/viewpoints_api_spec.rbindex 40c45a50ac8..d12087c0570 100644--- a/modules/bcf/spec/requests/api/bcf/v2_1/viewpoints_api_spec.rb+++ b/modules/bcf/spec/requests/api/bcf/v2_1/viewpoints_api_spec.rb@@ -54,7 +54,7 @@ describe 'BCF 2.1 viewpoints resource', type: :request, content_type: :json, witshared_let(:bcf_issue) { FactoryBot.create(:bcf_issue_with_viewpoint, work_package: work_package) }let(:viewpoint) { bcf_issue.viewpoints.first }- let(:viewpoint_json) { ::OpenProject::Bcf::BcfJson::ViewpointReader.new(viewpoint).result }+ let(:viewpoint_json) { viewpoint.json_viewpoint }subject(:response) { last_response }describe 'GET /api/bcf/2.1/projects/:project_id/topics/:topic/viewpoints' do