Budget services

pull/9686/head
Oliver Günther 3 years ago
parent 0fce502a69
commit 197afad075
No known key found for this signature in database
GPG Key ID: A3A8BDAD7C0C552C
  1. 66
      modules/budgets/app/contracts/budgets/base_contract.rb
  2. 34
      modules/budgets/app/contracts/budgets/create_contract.rb
  3. 34
      modules/budgets/app/contracts/budgets/update_contract.rb
  4. 37
      modules/budgets/app/controllers/budgets_controller.rb
  5. 33
      modules/budgets/app/services/budgets/create_service.rb
  6. 65
      modules/budgets/app/services/budgets/set_attributes_service.rb
  7. 35
      modules/budgets/app/services/budgets/update_service.rb
  8. 36
      modules/budgets/spec/services/budgets/create_service_spec.rb
  9. 36
      modules/budgets/spec/services/budgets/update_service_spec.rb

@ -0,0 +1,66 @@
#-- encoding: UTF-8
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2021 the OpenProject GmbH
#
# 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 docs/COPYRIGHT.rdoc for more details.
#++
module Budgets
class BaseContract < ::ModelContract
include Attachments::ValidateReplacements
def self.model
Budget
end
attribute :subject
attribute :description
attribute :fixed_date
attribute :project
attribute :author
attribute :new_material_budget_item_attributes,
readable: false
attribute :new_labor_budget_item_attributes,
readable: false
attribute :existing_material_budget_item_attributes,
readable: false
attribute :existing_labor_budget_item_attributes,
readable: false
validate :validate_manage_allowed
private
def validate_manage_allowed
unless user.allowed_to?(:edit_budgets, model.project)
errors.add :base, :error_unauthorized
end
end
end
end

@ -0,0 +1,34 @@
#-- encoding: UTF-8
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2021 the OpenProject GmbH
#
# 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 docs/COPYRIGHT.rdoc for more details.
#++
module Budgets
class CreateContract < BaseContract
end
end

@ -0,0 +1,34 @@
#-- encoding: UTF-8
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2021 the OpenProject GmbH
#
# 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 docs/COPYRIGHT.rdoc for more details.
#++
module Budgets
class UpdateContract < BaseContract
end
end

@ -27,6 +27,8 @@
#++
class BudgetsController < ApplicationController
include AttachableServiceCall
before_action :find_budget, only: %i[show edit update copy]
before_action :find_budgets, only: :destroy
before_action :find_project, only: %i[new create update_material_budget_item update_labor_budget_item]
@ -85,7 +87,8 @@ class BudgetsController < ApplicationController
def copy
source = Budget.find(params[:id].to_i)
@budget = if source
@budget =
if source
Budget.new_copy(source)
else
Budget.new
@ -97,20 +100,11 @@ class BudgetsController < ApplicationController
end
def create
@budget = Budget.new
@budget.project_id = @project.id
# fixed_date must be set before material_budget_items and labor_budget_items
@budget.fixed_date = if params[:budget] && params[:budget][:fixed_date]
params[:budget].delete(:fixed_date)
else
Date.today
end
call = attachable_create_call ::Budgets::CreateService,
args: permitted_params.budget.merge(project: @project)
@budget = call.result
@budget.attributes = permitted_params.budget
@budget.attach_files(permitted_params.attachments.to_h)
if @budget.save
if call.success?
flash[:notice] = t(:notice_successful_create)
redirect_to(params[:continue] ? { action: 'new' } : { action: 'show', id: @budget })
else
@ -123,20 +117,15 @@ class BudgetsController < ApplicationController
end
def update
@budget.attributes = permitted_params.budget if params[:budget]
if params[:budget][:existing_material_budget_item_attributes].nil?
@budget.existing_material_budget_item_attributes = ({})
end
if params[:budget][:existing_labor_budget_item_attributes].nil?
@budget.existing_labor_budget_item_attributes = ({})
end
@budget.attach_files(permitted_params.attachments.to_h)
call = attachable_update_call ::Budgets::UpdateService,
model: @budget,
args: permitted_params.budget
if @budget.save
if call.success?
flash[:notice] = t(:notice_successful_update)
redirect_to(params[:back_to] || { action: 'show', id: @budget })
else
@budget = call.result
render action: 'edit'
end
rescue ActiveRecord::StaleObjectError

@ -0,0 +1,33 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2021 the OpenProject GmbH
#
# 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 docs/COPYRIGHT.rdoc for more details.
#++
module Budgets
class CreateService < ::BaseServices::Create
include Attachments::ReplaceAttachments
end
end

@ -0,0 +1,65 @@
#-- encoding: UTF-8
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2021 the OpenProject GmbH
#
# 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 docs/COPYRIGHT.rdoc for more details.
#++
module Budgets
class SetAttributesService < ::BaseServices::SetAttributes
include Attachments::SetReplacements
private
def set_attributes(params)
set_fixed_date(params)
unset_items(params)
super
end
def set_default_attributes(_params)
model.change_by_system do
model.author = user
end
end
# fixed_date must be set before material_budget_items and labor_budget_items
def set_fixed_date(params)
model.fixed_date = params.delete(:fixed_date) || Date.today
end
def unset_items(params)
if params[:existing_material_budget_item_attributes].nil?
model.existing_material_budget_item_attributes = ({})
end
if params[:existing_labor_budget_item_attributes].nil?
model.existing_labor_budget_item_attributes = ({})
end
end
end
end

@ -0,0 +1,35 @@
#-- encoding: UTF-8
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2021 the OpenProject GmbH
#
# 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 docs/COPYRIGHT.rdoc for more details.
#++
module Budgets
class UpdateService < ::BaseServices::Update
include Attachments::ReplaceAttachments
end
end

@ -0,0 +1,36 @@
#-- encoding: UTF-8
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2021 the OpenProject GmbH
#
# 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 docs/COPYRIGHT.rdoc for more details.
#++
require 'spec_helper'
require 'services/base_services/behaves_like_create_service'
describe Budgets::CreateService, type: :model do
it_behaves_like 'BaseServices create service'
end

@ -0,0 +1,36 @@
#-- encoding: UTF-8
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2021 the OpenProject GmbH
#
# 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 docs/COPYRIGHT.rdoc for more details.
#++
require 'spec_helper'
require 'services/base_services/behaves_like_update_service'
describe Budgets::UpdateService, type: :model do
it_behaves_like 'BaseServices update service'
end
Loading…
Cancel
Save