From cb48cd2dcd0410240aca43f0b439853aa48c1b8d Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 19 Oct 2021 10:38:20 +0200 Subject: [PATCH] Fixes #3811 - Able to create custom fields for existing relation (e. g. ticket.state) - will lead to non bootable Zammad. --- app/models/object_manager/attribute.rb | 2 +- spec/models/object_manager/attribute_spec.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/models/object_manager/attribute.rb b/app/models/object_manager/attribute.rb index 404b5b3d1..d3071e5d0 100644 --- a/app/models/object_manager/attribute.rb +++ b/app/models/object_manager/attribute.rb @@ -820,7 +820,7 @@ is certain attribute used by triggers, overviews or schedulers end record = object_lookup.name.constantize.new - if record.respond_to?(name.to_sym) && record.attributes.key?(name) && new_record? + if new_record? && (record.respond_to?(name.to_sym) || record.attributes.key?(name)) errors.add(:name, "#{name} already exists!") end diff --git a/spec/models/object_manager/attribute_spec.rb b/spec/models/object_manager/attribute_spec.rb index 4e71f5b58..e283a8183 100644 --- a/spec/models/object_manager/attribute_spec.rb +++ b/spec/models/object_manager/attribute_spec.rb @@ -76,6 +76,22 @@ RSpec.describe ObjectManager::Attribute, type: :model do end end + %w[title tags].each do |not_editable_attribute| + it "rejects '#{not_editable_attribute}' which is used" do + expect do + described_class.add attributes_for :object_manager_attribute_text, name: not_editable_attribute + end.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Name Attribute not editable!') + end + end + + %w[priority state note number].each do |existing_attribute| + it "rejects '#{existing_attribute}' which is used" do + expect do + described_class.add attributes_for :object_manager_attribute_text, name: existing_attribute + end.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Name #{existing_attribute} already exists!") + end + end + it 'rejects duplicate attribute name of conflicting types' do attribute = attributes_for :object_manager_attribute_text described_class.add attribute