Hi, it’s Emmanuel Hayford. Here’s a look at the highlights from this week’s updates to the Rails codebase.
Add schematized json for has_json
Provides a schema-enforced access object for a JSON attribute. This allows you to assign values
directly from the UI as strings, and still have them set with the correct JSON type in the database.
Only the three basic JSON types are supported: boolean, integer, and string. No nesting either.
These types can either be set by referring to them by their symbol or by setting a default value.
Default values are set when a new model is instantiated and on before_save (if defined).
Examples:
class Account < ApplicationRecord
has_json :settings, restrict_creation_to_admins: true, max_invites: 10, greeting: "Hello!"
has_delegated_json :flags, beta: false, staff: :boolean
end
a = Account.new
a.settings.restrict_creation_to_admins? # => true
a.settings.max_invites = "100" # => Set to integer 100
a.settings = { "restrict_creation_to_admins" => "false", "max_invites" => "500", "greeting" => "goodbye" }
a.settings.greeting # => "goodbye"
a.staff # => nil
a.staff = true
a.staff? # => true
Add SecureRandom.base32
Rails adds a new helper to SecureRandom in SecureRandom.base32, which generates a random Base32, uppercase, human-friendly, case-insensitive string. Essentially allowing:
SecureRandom.base32 # => "PAK1NG78CM1HJ44A"
SecureRandom.base32(24) # => "BN9EAB8RG9BNTTC9BX7P5JGJ"
ActionText: Validate RemoteImage URLs
RemoteImage.from_node now validates the URL before creating a RemoteImage object, using the same regex that AssetUrlHelper uses during rendering. URLs like “image.png” that would previously have been passed to the asset pipeline and raised an ActionView::Template::Error are rejected early, and gracefully fail by resulting in a MissingAttachable.
Introduce DevToolsController for Chrome workspaces
Add a new internal route in development to respond to Chromium DevTools GET requests.
This allows the app folder to be easily connected as a workspace in Chromium-based browsers.
Restore missing Postgres type decoding
Decode PostgreSQL bytea and money columns when they appear in direct query results.
bytea columns are now decoded into binary-encoded String values, and money columns are decoded into BigDecimal instead of String.
ActiveRecord::Base.connection
.select_value("select '\\x48656c6c6f'::bytea").encoding
#=> Encoding::BINARY
ActiveRecord::Base.connection
.select_value("select '12.34'::money").class
#=> BigDecimal
ActiveStorage immediate variants
This Pull Request introduces the immediate option and follows the existing patterns around preprocessed.
has_one_attached :avatar_with_immediate do |attachable|
attachable.variant :thumb, resize_to_limit: [4, 4], immediate: true
end
Extract ActionText::Editor base class and ActionText::TrixEditor adapter
The introduction of the ActionText::TrixEditor class enables the
deprecation of a variety of class and instance-level methods across the
ActionText namespace. Action Text benefits from deprecating methods in
order to reduce its public API.
You can view the whole list of changes here. We had 26 contributors to the Rails codebase this past week!
Until next time!
Subscribe to get these updates mailed to you.