From 24e858c39629d46be973bb4a749c0ef60b13e34c Mon Sep 17 00:00:00 2001 From: Jan Sandbrink Date: Wed, 18 Mar 2015 10:39:24 +0100 Subject: [PATCH] include schema for budget --- lib/open_project/costs/engine.rb | 20 ++++++ .../costs/patches/project_patch.rb | 16 ++++- .../costs/patches/work_package_patch.rb | 2 +- .../patches/work_package_schema_patch.rb | 56 +++++++++++++++ .../work_package_schema_representer_spec.rb | 69 +++++++++++++++++++ .../work_packages/work_package_schema_spec.rb | 54 +++++++++++++++ 6 files changed, 213 insertions(+), 4 deletions(-) create mode 100644 lib/open_project/costs/patches/work_package_schema_patch.rb create mode 100644 spec/lib/api/v3/work_packages/work_package_schema_representer_spec.rb create mode 100644 spec/lib/api/v3/work_packages/work_package_schema_spec.rb diff --git a/lib/open_project/costs/engine.rb b/lib/open_project/costs/engine.rb index 25f621a5a1..598b9efeb4 100644 --- a/lib/open_project/costs/engine.rb +++ b/lib/open_project/costs/engine.rb @@ -102,6 +102,7 @@ module OpenProject::Costs patches [:WorkPackage, :Project, :Query, :User, :TimeEntry, :PermittedParams, :ProjectsController, :ApplicationHelper, :UsersHelper] + patch_with_namespace :API, :V3, :WorkPackages, :Schema, :WorkPackageSchema add_api_path :budget do |id| "#{root}/budgets/#{id}" @@ -201,6 +202,25 @@ module OpenProject::Costs end end + extend_api_response(:v3, :work_packages, :schema, :work_package_schema) do + schema_with_allowed_collection :budget, + name_source: :cost_object, + required: false, + values_callback: -> (*) { + represented.assignable_cost_objects + }, + value_representer: ::API::V3::Budgets::BudgetRepresenter, + link_factory: -> (budget) { + { + href: api_v3_paths.budget(budget.id), + title: budget.subject + } + }, + show_if: -> (*) { + represented.project.costs_enabled? + } + end + assets %w(costs/costs.css costs/costs.js work_packages/cost_object.html diff --git a/lib/open_project/costs/patches/project_patch.rb b/lib/open_project/costs/patches/project_patch.rb index f6df322e97..7edeeb4cb7 100644 --- a/lib/open_project/costs/patches/project_patch.rb +++ b/lib/open_project/costs/patches/project_patch.rb @@ -19,7 +19,9 @@ module OpenProject::Costs::Patches::ProjectPatch def self.included(base) # :nodoc: - # Same as typing in the class + base.extend(ClassMethods) + base.include(InstanceMethods) + base.class_eval do unloadable @@ -27,10 +29,18 @@ module OpenProject::Costs::Patches::ProjectPatch has_many :rates, :class_name => 'HourlyRate' has_many :member_groups, :class_name => 'Member', - :include => :principal, - :conditions => "#{Principal.table_name}.type='Group'" + :include => :principal, + :conditions => "#{Principal.table_name}.type='Group'" has_many :groups, :through => :member_groups, :source => :principal end + end + module ClassMethods + end + + module InstanceMethods + def costs_enabled? + module_enabled?(:costs_module) + end end end diff --git a/lib/open_project/costs/patches/work_package_patch.rb b/lib/open_project/costs/patches/work_package_patch.rb index bba69c0789..2b08d59e91 100644 --- a/lib/open_project/costs/patches/work_package_patch.rb +++ b/lib/open_project/costs/patches/work_package_patch.rb @@ -95,7 +95,7 @@ module OpenProject::Costs::Patches::WorkPackagePatch module InstanceMethods def costs_enabled? - project && project.module_enabled?(:costs_module) + project && project.costs_enabled? end def validate_cost_object diff --git a/lib/open_project/costs/patches/work_package_schema_patch.rb b/lib/open_project/costs/patches/work_package_schema_patch.rb new file mode 100644 index 0000000000..29dc1d224f --- /dev/null +++ b/lib/open_project/costs/patches/work_package_schema_patch.rb @@ -0,0 +1,56 @@ +#-- copyright +# OpenProject Backlogs Plugin +# +# Copyright (C)2013-2014 the OpenProject Foundation (OPF) +# Copyright (C)2011 Stephan Eckardt, Tim Felgentreff, Marnen Laibow-Koser, Sandro Munda +# Copyright (C)2010-2011 friflaj +# Copyright (C)2010 Maxime Guilbot, Andrew Vit, Joakim Kolsjö, ibussieres, Daniel Passos, Jason Vasquez, jpic, Emiliano Heyns +# Copyright (C)2009-2010 Mark Maglana +# Copyright (C)2009 Joe Heck, Nate Lowrie +# +# 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 Backlogs is a derivative work based on ChiliProject Backlogs. +# The copyright follows: +# Copyright (C) 2010-2011 - Emiliano Heyns, Mark Maglana, friflaj +# Copyright (C) 2011 - Jens Ulferts, Gregor Schmidt - Finn GmbH - Berlin, Germany +# +# 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_dependency 'api/v3/work_packages/schema/work_package_schema' + +module OpenProject::Costs::Patches::WorkPackageSchemaPatch + def self.included(base) + base.class_eval do + unloadable + + include InstanceMethods + extend ClassMethods + end + end + + module ClassMethods + end + + module InstanceMethods + def assignable_cost_objects + project.try(:cost_objects) + end + end +end diff --git a/spec/lib/api/v3/work_packages/work_package_schema_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_schema_representer_spec.rb new file mode 100644 index 0000000000..7fc120d4d0 --- /dev/null +++ b/spec/lib/api/v3/work_packages/work_package_schema_representer_spec.rb @@ -0,0 +1,69 @@ +#-- copyright +# OpenProject is a project management system. +# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See doc/COPYRIGHT.rdoc for more details. +#++ + +require 'spec_helper' + +describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do + let(:custom_field) { FactoryGirl.build(:custom_field) } + let(:work_package) { FactoryGirl.build(:work_package) } + let(:current_user) { + FactoryGirl.build(:user, member_in_project: work_package.project) + } + let(:schema) { + ::API::V3::WorkPackages::Schema::WorkPackageSchema.new(work_package: work_package) + } + let(:representer) { described_class.create(schema, current_user: current_user) } + subject { representer.to_json } + + describe 'budget' do + it_behaves_like 'has basic schema properties' do + let(:path) { 'budget' } + let(:type) { 'Budget' } + let(:name) { I18n.t('attributes.cost_object') } + let(:required) { false } + let(:writable) { true } + end + + it_behaves_like 'has a collection of allowed values' do + let(:json_path) { 'budget' } + let(:href_path) { 'budgets' } + let(:factory) { :cost_object } + let(:allowed_values_method) { :assignable_cost_objects } + end + + context 'costs disabled' do + before do + allow(schema.project).to receive(:costs_enabled?).and_return(false) + end + + it 'has no schema for budget' do + is_expected.not_to have_json_path('budget') + end + end + end +end diff --git a/spec/lib/api/v3/work_packages/work_package_schema_spec.rb b/spec/lib/api/v3/work_packages/work_package_schema_spec.rb new file mode 100644 index 0000000000..71206fd0bc --- /dev/null +++ b/spec/lib/api/v3/work_packages/work_package_schema_spec.rb @@ -0,0 +1,54 @@ +#-- copyright +# OpenProject is a project management system. +# Copyright (C) 2012-2015 the OpenProject Foundation (OPF) +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See doc/COPYRIGHT.rdoc for more details. +#++ + +require 'spec_helper' + +describe ::API::V3::WorkPackages::Schema::WorkPackageSchema do + let(:project) { + double('Project', cost_objects: double('CostObjects')) + } + let(:type) { FactoryGirl.build(:type) } + let(:work_package) { FactoryGirl.build(:work_package, project: project, type: type) } + + describe '#assignable_cost_objects' do + subject { described_class.new(project: project, type: type) } + + it 'returns project.cost_objects' do + expect(subject.assignable_cost_objects).to eql(project.cost_objects) + end + + context 'project is nil' do + let(:project) { nil } + subject { described_class.new(work_package: work_package) } + + it 'returns nil' do + expect(subject.assignable_cost_objects).to eql(nil) + end + end + end +end