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