From e06d10978e9e11fe527d6fc70a4c7027d3bce8f1 Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Tue, 17 May 2022 19:19:09 +0200 Subject: [PATCH] Fixes #4086 - Custom sort on select returns "undefined method `merge' " for older objects. --- ...065751_issue4086_fix_historical_options.rb | 15 +++++++++ .../issue_4086_fix_historical_options_spec.rb | 33 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 db/migrate/20220517065751_issue4086_fix_historical_options.rb create mode 100644 spec/db/migrate/issue_4086_fix_historical_options_spec.rb diff --git a/db/migrate/20220517065751_issue4086_fix_historical_options.rb b/db/migrate/20220517065751_issue4086_fix_historical_options.rb new file mode 100644 index 000000000..65a3dac13 --- /dev/null +++ b/db/migrate/20220517065751_issue4086_fix_historical_options.rb @@ -0,0 +1,15 @@ +# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/ + +class Issue4086FixHistoricalOptions < ActiveRecord::Migration[5.0] + def change + # return if it's a new setup + return if !Setting.exists?(name: 'system_init_done') + + ObjectManager::Attribute.find_each do |attribute| + next if !%r{^(multi|tree_)?select$}.match?(attribute.data_type) + + attribute.data_option[:historical_options] = ObjectManager::Attribute.data_options_hash(attribute.data_option[:historical_options] || {}) + attribute.save + end + end +end diff --git a/spec/db/migrate/issue_4086_fix_historical_options_spec.rb b/spec/db/migrate/issue_4086_fix_historical_options_spec.rb new file mode 100644 index 000000000..03f5bd459 --- /dev/null +++ b/spec/db/migrate/issue_4086_fix_historical_options_spec.rb @@ -0,0 +1,33 @@ +# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/ + +require 'rails_helper' + +RSpec.describe Issue4086FixHistoricalOptions, type: :db_migration do + let(:expected) do + { + 'Sonstiges' => 'Sonstiges', + 'Hardware' => 'Hardware', + 'Software' => 'Software', + } + end + + let(:attribute) do + attribute = create(:object_manager_attribute_select) + attribute.data_option[:historical_options] = [ + { 'name' => 'Sonstiges', 'value' => 'Sonstiges' }, + { 'name' => 'Hardware', 'value' => 'Hardware' }, + { 'name' => 'Software', 'value' => 'Software' } + ] + attribute.save + attribute + end + + before do + attribute + end + + it 'does fix the broken historical_options' do + migrate + expect(attribute.reload.data_option[:historical_options]).to eq(expected) + end +end