extract contract related methods of service into concern

pull/4262/head
Jens Ulferts 9 years ago committed by Oliver Günther
parent 2a6885043c
commit 2f14b793b5
  1. 52
      app/services/concerns/contracted.rb
  2. 20
      app/services/update_work_package_service.rb

@ -0,0 +1,52 @@
#-- encoding: UTF-8
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2015 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.
#
# 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 doc/COPYRIGHT.rdoc for more details.
#++
module Concerns::Contracted
extend ActiveSupport::Concern
included do
class << self
attr_accessor :contract
end
private
attr_accessor :contract
def validate_and_save(object)
if !contract.validate
return false, contract.errors
elsif !object.save
return false, object.errors
else
return true, object.errors
end
end
end
end

@ -28,11 +28,9 @@
#++
class UpdateWorkPackageService
attr_accessor :user, :work_package
include Concerns::Contracted
class << self
attr_accessor :contract
end
attr_accessor :user, :work_package
self.contract = WorkPackages::UpdateContract
@ -53,13 +51,11 @@ class UpdateWorkPackageService
private
attr_accessor :contract
def update(attributes)
set_attributes(attributes)
changed_attributes = work_package.changes.dup
save_result, save_errors = validate_and_save
save_result, save_errors = validate_and_save(work_package)
if save_result
cleanup_result, cleanup_errors = cleanup(changed_attributes)
@ -74,16 +70,6 @@ class UpdateWorkPackageService
work_package.attributes = attributes
end
def validate_and_save
if !contract.validate
return false, contract.errors
elsif !work_package.save
return false, work_package.errors
else
return true, work_package.errors
end
end
def cleanup(attributes)
result = true
errors = work_package.errors

Loading…
Cancel
Save