Skip to content

Commit

Permalink
ppease the rubocop gods
Browse files Browse the repository at this point in the history
  • Loading branch information
mereghost committed Dec 20, 2024
1 parent 88c037c commit 8636fa3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/models/types/pattern_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

module Types
class PatternMapper
TOKEN_REGEX = /{{[0-9A-z_]+}}/
TOKEN_REGEX = /{{[0-9A-Za-z_]+}}/

MAPPING = {
type: ->(wp) { wp.type.name },
Expand Down Expand Up @@ -81,7 +81,7 @@ def stringify(value)
when Date, Time, DateTime
value.strftime("%Y-%m-%d")
when NilClass
"PITY DA FOOL!"
"N/A"
else
value.to_s
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/types/patterns/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def self.build(pattern)
new(pattern, pattern.tr("{}", "").to_sym)
end

def custom_field? = key.include?("custom_field")
def custom_field? = key.to_s.include?("custom_field")

def custom_field_id
return nil unless custom_field?
Expand Down
12 changes: 5 additions & 7 deletions app/services/work_packages/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class WorkPackages::CreateService < BaseServices::BaseCallable
attr_reader :user, :contract_class

def initialize(user:, contract_class: WorkPackages::CreateContract)
super()
@user = user
@contract_class = contract_class
end
Expand All @@ -54,10 +55,8 @@ def create(attributes, work_package)
if result.success
work_package.attachments = work_package.attachments_replacements if work_package.attachments_replacements
work_package.save

set_templated_subject(work_package)
work_package.save
else
false
end

if result.success?
Expand All @@ -68,18 +67,17 @@ def create(attributes, work_package)
end

set_user_as_watcher(work_package)
else
result.success = false
end

result
end

def set_templated_subject(work_package)
return unless work_package.type&.replacement_patterns_defined?
return unless work_package.type.enabled_patterns[:subject]
return true unless work_package.type&.replacement_patterns_defined?
return true unless work_package.type.enabled_patterns[:subject]

work_package.subject = work_package.type.enabled_patterns[:subject].resolve(work_package)
work_package.save
end

def set_attributes(attributes, work_package)
Expand Down
28 changes: 26 additions & 2 deletions spec/models/types/pattern_mapper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 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 COPYRIGHT and LICENSE files for more details.
#++

require "spec_helper"

RSpec.describe Types::PatternMapper do
let(:type) { build(:type, patterns: { subject: subject_pattern }) }
let(:subject_pattern) { "ID Please: {{id}}" }
let(:work_package) { create(:work_package) }

Expand All @@ -21,7 +45,7 @@

it "resolves the pattern" do
expect(subject.resolve(work_package))
.to eq("#{work_package.id} | #{work_package.done_ratio} | #{work_package.created_at.to_date.iso8601}")
.to eq("#{work_package.id} | N/A | #{work_package.created_at.to_date.iso8601}")
end
end

Expand Down

0 comments on commit 8636fa3

Please sign in to comment.