mirror of
https://github.com/discourse/discourse.git
synced 2026-08-09 12:38:21 -05:00
DEV: Introduce an array type for Chat contracts (#22278)
This small patch registers a new `ActiveModel` type: `array`.
It will split a string on `,` to create a new array. If the value is
already an array, nothing will happen and for all other types, it will
wrap the value in an array.
Here’s an example on an existing contract:
```ruby
attribute :target_usernames
before_validation do
self.target_usernames =
(
if target_usernames.is_a?(String)
target_usernames.split(",")
else
target_usernames
end
)
end
# can be rewritten as:
attribute :target_usernames, :array
```
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
# 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(",")
|
||||
else
|
||||
::Array.wrap(value)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user