Ruby on Rails - March 2024
Sajjad Umar
Senior Backend Engineer | Ruby on Rails | Author of Ruby on Rails for Agile Web Development | Manager RubyConfPk | Desi Developer | Building adex.world
Welcome to the March edition of "Ruby on Rails, Monthly" newsletter for 2024! As we delve into this month's updates, let's approach our coding journey with a renewed sense of curiosity and determination. May March bring you fresh insights and a deeper appreciation for the artistry of Rails development.
I'm Sajjad Umar, your DesiDeveloper, and I'm thrilled to be your guide through the ever-evolving landscape of Ruby on Rails. Let's embark on this month's adventure with enthusiasm and eagerness!
Furthermore, as we commemorate International Women's Day, let's take a moment to recognize the invaluable contributions of women in the tech industry, particularly those who have played a pivotal role in shaping the world of Ruby on Rails development. Their ingenuity and dedication serve as a beacon of inspiration for us all.
Without further ado, let’s jump right in…
CFP for Rails World is closing next week
The CFP for Rails World 2024 will remain open until 21st March. If you want to speak at the conference, submit your proposal here.
Rails Versions 6.1.7.7, 7.0.8.1, and 7.1.3.2 have been released!
These are security releases, so please upgrade at your earliest convenience.
Read all the details here.
Active Record Basics Guide Updated
This PR refreshes the ActiveRecord basic guide to:
Read all the details here.
railties: configure sanitizer vendor in 7.1 defaults more robustly
rails-html-santizer is a dependency of Action View and a transitive dependency of Action Text (via Action Pack), but may not be loaded until after railties sets configuration defaults, meaning that the sanitizer vendor may remain Rails::HTML4 and not be set to Rails::HTML5 as we intend in Rails 7.1.
This change requires rails-html-sanitizer immediately before it's needed, and avoids the possibly-incorrect assumption that Rails::HTML::Sanitizer is already defined.
Read all the details here.
Active Record Query Guide is Open for Review & Feedback
The Active Record Querying Guide is undergoing restructuring, and the team managing it seeks input from the community.
Read all the details here.
Devcontainer files by default
This Pull Request adds templates for devcontainer files (devcontainer.json, Dockerfile and docker-compose.yml) to be generated on rails new.
By default, the devcontainer contains the following:
If rails new is called with options to skip any of the above they will be removed from the dev container config.
The dev container can be skipped entirely with the --skip_devcontainer option.
When creating a new app, this modification produces a .devcontainer directory along with its contents. Within the .devcontainer folder lies all necessary components for initializing the app and conducting development within a remote container environment.
Read all the details here.
Allowed .test by default in development
By default, puma-dev uses the .test TLD to route development requests. Right now you'll get the following error message:
This PR ensures that this works out of the box.
Read all the details here.
Puma-dev as Docker alternative
If you are not using Docker, rails suggest puma-dev as a golden path to develop multiple rails apps locally. bin/setup is updated to now suggests how to setup puma-dev.
Read all the details here.
Added ActiveRecord::Base.with_connection as a shortcut
This update introduces ActiveRecord::Base.with_connection as a convenient method for temporarily acquiring a connection. The acquired connection is provided as a yield, ensuring that any subsequent calls to ActiveRecord::Base.connection within the block utilize the same connection. This functionality proves beneficial for executing a limited number of database operations without retaining a connection for the entirety of the request or job.
Read all the details here.
assert_emails: now returns the emails that were sent
With this PR:
email = assert_emails 1 do
post invite_user_path # ...
end
assert_equal "You were invited!", email.subject
If a single email is sent, a single Mail::Message is returned. If multiple emails were sent, an array is returned.
emails = assert_emails 2 do
post invite_user_path # ...
end
emails.each do |email|
assert_equal "You were invited!", email.subject
end
Before this PR, assert_emails just returned true (or raised if the assertion failed). So while some tests may be relying on that return value, they don't really need to and it shouldn't be a very annoying change to fix them.
Read all the details here.
Logger.logger_outputs_to? now support filenames
Logger.logger_outputs_to? used to supports STDOUT and STDERR, not File objects or plain filenames. This PR makes Logger.logger_outputs_to? support file objects or filenames.
Read all the details here.
Autosaving has_one sets foreign key attribute when unchanged
This PR updates ActiveRecord::AutosaveAssociation#save_has_one_association to only update the foreign key attribute on the child record if it has changed. This makes its behaviour consistent with belongs_to associations and ensures that ActiveRecord::ReadOnlyAttributeError isn't raised when no changes have been made to the attribute.
Read all the details here.
Fixed override existing join types in the query in the where.associated method
The pivotal improvement in this Pull Request is the inclusion of a check ensuring that associations are joined using the appropriate join type (either inner join or left outer join) based on existing joins within the scope. This safeguards against unintentional overrides of existing join types and fosters consistency in the generated SQL queries.
Example:
领英推荐
User.left_outer_joins(:orders).where.associated(:orders).count
Old query
SELECT COUNT(*) FROM "users" INNER JOIN "orders" ON "orders"."user_id" = "users"."id" WHERE "orders"."id" IS NOT NULL
New query after this fix:
SELECT COUNT(*) FROM "users" LEFT OUTER JOIN "orders" ON "orders"."user_id" = "users"."id" WHERE "orders"."id" IS NOT NULL
Read all the details here.
Added parameter filter capability for redirect locations
This PR has completed an accepted pull request #21045 submitted 8 years ago because the author had no time to keep working on it.
It uses the config.filter_parameters to match what needs to be filtered. The result looks like this:
Redirected to https://secret.foo.bar?username=roque&password=[FILTERED]
This PR also made improvements to make the code clearer.
Read all the details here.
Exposed a generic fixture accessor for fixture names that may conflict with Minitest
With this change it is not possible to load fixtures like this:
assert_equal "Ruby on Rails", web_sites(:rubyonrails).name
assert_equal "Ruby on Rails", fixture(:web_sites, :rubyonrails).name
Read all the details here.
Fixed: Model.query_constraints with single non-primary-key column raises incorrect error
This Pull Request changes ActiveRecord::Reflection::AssociationReflection#derive_fk_query_constraints to raise an existing more appropriate error if Model.query_constraints is given a single non-primary-key column argument.
Read all the details here.
Set action_mailer.default_url_options values in development and test
Prior to this commit, new Rails applications would raise ActionView::Template::Error if a mailer included a url built with a *_path helper.
Since we already know new apps will be served on localhost:3000, we set this as the value in development.
In an effort to remain consistent with existing patters, we set the host to www.example.com in test.
Read all the details here.
Illustrator .ai files are previewable as PDFs
This happened to work with Marcel 1.0.2 and earlier since magic byte sniffing sees that Illustrator files are PDFs internally, causing these files to be treated as application/pdf despite having a declared content type of application/illustrator and an .ai file extension.
Marcel 1.0.3 corrected this to the more specific application/illustrator subtype of application/pdf, but the MuPDF previewer only accepts the parent application/pdf type.
Changing it to accept PDF and any child types allows the previewer to explicitly work with Illustrator files again, which was only a happy accident previously.
Read all the details here.
Introduced Rails::Generators::Testing::Assertions#assert_initializer
This PR complements the existing initializer generator action.
assert_initializer "mail_interceptors.rb"
assert_initializer "mail_interceptors.rb", /SandboxEmailInterceptor/
assert_initializer "mail_interceptors.rb" do |initializer|
assert_match(/SandboxEmailInterceptor/, initializer)
end
Read all the details here.
Added dirties option to Model.uncached
This adds a dirties option to ActiveRecord::Base.uncached and ActiveRecord::ConnectionAdapters::ConnectionPool#uncached.
Setting dirties to false, means database writes to the connection pool will not mark any query caches as dirty.
The option defaults to true which retains the existing behavior and clears query caches on all connection pools used by the current thread.
Read all the details here.
Deprecated ConnectionPool#connection
This pull request introduces deprecation for ActiveRecord::Base.connection and ActiveRecord::ConnectionAdapters::ConnectionPool#connection in favor of .lease_connection. The method has been renamed to lease_connection to more accurately convey that the retrieved connection will be retained throughout the request or job. Deprecation of ActiveRecord::Base.connection is considered soft, meaning no warnings will be issued, and there are no immediate plans to remove the method.
Read all the details here.
Desi Developer
A small update on my upcoming book titled “Ruby on Rails for Web Development“. Out of 12 chapters, 10 are now complete, pushing myself to complete it by the end of this month (fingers crossed).
I’m putting my heart and soul into writing this book, I hope it can help people learn Ruby on Rails for Web Development.
Chapter 10 was all about building an MVP of a real-world application, that can be deployed as a working SaaS system.
2 more to go!
If you want to support this newsletter, consider subscribing to the paid version on substack here:
Alternatively, you can buy me a coffee here: https://ko-fi.com/sumar7
That is all for now, will be back with more updates next month. Until then ??