mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 03:36:18 +08:00
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 <j.jaffeux@gmail.com>
This commit is contained in:
@ -0,0 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
Rails.application.config.to_prepare do
|
||||||
|
ActiveModel::Type.register(:array, ActiveSupportTypeExtensions::Array)
|
||||||
|
end
|
28
lib/active_support_type_extensions/array.rb
Normal file
28
lib/active_support_type_extensions/array.rb
Normal file
@ -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
|
@ -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) }
|
|
@ -39,7 +39,6 @@ module ::Chat
|
|||||||
end
|
end
|
||||||
|
|
||||||
require_relative "lib/chat/engine"
|
require_relative "lib/chat/engine"
|
||||||
require_relative "lib/chat/types/array"
|
|
||||||
|
|
||||||
after_initialize do
|
after_initialize do
|
||||||
register_seedfu_fixtures(Rails.root.join("plugins", "chat", "db", "fixtures"))
|
register_seedfu_fixtures(Rails.root.join("plugins", "chat", "db", "fixtures"))
|
||||||
|
@ -130,8 +130,6 @@ end
|
|||||||
RSpec.configure do |config|
|
RSpec.configure do |config|
|
||||||
config.include ChatSystemHelpers, type: :system
|
config.include ChatSystemHelpers, type: :system
|
||||||
config.include ChatSpecHelpers
|
config.include ChatSpecHelpers
|
||||||
config.include WithServiceHelper
|
|
||||||
config.include ServiceMatchers
|
|
||||||
|
|
||||||
config.expect_with :rspec do |c|
|
config.expect_with :rspec do |c|
|
||||||
# Or a very large value, if you do want to truncate at some point
|
# Or a very large value, if you do want to truncate at some point
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
RSpec.describe Chat::Types::Array do
|
RSpec.describe ActiveSupportTypeExtensions::Array do
|
||||||
subject(:type) { described_class.new }
|
subject(:type) { described_class.new }
|
||||||
|
|
||||||
describe "#cast" do
|
describe "#cast" do
|
@ -201,6 +201,9 @@ RSpec.configure do |config|
|
|||||||
config.include UploadsHelpers
|
config.include UploadsHelpers
|
||||||
config.include OneboxHelpers
|
config.include OneboxHelpers
|
||||||
config.include FastImageHelpers
|
config.include FastImageHelpers
|
||||||
|
config.include WithServiceHelper
|
||||||
|
config.include ServiceMatchers
|
||||||
|
|
||||||
config.mock_framework = :mocha
|
config.mock_framework = :mocha
|
||||||
config.order = "random"
|
config.order = "random"
|
||||||
config.infer_spec_type_from_file_location!
|
config.infer_spec_type_from_file_location!
|
||||||
|
Reference in New Issue
Block a user