adapt to core changes w.r.t. APIv3 registration

- register attributes in redesigned way
- patch the right methods/classes for WP schema
pull/6827/head
Jan Sandbrink 9 years ago
parent eb49a1b0e7
commit 3b7d095a3d
  1. 7
      lib/open_project/costs/engine.rb
  2. 14
      lib/open_project/costs/patches/specific_work_package_schema_patch.rb
  3. 19
      spec/lib/api/v3/work_packages/specific_work_package_schema_spec.rb
  4. 19
      spec/lib/api/v3/work_packages/work_package_schema_representer_spec.rb

@ -105,9 +105,9 @@ module OpenProject::Costs
patches [:WorkPackage, :Project, :Query, :User, :TimeEntry, :PermittedParams,
:ProjectsController, :ApplicationHelper, :UsersHelper, :WorkPackagesHelper]
patch_with_namespace :API, :V3, :WorkPackages, :Schema, :WorkPackageSchema
patch_with_namespace :API, :V3, :WorkPackages, :Schema, :SpecificWorkPackageSchema
allow_attribute_update :work_package, [:create, :update], :cost_object_id
add_api_attribute on: :work_package, ar_name: :cost_object_id, api_name: :cost_object
add_api_path :cost_entry do |id|
"#{root}/cost_entries/#{id}"
@ -254,9 +254,6 @@ module OpenProject::Costs
schema_with_allowed_collection :cost_object,
type: 'Budget',
required: false,
values_callback: -> (*) {
represented.assignable_cost_objects
},
value_representer: ::API::V3::Budgets::BudgetRepresenter,
link_factory: -> (budget) {
{

@ -33,12 +33,12 @@
# See doc/COPYRIGHT.rdoc for more details.
#++
require_dependency 'api/v3/work_packages/schema/work_package_schema'
require_dependency 'api/v3/work_packages/schema/specific_work_package_schema'
module OpenProject::Costs::Patches::WorkPackageSchemaPatch
module OpenProject::Costs::Patches::SpecificWorkPackageSchemaPatch
def self.included(base)
base.class_eval do
include InstanceMethods
prepend InstanceMethods
extend ClassMethods
end
end
@ -47,8 +47,12 @@ module OpenProject::Costs::Patches::WorkPackageSchemaPatch
end
module InstanceMethods
def assignable_cost_objects
project.try(:cost_objects)
def assignable_values(property, _context)
if property == :cost_object
return project.try(:cost_objects)
end
super
end
end
end

@ -28,27 +28,22 @@
require 'spec_helper'
describe ::API::V3::WorkPackages::Schema::WorkPackageSchema do
describe ::API::V3::WorkPackages::Schema::SpecificWorkPackageSchema do
let(:project) {
double('Project', cost_objects: double('CostObjects'))
FactoryGirl.build(:project)
}
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) }
subject { described_class.new(work_package: work_package) }
it 'returns project.cost_objects' do
expect(subject.assignable_cost_objects).to eql(project.cost_objects)
before do
allow(project).to receive(:cost_objects).and_return(double('CostObjects'))
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
it 'returns project.cost_objects' do
expect(subject.assignable_values(:cost_object, nil)).to eql(project.cost_objects)
end
end
end

@ -35,7 +35,7 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
FactoryGirl.build(:user, member_in_project: work_package.project)
}
let(:schema) {
::API::V3::WorkPackages::Schema::WorkPackageSchema.new(work_package: work_package)
::API::V3::WorkPackages::Schema::SpecificWorkPackageSchema.new(work_package: work_package)
}
let(:embedded) { false }
let(:representer) {
@ -48,8 +48,14 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
shared_examples_for 'has a collection of allowed values' do
let(:embedded) { true }
before do
allow(schema).to receive(:assignable_values).and_return(nil)
end
context 'when no values are allowed' do
before do allow(schema).to receive(allowed_values_method).and_return([]) end
before do
allow(schema).to receive(:assignable_values).with(factory, anything).and_return([])
end
it_behaves_like 'links to and embeds allowed values directly' do
let(:path) { json_path }
@ -60,7 +66,9 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
context 'when values are allowed' do
let(:values) { FactoryGirl.build_stubbed_list(factory, 3) }
before do allow(schema).to receive(allowed_values_method).and_return(values) end
before do
allow(schema).to receive(:assignable_values).with(factory, anything).and_return(values)
end
it_behaves_like 'links to and embeds allowed values directly' do
let(:path) { json_path }
@ -69,7 +77,9 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
end
context 'when not embedded' do
let(:embedded) { false }
before do
allow(schema).to receive(:assignable_values).with(factory, anything).and_return(nil)
end
it_behaves_like 'does not link to allowed values' do
let(:path) { json_path }
@ -231,7 +241,6 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
let(:json_path) { 'costObject' }
let(:href_path) { 'budgets' }
let(:factory) { :cost_object }
let(:allowed_values_method) { :assignable_cost_objects }
end
context 'costs disabled' do

Loading…
Cancel
Save