From 380e5ca6cb6704c0949f1c2cadb6a8569e264e29 Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Fri, 12 Apr 2024 21:14:19 +1000 Subject: [PATCH] DEV: Move more service code to core (#26613) This is to enable :array type attributes for Contract attributes in services, this is a followup to the move of services from chat to core here: cab178a40557e205e9c3e75fcb411a5e0e164d15 Co-authored-by: Joffrey JAFFEUX --- .../100-active-support-type-extensions.rb | 5 +++ lib/active_support_type_extensions/array.rb | 28 ++++++++++++++++ plugins/chat/lib/chat/types/array.rb | 32 ------------------- plugins/chat/plugin.rb | 1 - plugins/chat/spec/plugin_helper.rb | 2 -- .../array_spec.rb | 2 +- spec/rails_helper.rb | 3 ++ 7 files changed, 37 insertions(+), 36 deletions(-) create mode 100644 config/initializers/100-active-support-type-extensions.rb create mode 100644 lib/active_support_type_extensions/array.rb delete mode 100644 plugins/chat/lib/chat/types/array.rb rename {plugins/chat/spec/lib/chat/types => spec/lib/active_support_type_extensions}/array_spec.rb (95%) diff --git a/config/initializers/100-active-support-type-extensions.rb b/config/initializers/100-active-support-type-extensions.rb new file mode 100644 index 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) +end diff --git a/lib/active_support_type_extensions/array.rb b/lib/active_support_type_extensions/array.rb new file mode 100644 index 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 +end diff --git a/plugins/chat/lib/chat/types/array.rb b/plugins/chat/lib/chat/types/array.rb deleted file mode 100644 index 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.rb index f3d710cbd6f..5760be0ebdb 100644 --- a/plugins/chat/plugin.rb +++ b/plugins/chat/plugin.rb @@ -39,7 +39,6 @@ module ::Chat end require_relative "lib/chat/engine" -require_relative "lib/chat/types/array" after_initialize do register_seedfu_fixtures(Rails.root.join("plugins", "chat", "db", "fixtures")) diff --git a/plugins/chat/spec/plugin_helper.rb b/plugins/chat/spec/plugin_helper.rb index 670edb0a680..8b6451f04b6 100644 --- a/plugins/chat/spec/plugin_helper.rb +++ b/plugins/chat/spec/plugin_helper.rb @@ -130,8 +130,6 @@ end RSpec.configure do |config| config.include ChatSystemHelpers, type: :system config.include ChatSpecHelpers - config.include WithServiceHelper - config.include ServiceMatchers config.expect_with :rspec do |c| # Or a very large value, if you do want to truncate at some point diff --git a/plugins/chat/spec/lib/chat/types/array_spec.rb b/spec/lib/active_support_type_extensions/array_spec.rb similarity index 95% rename from plugins/chat/spec/lib/chat/types/array_spec.rb rename to spec/lib/active_support_type_extensions/array_spec.rb index 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 do subject(:type) { described_class.new } describe "#cast" do diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index cb42b4fb0d0..a3fae4edbb5 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -201,6 +201,9 @@ RSpec.configure do |config| config.include UploadsHelpers config.include OneboxHelpers config.include FastImageHelpers + config.include WithServiceHelper + config.include ServiceMatchers + config.mock_framework = :mocha config.order = "random" config.infer_spec_type_from_file_location!