A platform for community discussion. Free, open, simple.
DEV: Move more service code to core (#26613)
Martin Brennan380e5ca6cb67
diff --git a/config/initializers/100-active-support-type-extensions.rb b/config/initializers/100-active-support-type-extensions.rbnew file mode 100644index 00000000000..598f0df32dd--- /dev/null+++ b/config/initializers/100-active-support-type-extensions.rb@@ -0,0 +1,5 @@+# frozen_string_literal: true++Rails.application.config.to_prepare do+ ActiveModel::Type.register(:array, ActiveSupportTypeExtensions::Array)+enddiff --git a/lib/active_support_type_extensions/array.rb b/lib/active_support_type_extensions/array.rbnew file mode 100644index 00000000000..b65ebc99e9d--- /dev/null+++ b/lib/active_support_type_extensions/array.rb@@ -0,0 +1,28 @@+# frozen_string_literal: true++module ActiveSupportTypeExtensions+ class Array < ActiveModel::Type::Value+ def serializable?(_)+ false+ end++ def cast_value(value)+ case value+ when String+ value.split(",")+ when ::Array+ value.map { |item| convert_to_integer(item) }+ else+ ::Array.wrap(value)+ end+ end++ private++ def convert_to_integer(item)+ Integer(item)+ rescue ArgumentError+ item+ end+ end+enddiff --git a/plugins/chat/lib/chat/types/array.rb b/plugins/chat/lib/chat/types/array.rbdeleted file mode 100644index 81212f42cd0..00000000000--- a/plugins/chat/lib/chat/types/array.rb+++ /dev/null@@ -1,32 +0,0 @@-# frozen_string_literal: true--module Chat- module Types- class Array < ActiveModel::Type::Value- def serializable?(_)- false- end-- def cast_value(value)- case value- when String- value.split(",")- when ::Array- value.map { |item| convert_to_integer(item) }- else- ::Array.wrap(value)- end- end-- private-- def convert_to_integer(item)- Integer(item)- rescue ArgumentError- item- end- end- end-end--ActiveSupport.on_load(:active_record) { ActiveModel::Type.register(:array, Chat::Types::Array) }diff --git a/plugins/chat/plugin.rb b/plugins/chat/plugin.rbindex f3d710cbd6f..5760be0ebdb 100644--- a/plugins/chat/plugin.rb+++ b/plugins/chat/plugin.rb@@ -39,7 +39,6 @@ module ::Chatendrequire_relative "lib/chat/engine"-require_relative "lib/chat/types/array"after_initialize doregister_seedfu_fixtures(Rails.root.join("plugins", "chat", "db", "fixtures"))diff --git a/plugins/chat/spec/plugin_helper.rb b/plugins/chat/spec/plugin_helper.rbindex 670edb0a680..8b6451f04b6 100644--- a/plugins/chat/spec/plugin_helper.rb+++ b/plugins/chat/spec/plugin_helper.rb@@ -130,8 +130,6 @@ endRSpec.configure do |config|config.include ChatSystemHelpers, type: :systemconfig.include ChatSpecHelpers- config.include WithServiceHelper- config.include ServiceMatchersconfig.expect_with :rspec do |c|# Or a very large value, if you do want to truncate at some pointdiff --git a/plugins/chat/spec/lib/chat/types/array_spec.rb b/spec/lib/active_support_type_extensions/array_spec.rbsimilarity index 95%rename from plugins/chat/spec/lib/chat/types/array_spec.rbrename to spec/lib/active_support_type_extensions/array_spec.rbindex 8ffc96074b8..a6aab57b07c 100644--- a/plugins/chat/spec/lib/chat/types/array_spec.rb+++ b/spec/lib/active_support_type_extensions/array_spec.rb@@ -1,6 +1,6 @@# frozen_string_literal: true-RSpec.describe Chat::Types::Array do+RSpec.describe ActiveSupportTypeExtensions::Array dosubject(:type) { described_class.new }describe "#cast" dodiff --git a/spec/rails_helper.rb b/spec/rails_helper.rbindex cb42b4fb0d0..a3fae4edbb5 100644--- a/spec/rails_helper.rb+++ b/spec/rails_helper.rb@@ -201,6 +201,9 @@ RSpec.configure do |config|config.include UploadsHelpersconfig.include OneboxHelpersconfig.include FastImageHelpers+ config.include WithServiceHelper+ config.include ServiceMatchers+config.mock_framework = :mochaconfig.order = "random"config.infer_spec_type_from_file_location!