Merge pull request #112 from finnlabs/fix/17243_value_for_spent_units_in_api_v3_wrong

17243 value for spent units in api v3 wrong
pull/6827/head
ulferts 10 years ago
commit 222c43d56b
  1. 13
      lib/api/v3/cost_types/cost_type_representer.rb
  2. 8
      lib/open_project/costs/engine.rb
  3. 103
      spec/lib/api/v3/cost_types/cost_type_representer_spec.rb

@ -31,9 +31,9 @@ module API
self.as_strategy = API::Utilities::CamelCasingStrategy.new
def initialize(model, unit_summary, options = {}, *expand)
@expand = expand
@work_package = options[:work_package]
@summary = unit_summary
@work_package = options[:work_package]
@current_user = options[:current_user]
super(model)
end
@ -43,7 +43,14 @@ module API
property :id, render_nil: true
property :name, render_nil: true
property :units,
getter: -> (*) { cost_entries.sum(&:units) },
getter: -> (*) {
cost_entries = @work_package.cost_entries
.visible(@current_user, @work_package.project)
.where(cost_type_id: represented.id)
cost_entries.sum(&:units)
},
exec_context: :decorator,
render_nil: true
property :unit,
exec_context: :decorator,

@ -156,7 +156,13 @@ module OpenProject::Costs
send(:define_method, :summarized_cost_entries) do
self.attributes_helper.summarized_cost_entries
.map { |c| ::API::V3::CostTypes::CostTypeRepresenter.new(c[0], c[1], work_package: represented) }
.map do |c|
::API::V3::CostTypes::CostTypeRepresenter
.new(c[0],
c[1],
work_package: represented,
current_user: @current_user)
end
end
send(:define_method, :attributes_helper) do

@ -20,23 +20,86 @@
require 'spec_helper'
describe ::API::V3::CostTypes::CostTypeRepresenter do
let(:project) { FactoryGirl.build(:project, id: 999) }
let(:user) { FactoryGirl.build(:user,
member_in_project: project,
created_on: 1.day.ago,
updated_on: Date.today) }
let(:work_package) { FactoryGirl.build(:work_package,
project: project) }
let(:cost_type) { FactoryGirl.build(:cost_type) }
let!(:cost_entry) { FactoryGirl.build(:cost_entry,
work_package: nil,
project: project,
units: 3,
spent_on: Date.today,
user: user,
comments: "Entry 1") }
let(:representer) { described_class.new(cost_type, work_package: work_package) }
let(:project1) { FactoryGirl.create(:project) }
let(:project2) { FactoryGirl.create(:project) }
let(:role) {
FactoryGirl.create(:role, permissions: [:view_work_package, :view_own_cost_entries])
}
let(:user1) {
FactoryGirl.create(:user,
member_in_projects: [project1, project2],
member_through_role: role,
created_on: 1.day.ago,
updated_on: Date.today)
}
let(:user2) {
FactoryGirl.create(:user,
member_in_projects: [project1, project2],
member_through_role: role,
created_on: 1.day.ago,
updated_on: Date.today)
}
let(:work_package1) { FactoryGirl.create(:work_package, project: project1) }
let(:work_package2) { FactoryGirl.create(:work_package, project: project2) }
let(:cost_type1) { FactoryGirl.create(:cost_type) }
let(:cost_type2) { FactoryGirl.create(:cost_type) }
let!(:cost_entry11) {
FactoryGirl.create(:cost_entry,
cost_type: cost_type1,
work_package: work_package1,
project: project1,
units: 3,
spent_on: Date.today,
user_id: user1.id,
comments: 'Entry 1')
}
let!(:cost_entry12) {
FactoryGirl.create(:cost_entry,
cost_type: cost_type2,
work_package: work_package1,
project: project1,
units: 3,
spent_on: Date.today,
user_id: user1.id,
comments: 'Entry 2')
}
let!(:cost_entry13) {
FactoryGirl.create(:cost_entry,
cost_type: cost_type1,
work_package: work_package1,
project: project1,
units: 3,
spent_on: Date.today,
user_id: user2.id,
comments: 'Entry 3')
}
let!(:cost_entry21) {
FactoryGirl.create(:cost_entry,
cost_type: cost_type1,
work_package: work_package2,
project: project2,
units: 3,
spent_on: Date.today,
user: user1,
comments: 'Entry 1')
}
let!(:cost_entry22) {
FactoryGirl.create(:cost_entry,
cost_type: cost_type2,
work_package: work_package2,
project: project2,
units: 3,
spent_on: Date.today,
user: user1,
comments: 'Entry 2')
}
let(:representer) do
described_class.new(cost_type1,
{ unit: 'tonne', unit_plural: 'tonnes' },
work_package: work_package1,
current_user: user1)
end
context 'generation' do
subject(:generated) { representer.to_json }
@ -52,5 +115,11 @@ describe ::API::V3::CostTypes::CostTypeRepresenter do
it { should have_json_path('unit') }
it { should have_json_path('unitPlural') }
end
describe 'units' do
it 'shows only cost entries of type cost_type1 for user1 in project project1' do
should be_json_eql(cost_entry11.units.to_json).at_path('units')
end
end
end
end

Loading…
Cancel
Save