Extract shareable methods into separate module

pull/2990/head
Alexander Bach 10 years ago
parent 0e66c303ac
commit d98547738c
  1. 50
      lib/api/v3/work_packages/work_packages_api.rb
  2. 87
      lib/api/v3/work_packages/work_packages_shared_helpers.rb

@ -27,9 +27,7 @@
#++
require 'api/v3/activities/activity_representer'
require 'api/v3/work_packages/work_package_contract'
require 'api/v3/work_packages/work_package_representer'
require 'api/v3/work_packages/form/work_package_payload_representer'
module API
module V3
@ -40,6 +38,7 @@ module API
requires :id, desc: 'Work package id'
end
route_param :id do
helpers WorkPackagesSharedHelpers
helpers do
attr_reader :work_package
@ -47,53 +46,6 @@ module API
WorkPackageRepresenter.create(@work_package,
current_user: current_user)
end
def write_work_package_attributes
if request_body
begin
# we need to merge the JSON two times:
# In Pass 1 the representer only has custom fields for the current WP type
# After Pass 1 the correct type information is merged into the WP
# In Pass 2 the representer is created with the new type info and will be able
# to also parse custom fields successfully
merge_json_into_work_package!(request_body.to_json)
merge_json_into_work_package!(request_body.to_json)
rescue ::API::Errors::Form::InvalidResourceLink => e
fail ::API::Errors::Validation.new(e.message)
end
end
end
# merges the given JSON representation into @work_package
def merge_json_into_work_package!(json)
payload = Form::WorkPackagePayloadRepresenter.create(
@work_package,
enforce_lock_version_validation: true)
payload.from_json(json)
end
def request_body
env['api.request.body']
end
def write_request_valid?
contract = WorkPackageContract.new(@work_package, current_user)
contract_valid = contract.validate
represented_valid = @work_package.valid?
return true if contract_valid && represented_valid
# We need to merge the contract errors with the model errors in
# order to have them available at one place.
contract.errors.keys.each do |key|
contract.errors[key].each do |message|
@work_package.errors.add(key, message)
end
end
false
end
end
before do

@ -0,0 +1,87 @@
#-- 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.
#++
require 'api/v3/work_packages/work_package_contract'
require 'api/v3/work_packages/form/work_package_payload_representer'
module API
module V3
module WorkPackages
module WorkPackagesSharedHelpers
extend Grape::API::Helpers
def request_body
env['api.request.body']
end
# merges the given JSON representation into @work_package
def merge_json_into_work_package!(json)
payload = Form::WorkPackagePayloadRepresenter.create(
@work_package,
enforce_lock_version_validation: true)
payload.from_json(json)
end
def write_work_package_attributes
if request_body
begin
# we need to merge the JSON two times:
# In Pass 1 the representer only has custom fields for the current WP type
# After Pass 1 the correct type information is merged into the WP
# In Pass 2 the representer is created with the new type info and will be able
# to also parse custom fields successfully
merge_json_into_work_package!(request_body.to_json)
merge_json_into_work_package!(request_body.to_json)
rescue ::API::Errors::Form::InvalidResourceLink => e
fail ::API::Errors::Validation.new(e.message)
end
end
end
def write_request_valid?
contract = WorkPackageContract.new(@work_package, current_user)
contract_valid = contract.validate
represented_valid = @work_package.valid?
return true if contract_valid && represented_valid
# We need to merge the contract errors with the model errors in
# order to have them available at one place.
contract.errors.keys.each do |key|
contract.errors[key].each do |message|
@work_package.errors.add(key, message)
end
end
false
end
end
end
end
end
Loading…
Cancel
Save