diff --git a/app/models/taskbar.rb b/app/models/taskbar.rb index 9c1be26c7..cfe9f486e 100644 --- a/app/models/taskbar.rb +++ b/app/models/taskbar.rb @@ -47,6 +47,7 @@ class Taskbar < ApplicationModel def set_user return true if local_update + return true if !UserInfo.current_user_id self.user_id = UserInfo.current_user_id end diff --git a/db/migrate/20170910000001_fixed_store_upgrade_45.rb b/db/migrate/20170910000001_fixed_store_upgrade_45.rb new file mode 100644 index 000000000..8df1273e1 --- /dev/null +++ b/db/migrate/20170910000001_fixed_store_upgrade_45.rb @@ -0,0 +1,17 @@ +class FixedStoreUpgrade45 < ActiveRecord::Migration[5.0] + def up + + # return if it's a new setup + return if !Setting.find_by(name: 'system_init_done') + Cache.clear + [Macro, Taskbar, Calendar, Trigger, Channel, Job, PostmasterFilter, Report::Profile, Setting, Sla, Template].each do |class_name| + class_name.all.each do |record| + begin + record.save! + rescue => e + Rails.logger.error "Unable to save/update #{class_name}.find(#{record.id}): #{e.message}" + end + end + end + end +end diff --git a/lib/core_ext/active_record/store/indifferent_coder.rb b/lib/core_ext/active_record/store/indifferent_coder.rb new file mode 100644 index 000000000..3e5ce3dd4 --- /dev/null +++ b/lib/core_ext/active_record/store/indifferent_coder.rb @@ -0,0 +1,24 @@ +# Rails 5.0 has changed to only store and read ActiveSupport::HashWithIndifferentAccess from stores +# we extended lib/core_ext/active_record/store/indifferent_coder.rb to read also ActionController::Parameters +# and convert them to ActiveSupport::HashWithIndifferentAccess for migration in db/migrate/20170910000001_fixed_store_upgrade_45.rb. +require 'active_record/store' +module ActiveRecord + module Store + class IndifferentCoder + def self.as_indifferent_hash(obj) + case obj + # re-enable using ActionController::Parameters in stores + when ActionController::Parameters + obj.permit!.to_h + # /re-enable using ActionController::Parameters in stores + when ActiveSupport::HashWithIndifferentAccess + obj + when Hash + obj.with_indifferent_access + else + ActiveSupport::HashWithIndifferentAccess.new + end + end + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 169cae892..5410c0174 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -61,6 +61,7 @@ class ActiveSupport::TestCase ActivityStream.destroy_all PostmasterFilter.destroy_all Ticket.destroy_all + Taskbar.destroy_all # reset settings Setting.all.pluck(:name).each { |name| diff --git a/test/unit/migration_ror_42_to50_store_test.rb b/test/unit/migration_ror_42_to50_store_test.rb new file mode 100644 index 000000000..56778693b --- /dev/null +++ b/test/unit/migration_ror_42_to50_store_test.rb @@ -0,0 +1,82 @@ +# encoding: utf-8 +# Rails 5.0 has changed to only store and read ActiveSupport::HashWithIndifferentAccess from stores +# we extended lib/core_ext/active_record/store/indifferent_coder.rb to read also ActionController::Parameters +# and convert them to ActiveSupport::HashWithIndifferentAccess for migration in db/migrate/20170910000001_fixed_store_upgrade_45.rb. + +require 'test_helper' + +class MigrationRor42To50StoreTest < ActiveSupport::TestCase + test 'store with ActionController::Parameters object - get ActiveSupport::HashWithIndifferentAccess' do + + user = User.last + last_contact = '2017-09-01 10:10:00' + state = "--- !ruby/hash-with-ivars:ActionController::Parameters +elements: + ticket: !ruby/hash-with-ivars:ActionController::Parameters + elements: {} + ivars: + :@permitted: false + article: !ruby/hash-with-ivars:ActionController::Parameters + elements: {} + ivars: + :@permitted: false +ivars: + :@permitted: false +" + params = "--- !ruby/hash-with-ivars:ActionController::Parameters +elements: + ticket_id: 1234 + shown: true +ivars: + :@permitted: false +" + preferences = "--- !ruby/hash-with-ivars:ActionController::Parameters +elements: + tasks: &1 + - !ruby/hash-with-ivars:ActionController::Parameters + elements: + id: 99282 + user_id: 85370 + last_contact: 2017-09-08 11:28:00.289663000 Z + changed: true + ivars: + :@permitted: false +ivars: + :@permitted: false + :@converted_arrays: !ruby/object:Set + hash: + *1: true +" + sql = "INSERT INTO taskbars (`user_id`, `client_id`, `key`, `callback`, `state`, `params`, `prio`, `notify`, `active`, `preferences`, `last_contact`, `updated_at`, `created_at`) VALUES (#{user.id}, '123', 'Ticket-123', 'TicketZoom', '#{state}', '#{params}', 1, FALSE, TRUE, '#{preferences}', '#{last_contact}', '#{last_contact}', '#{last_contact}')" + if ActiveRecord::Base.connection_config[:adapter] != 'mysql2' + sql.delete!('`') + end + records_array = ActiveRecord::Base.connection.execute(sql) + + taskbar = Taskbar.last + assert(taskbar) + assert(taskbar.params) + assert_equal(ActiveSupport::HashWithIndifferentAccess, taskbar.params.class) + assert_equal(1234, taskbar.params[:ticket_id]) + assert(taskbar.state) + assert_equal(ActiveSupport::HashWithIndifferentAccess, taskbar.state.class) + assert(taskbar.state[:ticket].blank?) + assert(ActiveSupport::HashWithIndifferentAccess, taskbar.state[:ticket].class) + assert(taskbar.state[:article].blank?) + assert(ActiveSupport::HashWithIndifferentAccess, taskbar.state[:article].class) + + taskbar.save! + taskbar.reload + assert(taskbar) + assert(taskbar.params) + assert_equal(ActiveSupport::HashWithIndifferentAccess, taskbar.params.class) + assert_equal(1234, taskbar.params[:ticket_id]) + assert(taskbar.state) + assert_equal(ActiveSupport::HashWithIndifferentAccess, taskbar.state.class) + assert(taskbar.state[:ticket].blank?) + assert(ActiveSupport::HashWithIndifferentAccess, taskbar.state[:ticket].class) + assert(taskbar.state[:article].blank?) + assert(ActiveSupport::HashWithIndifferentAccess, taskbar.state[:article].class) + end + +end