Ruby on Rails - Jan 2023
Sajjad Umar
Senior Backend Engineer | Ruby on Rails | Author of Ruby on Rails for Agile Web Development | Manager RubyConfPk | Desi Developer | Building adex.world
Hey Ruby Folkes, Happy New Year to everyone from your own Desi Developer .
A lot happened in the Ruby on Rails world in the past 30 days, and we will cover all of it here in a moment. But first things first; I will be speaking live at "Global Ruby on Rails Summit 2023" by Geekle.us on 25 January 2023.
See you guys there...
Subscribe now to join 1800+ readers and stay up to date with all the things happening in RoR world
Let's start with the most fantastic update of this month.
Ruby 3.2.0 Released
Ruby 3.2.0 has been released with tons of exciting new features - here is a brief summary of what's new:
Read all the details here.
Try Ruby - Playground, now supports CRuby - try now
Running rails -h will now show only relevant commands based on context
This pull request enhances the user experience by showing the relevant commands depending on the context in which the user is using the "rails -h" or "rails" command. When not in a Rails application, the output will show options for creating a new application, and when inside an application, it will display common Rails commands. This eliminates confusion for users who might have expected different commands in different situations.
Read all the details here.
Added default Dockerfiles
This pull request introduces the addition of Docker files as default in new applications: Dockerfile, .dockerignore, and bin/docker-entrypoint. These files can be excluded by using the option --skip-docker. These files are intended as a starting point for deploying the application in production and not for development purposes.
Read all the details here.
Docked Rails CLI
Setting up Rails for the first time with all the dependencies necessary can be daunting for beginners. Docked Rails CLI uses a Docker image to make it much easier, requiring only Docker to be installed.
Read all the details here.
Dockerfile will now match the Node version from the dev environment
This Pull Request has been created because deploying with a different node version than the application was tested with locally may not work as intended (or even at all).
Moving forward generated Dockerfile by installing the locally installed versions of Node and Yarn, decoupling the installation of libvips from Node, and adding the installation of?node_modules?by running?yarn.
Read all the details here.
HashWithIndifferentAccess#transform_keys now takes a Hash argument just like Ruby's Hash#transform_keys
The HashWithIndifferentAccess#transform_keys now has the same capability as Ruby's Hash#transform_keys by accepting a Hash as an argument.
Read all the details here.
test:prepare now runs before bin/rails test commands
This pull request simplifies the process of executing all tests in a CI environment by making bin/rails test equivalent to bin/rake test in terms of test preparation. This means that running bin/rails test alone will now execute all tests, eliminating the need for the additional bin/rake test command.
It's important to note that by default, running bin/rails test alone will not include system tests as they are generally slower. To include system tests as well, you can add an additional step to your CI pipeline that runs bin/rails test:system, or you can change your first step to bin/rails test:all which runs all tests including the system tests.
Read all the details here.
delegate now defines method with proper arity when delegating to a class
This change enhances the delegate method by verifying that the method being defined has the correct number of arguments when delegating to a class. This improves the performance of the defined method, which can be up to 3.5 times faster when there are no arguments. However, it is important to note that in order to benefit from this performance enhancement, the target method being delegated to must be defined before declaring the delegation.
Read all the details here.
Rails now have a default health controller
Load balancers and uptime monitors all need a basic endpoint to tell whether the app is up. Now rails have a default health controller that will be handy in many situations.
Read all the details here.
Karma will be used as the test runner for the UJS tests
Previously Blade was replaced by rollup for building UJS.
This PR continues this work and replaces Blade with Rollup for the build of the tests and Karma as the test runner as per the Action Cable tests.
This removes the last use of Blade which is one of the dependencies that is locking Rails to Rack 2.X.
While this isn't really user-facing, it is a huge step toward us being able to fully support Rack 3 in Rails.
Read all the details here.
Recently added Rails.env.local? is now almost 40% faster
A shorthand for Rails.env.development? || Rails.env.test? was added in this PR and now has some new performance optimizations
This optimization guarantees the performance of the new environment predicates will be on par with the pre-existing ones, as they now return a precomputed instance variable. This eliminates the need to be concerned about calling this method in production, as it's now 40% faster!
Read all the details here.
HostAuthorization will now only be used if configured
Previously, HostAuthorization would be added to the middleware stack whether or not config.hosts had any entries. While HostAuthorization does check config.hosts before doing computationally expensive things, its inclusion ends up being easily avoidable overhead on every request.
This PR removes the middleware stack from every request unless you've configured your app for a particular host.
Read all the details here.
Fixed a bug during database environment setup
This fixes a bug where we lost track of the environment between setting up the database for Rails.
Read all the details here.
Raise error on missing only unless for controller callbacks
The only and except options of a callback are allowed to contain symbols for actions that don't exist. This patch raises an error when one of those exceptional action names doesn't correspond to the actual actions available.
The purpose of this change is to prevent important callbacks from accidentally not being called when they should be, either from a typo.
领英推荐
before_action :authorize_user, only: [:showww] #typo
def show
end
or from changing the name of an action without realizing that a callback references it
before_action :authorize_user, only: [:admin_console]
# many lines of distracting code...
def admin_panel # method recently name changed from `admin_console`
end
While this behavior will default to false, it will be enabled on newly generated Rails apps.
Read all the details here.
Bumped required_rubygems_version to 3.3.13 or higher
This was necessary in order to support pre-release Ruby versions in the Gemfile template for newly generated Rails apps. However, due to it being such a huge jump please submit a new issue if you notice anything weird!
Read all the details here.
Added ability to match exception messages to assert_raises assertion
After the addition of this feature, instead of this:
error = assert_raises(SomeError) d0
? do_something
end
assert_equal "Some error message", error.message
Now yon do this;
assert_raises(SomeError, match: "Some error message") do # or using a regexp
do_something
end
Read all the details here.
Behaviour around before_committed! callbacks has changed
This pull request introduces a new configuration option in Rails 7.1 that enables before_committed! callbacks on all records enrolled in a transaction by default. Previously, callbacks were only triggered on the first copy of a record if multiple copies of the same record were enrolled in a transaction.
Read all the details here.
Rails Docs: Documented four ways to preload STIs
This pull request adds documentation on different methods for preloading Single Table Inheritance (STIs) in the Edge Rails Guides. The documentation provides information on how to preload STIs.
Read all the details here.
Fixed inconsistent behavior in form helper date/time tags with options
This change makes date/time options (value, min, max) in time_field, date_field, datetime_field, week_field, month_field form helpers behave in a unified way.
Previously we had:
<%= form.datetime_field :written_at, value: Time.current.strftime("%Y-%m-%dT%T") %>
And now we can do this:
<%= form.datetime_field :written_at, value: Time.current %>
Read all the details here.
TimeHelpers: includeed with_usec keyword parameter on travel & freeze
ActiveSupport::Testing::TimeHelpers has been updated to now accept an optional "with_usec" argument for the freeze_time, travel, and travel_to methods. When passed as true, this argument prevents truncation of the destination time's microseconds with change(usec: 0).
Read all the details here.
f.select can now be called with a single hash containing options and HTML options
This PR implements a special handling for required, multiple, and size HTML attributes so that these now do the same thing:
<%= select :post, :author, authors, include_blank: "Choose an option", required: true %>
<%= select :post, :author, authors, { include_blank: "Choose an option" }, { required: true } %>
Read all the details here.
ActiveRecord: only include all_queries default scopes on reload
This pull request alters the way in which default scopes are applied during a reload operation. The previous implementation applied all default scopes, regardless of their all_queries: true setting. The updated behavior applies only those default scopes that have been marked with all_queries: true.
Read all the details here.
Added support for pattern argument to ActiveRecord::Relation#none?/#any?/#one?
This change introduced the ability for ActiveRecord::Relation versions of the #none?, #any?, and #one? predicate methods to accept an optional pattern argument, similar to their Enumerable counterparts. This functionality was not present in these methods before this change.
Read all the details here.
assets:precompile can now run in a production build step without passing in RAILS_MASTER_KEY
When compiling assets in production as part of an image build step, it's inconvenient to have to pass in the real RAILS_MASTER_KEY. So allowed passing in a dummy secret_key_base, just like we do in development and test, via ENV["SECRET_KEY_BASE_DUMMY"] = 1.
This will not give access to any of the real credentials or message verifiers, but allow the build step to complete, since it typically does not need it anyway.
Read all the details here.
Added Message{Encryptors,Verifiers}#transitiona
This commit introduces a new attribute called "transitional" to ActiveSupport::MessageEncryptors and ActiveSupport::MessageVerifiers. When set to true, it will change the order of the first two rotations when constructing a message encryptor/verifier.
This can be useful during a phased deployment of an application, where older servers that have not yet been updated can still validate messages from updated servers.
Read all the details here.
Avoided unnecessary replacements in action text when the replacement node is unchanged
This pull request improves performance for situations where the replacement logic is conditional and results in the same unmodified node.
Read all the details here.
Official Merch
Share this Newsletter with your Ruby Friends - Your support gives me motivation to write this newsletter monthly.
And that is all for this month - I will be back with more updates next month, until then do share this newsletter with your ruby friends.
Sajjad Umar - Signing Out ??