From 8561d97ddc831cb54babab51e309fd12b89db3a6 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 806b77055..2b21267df 100644 --- a/app/models/object_manager/attribute.rb +++ b/app/models/object_manager/attribute.rb @@ -834,7 +834,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 03d040947..c98d7dcde 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