Move Costs attribute definition to client

pull/6827/head
Hagen Schink 10 years ago
parent 0bcedb8c0b
commit 7e13826de4
  1. 32
      app/assets/javascripts/angular/openproject-costs-app.js
  2. 8
      lib/open_project/costs/engine.rb
  3. 47
      lib/open_project/costs/hooks/work_packages_overview_attributes.rb
  4. 75
      spec/controllers/work_packages_controller_spec.rb

@ -30,6 +30,38 @@
var openprojectCostsApp = angular.module('openproject');
openprojectCostsApp.run(['HookService', function(HookService) {
HookService.register('workPackagePluginAttributes', function(params) {
var attributes = params.attributes;
var workPackage = params.workPackage;
var spentTimeIndex = attributes.indexOf('spentTime');
var costsActivted = false;
var costsAttributes = {
props: {
overallCosts: null,
spentHours: 'spentHoursLinked'
},
embedded: {
costObject: null,
summarizedCostEntries: 'spentUnits'
}
};
angular.forEach(costsAttributes, function(costAttributes, property) {
angular.forEach(costAttributes, function(id, costAttribute) {
if (workPackage[property][costAttribute]) {
attributes.push(id || costAttribute);
// if at least a single Costs attribute is available in the API
// response then the Costs module must be active
costsActivted = true;
}
});
});
if (spentTimeIndex >= 0 && costsActivted) {
attributes.splice(spentTimeIndex, 1);
}
});
HookService.register('workPackageOverviewAttributes', function(params) {
var directive;

@ -134,7 +134,7 @@ module OpenProject::Costs
property :summarized_cost_entries,
embedded: true,
exec_context: :decorator,
if: -> (*) { costs_enabled }
if: -> (*) { costs_enabled && current_user_allowed_to_view_summarized_cost_entries }
send(:define_method, :cost_object) do
::API::V3::CostObjects::CostObjectModel.new(represented.model.cost_object)
@ -149,6 +149,11 @@ module OpenProject::Costs
current_user_allowed_to(:view_own_time_entries, represented.model)
end
send(:define_method, :current_user_allowed_to_view_summarized_cost_entries) do
current_user_allowed_to(:view_cost_entries, represented.model) ||
current_user_allowed_to(:view_own_cost_entries, represented.model)
end
send(:define_method, :overall_costs) do
number_to_currency(self.attributes_helper.overall_costs)
end
@ -185,7 +190,6 @@ module OpenProject::Costs
require 'open_project/costs/hooks/project_hook'
require 'open_project/costs/hooks/work_package_action_menu'
require 'open_project/costs/hooks/work_packages_show_attributes'
require 'open_project/costs/hooks/work_packages_overview_attributes'
end
initializer 'costs.register_observers' do |app|

@ -1,47 +0,0 @@
#-- copyright
# OpenProject Costs Plugin
#
# Copyright (C) 2009 - 2014 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.
#
# 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.
#++
module OpenProject::Costs::Hooks
class WorkPackagesOverviewHook < Redmine::Hook::ViewListener
def work_packages_overview_attributes(context = {})
project = context[:project]
attributes = context[:attributes]
return unless project && project.module_enabled?(:costs_module)
attributes.reject!{ |attribute| attribute == :spentTime }
attributes << :costObject
attributes << :spentHoursLinked if user_allowed_to?(project, :view_time_entries, :view_own_time_entries)
attributes << :overallCosts
attributes << :spentUnits if user_allowed_to?(project, :view_cost_entries, :view_own_cost_entries)
attributes
end
private
def user_allowed_to?(project, *privileges)
privileges.inject(false) do |result, privilege|
result || User.current.allowed_to?(privilege, project)
end
end
end
end

@ -1,75 +0,0 @@
#-- copyright
# OpenProject Costs Plugin
#
# Copyright (C) 2009 - 2014 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.
#
# 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.
#++
require 'spec_helper'
describe WorkPackagesController, type: :controller do
let(:project) { FactoryGirl.create(:project) }
let(:current_user) { FactoryGirl.create(:user) }
let(:query) { FactoryGirl.build_stubbed(:query).tap(&:add_default_filter) }
let(:work_packages) { double("work packages").as_null_object }
before do
allow(User).to receive(:current).and_return current_user
allow(User.current).to receive(:allowed_to?).and_return(true)
end
describe 'settings passed to front-end client' do
describe 'visible attributes' do
let(:call_action) { get('index', project_id: project.id) }
let(:costs_attributes) { [:costObject, :spentHoursLinked, :overallCosts, :spentUnits] }
subject { assigns(:enabled_default_work_package_properties) }
context 'costs module disabled' do
before do
allow_any_instance_of(Project).to receive(:module_enabled?).and_return(true)
allow_any_instance_of(Project).to receive(:module_enabled?).with(:costs_module).and_return(false)
call_action
end
it { expect(subject).not_to include(*costs_attributes) }
it { expect(subject).to include(:spentTime) }
end
context 'all permissions granted' do
before { call_action }
it { expect(subject).to include(*costs_attributes) }
it { expect(subject).not_to include(:spentTime) }
end
context 'costs permissions revoked' do
before do
allow(User.current).to receive(:allowed_to?).with(:view_time_entries, anything).and_return(false)
allow(User.current).to receive(:allowed_to?).with(:view_own_time_entries, anything).and_return(false)
allow(User.current).to receive(:allowed_to?).with(:view_cost_entries, anything).and_return(false)
allow(User.current).to receive(:allowed_to?).with(:view_own_cost_entries, anything).and_return(false)
call_action
end
it { expect(subject).not_to include(:spentHoursLinked, :spentUnits) }
end
end
end
end
Loading…
Cancel
Save