include schema for budget

pull/6827/head
Jan Sandbrink 10 years ago
parent c93444aacb
commit 24e858c396
  1. 20
      lib/open_project/costs/engine.rb
  2. 12
      lib/open_project/costs/patches/project_patch.rb
  3. 2
      lib/open_project/costs/patches/work_package_patch.rb
  4. 56
      lib/open_project/costs/patches/work_package_schema_patch.rb
  5. 69
      spec/lib/api/v3/work_packages/work_package_schema_representer_spec.rb
  6. 54
      spec/lib/api/v3/work_packages/work_package_schema_spec.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

@ -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
@ -31,6 +33,14 @@ module OpenProject::Costs::Patches::ProjectPatch
: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

@ -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

@ -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

@ -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

@ -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
Loading…
Cancel
Save