Rails 5.1: Loving JavaScript, System Tests, Encrypted Secrets, and more

In celebration of the 12th RailsConf in Phoenix, Arizona this week, we’re proud to announce that Rails 5.1 is ready in its final form! We’ve spent over 4,100 commits since Rails 5.0 making everything EASIER, SIMPLER, and, uhhh, FUNNER? (That’s a RailsConf joke).

The highlight reel hasn’t really changed since the first beta, but here’s a repeat:

Loving JavaScript

We’ve had a stormy, perhaps even contentious, relationship with JavaScript over the years. But that time is past. JavaScript has improved immensely over the past few years, particularly with the advent of ES6, and with package and compilation tools like Yarn and webpack. Rails is embracing both of these solutions with open arms and letting whatever past water flow under the bridge.

JavaScript and Ruby share a deep philosophical bond over language design, if not ecosystem management. Let’s focus on the aspects we have in common and help Rails programmers extract the best from JavaScript with the help of some key guiding conventions.

The improvements in Rails 5.1 focus on three major parts:

  1. Manage JavaScript dependencies from NPM via Yarn. Think of Yarn like Bundler for JavaScript (it even has Yehuda Katz involved!). This makes it easy to depend on libraries like React or anything else from NPM. Everything you depend on via Yarn is then made available to be required in the asset pipeline, just like vendored dependencies would have been. Just use the binstub bin/yarn to add dependencies.

  2. Optionally compile JavaScript with webpack. While there are a million different module bundlers/compilers for JavaScript, webpack is quickly emerging as the preeminent choice. We’ve made it easy to use webpack with Rails through the new Webpacker gem that you can configure automatically on new projects with --webpack (or even --webpack=react, --webpack=angular, or --webpack=vue for a tailored configuration). This is fully compatible with the asset pipeline, which you can continue to use for images, fonts, sounds, whatever. You can even have some JavaScript on the asset pipeline and some done via webpack. It’s all managed via Yarn that’s on by default.

  3. Drop jQuery as a default dependency. We used to require jQuery in order to provide features like data-remote, data-confirm, and the other parts of Rails UJS. This dependency is no longer necessary as we’ve rewritten rails-ujs to use vanilla JavaScript. You’re of course still free to use jQuery, but you no longer have to.

Thanks to Liceth Ovalles for her work on Yarn integration, Dangyi Liu for his work on rails-ujs, and Guillermo Iguaran for chaperoning the whole thing!

System tests

In my 2014 keynote at RailsConf, I spoke at length about how an over focus on unit tests (and TDD) has lead us astray. While unit tests are part of a complete testing solution, they’re not the most important one. Integration tests that verify behavior all the way from controllers through models and views should play a much bigger part. Rails already has a great answer for these baked in.

But integration tests do not help you test the entire system, if that system relies on JavaScript. And most major web systems today rely at least to some extent on JavaScript. That’s where system tests driven by a real browser come in.

There’s long been an answer for system tests like this in Ruby called Capybara. It’s just been kind of a journey to configure properly for Rails. So now we’ve baked them straight into the framework! You get a lovely wrapping of Capybara that’s preconfigured for Chrome and enhanced to provide failure screenshots as part of Action Dispatch. You also don’t have to worry about extra database cleanup strategies anymore because the baked in transactional tests now rollback system test changes.

These tests are not without trade-offs. It’s of course still slower to run through a whole browser setup than just test a model with a stubbed out database. But it also tests so much more. You’d do well to familiarize yourself with system tests and have them as part of your testing answer.

Thanks to Eileen M. Uchitelle for her work extracting this from Basecamp!

Encrypted secrets

If you’re checking production passwords, API keys, and other secrets undisguised into your revision control system, you’re doing it wrong. That’s not safe and you should stop it! Now that’s an easy prescription, but without a coherent answer to what you should do instead, it’s also not that helpful.

People have long been loading up the ENV to store these secrets or used a variety of other solutions. There are all sorts of trade-offs and drawbacks to the ENV-model, not least of which that you still need to store those secrets for real somewhere else.

Inspired by Ara T. Howard’s sekrets gem, we’ve built encrypted secrets management into Rails 5.1. You can setup a new encrypted secrets file with bin/rails secrets:setup. That’ll generate a master key you’ll store outside of the repository, but allow you to commit the actual production secrets to your revision control. They’re then decrypted in production either through an injected key file or through RAILS_MASTER_KEY in the ENV.

Thank you to Kasper Timm Hansen for the work on this and Ara for the inspiration!

Parameterized mailers

Action Mailer is modeled on Action Controller. It shares underpinnings through Abstract Controller, but it’s long been disadvantaged from its controller cousin in the way it can share logic between actions.

In Action Controller, it’s common to use before_action and similar callbacks to extract logic that applies to multiple actions. This is doable because the params hash is available before the action is invoked. But in Action Mailer, we’ve been using regular method signatures with explicit arguments, so those arguments haven’t been available to filters that run before the actions.

With Parameterized Mailers, we now give you the option of calling mailers with parameters that, like in controllers, are available before the action is invoked. This combines with the default to/from/reply_to headers to dramatically DRY-up some mailer actions.

It’s completely backwards compatible and you can convert just the mailers that stand to gain the most from extraction first.

Direct & resolved routes

We have a lovely, simple API for declaring new resource routes. But if you’d like to add new programmatic routes that has logic determining the final destination based on the parameters, well, you’d have to row your own boat with helpers and other messy approaches.

With directed routes, you can now declare programmatic routes that have the full power of Ruby to do different things depending on the parameters passed.

With resolved routes, you can reprogram the polymorphic look-up for models based straight to compatible methods. So this allow you to turn link_to @comment into a final route like message_path(@comment.parent, anchor: "comment_#{@comment.id}").

Thank you to Andrew White for making all this work!

Unify form_tag/form_for with form_with

We’ve long had two parallel structures for creating forms. Those that were based off records through form_for, where we used convention over configuration to extract the details, and manually configured ones using form_tag. Now we’ve unified these two hierarchies with form_with. A single root tree that you can configure through an inferred record or manually. It’s much nicer and simpler.

Thanks to Kasper Timm Hansen for this one too!

Everything else

In addition to the highlight reel, we have hundreds of other fixes and improvements across all the frameworks. Please peruse the CHANGELOGs to acquaint yourself with all the goodies:

We have a great summary of the high-level changes in the release notes.

Your release manager for Rails 5.1 was Rafael França.

As per our maintenance policy, the release of Rails 5.1 means that bug fixes will only apply to 5.1.x, regular security issues to 5.1.x and 5.0.x, and severe security issues to 5.1.x, 5.0.x, and 4.2.x. This means 4.x and below will essentially be unsupported!

Thank you to everyone in the community for their diligent job testing the beta and release candidates of Rails 5.1! We made more than 600 commits following bug reports and concerns raised through this process. Thank you! Gracias! Merci! TAK!

Rails 5.1.0.rc2: Loving JavaScript, System Tests, Encrypted Secrets, and more

We’re happy to announce Rails 5.1.0.rc2 has been released. We are now really close to the final release. We still have a few open issues in the milestone but we are still expecting to be able to do the final release during the Railsconf 2017.

While we are working to close the last issues, you can check the Rails 5.1 releases notes, or the awesome summary of new features present in the 5.1.0.beta1 blog post.

To view the changes for each gem, please read the changelogs on GitHub:

As per our maintenance policy, the release of Rails 5.1 will mean that bug fixes will only apply to 5.1.x, regular security issues to 5.1.x and 5.0.x, and severe security issues to 5.1.x, 5.0.x, and 4.2.x. This means 4.x and below will essentially be unsupported!

To try out this new release:

gem install rails -v 5.1.0.rc2

Make sure you are using RubyGems 2.6.11 or later before trying to install a pre release, with:

gem update --system 2.6.11

Please help us test this release candidate version of Rails. It’s always frustrating when we put a lot of work into a new release, betas, release candidates, and then get people report all sorts of issues on week one of the final release. This is an incremental upgrade to Rails 5.0. Please do your community duty and help us land a solid 5.1 without needing an immediate 5.1.1.

Thank you!

This Week in Rails: hooks, fixtures, fetch_values, database connections and more!

Hi there! This is Andy with another edition of This Week in Rails. This week we’re covering two weeks worth of changes, so let’s dive right in.

New

Hooks for action_controller_base and action_controller_api

This change breaks out two Action Controller hooks, where there was one before. action_controller_base and action_controller_api. These can be used to load less code at boot time.

Fixture accessor with no arguments

Do you need to load all the fixtures of a particular type? Calling a fixture accessor method with no arguments returns all fixtures.

Implement fetch_values for HashWithIndifferentAccess

Add fetch_values for HashWithIndifferentAccess in Rails, added to Ruby in 2.3.0. Support was added conditionally by using method_defined?.

Fixed

Clear active connections after initialization

If your app uses a lot of database connections, this one is worth a look. The author said that after deploying this change, they reduced their peak open database connection count by 13%.

Fix mistake in JS response parser

This change restores the ability to accept ecmascript for parsing. The author also declares that the JS response should not modify the DOM. Take a look!

Improved

Doc improvement for before_destroy

Did you know that before_destroy hooks should be set *before* dependent: destroy for it to work correctly? This documents that.

Don’t freeze input strings

Check this out if you’re curious about changes around frozen strings and usages of Immutable String (Matz said “All String literals are immutable (frozen) on Ruby 3”).

Whew! That covers just some of the changes in the first couple of weeks of April. Many other great pull requests are available to browse, from 27 contributors including 5 first-timers. Nice work!

Until next week!

This Week in Rails: -j smoked, 42, designated drivers and more!

Yo folks! Your noble editor Kasper here with the latest and greatest from Rails.

Check the fine print 📰

Remove rails new –javascript

The –javascript option for new Rails apps could install any gem ending in -rails. From now you’ll have to add jquery-rails to your Gemfile manually.

Properly sourced fourty_two

The new documentation now has the answer to life the universe and everything.

New

Per subclass system testing driver overrides

Your system testing test case subclasses all have a designated driver. Now you can designate another driver in case the test case calls for it.

reverse_merge aliased to with_defaults

One for the aesthetics department: with_defaults has emerged as an alias for_reverse_merge_. So now_options.with_defaults(creator: @current_user)_makes your intent clearer.

Fixed

Fixed rails-ujs.js module support

Once Rails 5.1 ships proper you’ll be able to require rails-ujs from your JS again.

There were many other great pull requests this week from 45 contributors, including 8 first-timers. Thank you all!

Until next week!

This Week in Rails: 5.1.0.rc1, GSoC and more!

Hello everyone! This is Roque bringing the latest from the last two weeks.

The last couple of weeks have been very busy with the releasing of Rails 5.1.0.rc1 and the starting of Rails 5.2 🎉

Congratulations to all involved!!

Rails 5.1.0.rc1 has been released!

Please check it out and help the community by reporting issues before 5.1 is released.

If you are already using encrypted secrets, make sure to run this script to upgrade your app.

Google Summer of Code 2017

Are you a student, or know someone who would love to contribute to Rails? Our GSoC students application is now open! Check out our ideas page and join us on the mailing list for discussion.

The RailsConf 2017 schedule is live!

Have you also been waiting for this? No more. The schedule is live and looks awesome!

New

Add default option to belongs_to

The :default option adds a before_validation callback that initializes the association with the given lambda’s return value:
belongs_to :person, default: -> {​ Current.person }​

Add support to duplicable Rational and Complex

This is a work ahead of the upcoming Ruby changes.

Fixed

Allow Time#to_time on frozen objects

This fixes an issue with frozen that has been around since Rails 4.

Allow order to be given expressions as hash keys

The Active Record order method is no longer restricted to attribute names. It now accepts expressions like:
Post.order(“LENGTH(title)” => :asc).last

There were many other great pull requests this week from 45 contributors, including 8 first-timers. Thank you all!

Until next week!

Google Summer of Code 2017 Application Period

Want to participate in this year’s Google Summer of Code? Great! The student application period is now open. Be swift, though, as it ends on the 3rd of April.

If you are interested, you can check out the Google Summer of Code FAQ or hit us up on the mailing list if you have any questions.

To apply, head on to the Google Summer of Code website and write us a proposal. You can check out our existing list of ideas, but feel free to pitch one of your own as well. What is a good proposal you may ask? You can see a bunch of accepted proposals on our Google Summer of Code wiki.

Good luck! ✌️

Rails 5.1.0.rc1: Loving JavaScript, System Tests, Encrypted Secrets, and more

We’re happy to announce Rails 5.1.0.rc1 has been released. With the help of the community we polished the Rails 5.1 release with more than 380 commits.

Notably, Stephen Touset helped us up the encryption used for encrypted secrets, so you’ll want to run this script to upgrade your app if you’re already dabbling with the feature. Thanks Stephen!

While we are working to close the last issues, you can check the Rails 5.1 releases notes, or the awesome summary of new features present in the 5.1.0.beta1 blog post.

To view the changes for each gem, please read the changelogs on GitHub:

Basecamp 3 is already running this release candidate in production. Our friends at Heroku prepared the platform that is now fully compatible with all those shiny new features.

As per our maintenance policy, the release of Rails 5.1 will mean that bug fixes will only apply to 5.1.x, regular security issues to 5.1.x and 5.0.x, and severe security issues to 5.1.x, 5.0.x, and 4.2.x. This means 4.x and below will essentially be unsupported!

To try out this new release:

gem install rails -v 5.1.0.rc1

Make sure you are using RubyGems 2.6.11 or later before trying to install a pre release, with:

gem update --system 2.6.11

Thank you to Samuel Giddins for releasing this new version of RubyGems just in time for our release.

Please help us test this release candidate version of Rails. It’s always frustrating when we put a lot of work into a new release, betas, release candidates, and then get people report all sorts of issues on week one of the final release. This is an incremental upgrade to Rails 5.0. Please do your community duty and help us land a solid 5.1 without needing an immediate 5.1.1.

Thank you!

This Week in Rails: looots of fixes after the release and more.

Hello!

This is Vipul reporting from Delhi today, on this colourful Holi weekend 💥
 
Lets take a look at what’s the latest news from the world of Ruby on Rails !
So many fixes and much more.

Google Summer of Code 2017

We’re very happy to announce that Ruby on Rails has been accepted as an organization for the Google Summer of Code (GSoC) 2017 edition!

Fixed

Make sure local cache is cleared even if it’s throwing error

When an error was thrown from the middleware chain, it was sometimes not getting caught by LocalCache::Middleware.

This change, makes sure that we safely catch the errors and local cache is successfully cleared.

Fix rake db:schema:load with subdirectories

Previously db:schema:load didn’t work with migrations based out of subdirectories.

This change starts using Migrator.migration_files in assume_migrated_upto_version to fix the issue.

Fix malformed asset_url in ActionController::Renderer

This change fixes an issue with malformed asset_url when rendering a template with ActionController::Renderer.

Fix select with block doesn’t return newly built records in has_many association

The select method in QueryMethods is also an enumerable method. Ideally, enumerable methods with block should delegate to records on CollectionProxy and not the scope.

This change fixes this behaviour that select with block doesn’t return newly built records in has_many association.

Update titlelize regex to allow apostrophes

In a previous fix the regex in titlelize was updated to not match apostrophes to better reflect the nature of the transformation.

Unfortunately this had the side effect of breaking capitalization on the first word of a sub-string, eg:

>> "This was 'fake news'".titleize
=> "This Was 'fake News'"

This change fixes this behaviour by extending the look-behind to check for word character apart from an apostrophe.

Fix deserialize with JSON array

JSON array data was being returned in a stringified form, due to broken deserialization.

This change fixes deserialization behaviour.

Deprecated

Deprecate Migrator.schema_migrations_table_name

SchemaMigration model was extracted and SchemaMigration.table_name was being used instead in places where Migrator.schema_migrations_table_name was being used previously.

Migrator.schema_migrations_table_name is now deprecated in favour of SchemaMigration.table_name.

Improved

Added reverse_merge/reverse_merge! to AC::Parameters

This change adds the reverse_merge and reverse_merge! methods to ActionController::Parameters, similar to their corresponding Hash methods.

Do not take screenshot if driver does not support screenshot

Capybara::RackTest::Driver does not support taking screenshots. If we call #save_screenshot on Capybara::RackTest::Driver , it will raise an error.

To prevent errors, if a driver in Systems test does not support screenshot, we do not call it after this change.

There were many other great pull requests this week from 20 contributors, including 4 first-timers. Thank you all!

Outside of code changes, we had many more good news this and the previous week.

Until next week!

This Week in Rails: 5.0.2 released, stronger Encrypted Secrets and more

Hello!

It’s Claudio from sunny Los Angeles bringing you the latest news from the world of Ruby on Rails. So many improvements this week so… let’s get to it!

Rails 5.0.2 has been released!

Go on and update your Rails 5 app to the latest release!
v5.0.2 is fully backward-compatible with v5.0.1 and v5.0.0.

Google Summer of Code 2017

We’re very happy to announce that Ruby on Rails has been accepted as an organization for the Google Summer of Code (GSoC) 2017 edition!

Improved

Update secrets to use modern crypto

As announced last week, Rails 5.1 will include encrypted secrets. Thanks to this PR, the secrets will use a stronger cryptographic algorithm for enhanced security.

New

Add iso8601 and rfc3339 parsing to timezones

ActiveSupport::TimeZone now has a new rfc3339 method to get an RFC 3339 timestamp in a specific time zone.

Use webpacker 1.0 in new applications

In case you missed it, rails/webpacker is out of beta development and has recently released v1.0.

Fixed

Only load SystemTestCase if Puma is defined

System test cases (Capybara) are one of the new features in Rails 5.1. Since they only support Puma, they are skipped if Puma is not present.

Do not evaluate :if arguments when :on is not satisfied for transaction callbacks

Understanding when callbacks are run can be tricky. This PR smartly avoids running them as soon as one of the preconditions is not met.

Update DateTime#change to support usec and nsec

If you need to change a timestamp by a matter of microseconds and nanoseconds, now you can do it!

Deprecated

Soft-deprecate the HashWithIndifferentAccess constant

If you use HashWithIndifferentAccess in your code, you might want to look at using ActiveSupport::HashWithIndifferentAccess  instead.

Deprecate implicit coercion of ActiveSupport::Duration

A snippet of code like 2 * 1.day will display a warning, inviting you not to coerce a Duration into a Numeric since it can be confusing, because adding/subtracting a value from a date treats integers as a day and not a second.

There were many other great pull requests this week from 35 contributors, including 12 first-timers. Thank you all!

Outside of code changes, we had many more good news this week.

Until next week!

Google Summer of Code 2017

We’re very happy to announce that Ruby on Rails has been accepted as an organization for the Google Summer of Code (GSoC) 2017 edition!

The GSoC is a program proposed by Google that allows college students (who are at least 18 years old) to contribute to open source projects during the summer (from May 30 to August 21) and get paid for that!

Rails has already participated in the past and many different projects have been achieved through this program and are now invaluable in the framework’s ecosystem like the web console, the RubyBench site or the rails-ujs project.

Students need to propose an idea that will improve the project they are willing to work on and eventually the different steps that will be tackled to achieve it. Throughout the process, students are guided by one or several mentors. Mentors are here to make sure that students go in the right direction and help them if they stumble against problems.

This can be a very interesting and rewarding experience for students as they can learn a lot from more experienced developers and it’s an easy way to get involved in the open source world.

A list of possible ideas is already available if you want to work on Ruby on Rails this summer but feel free to propose your own if you want to work on something different and you still think it can be valuable for the project.

If you are interested in getting involved, please join the mailing list and let us know what you would like to work on. Thus, you can get early feedback and avoid going in the wrong direction or putting too much effort in a project that may not be accepted.

Student applications will be open on March 20 and will end on April 3. Make sure to keep an eye on the timeline if you are willing to participate to this program. You can find the application template on our wiki.

If you are not a student, you can still get involved by participating on the mailing list or by applying as a mentor!

As a side note, a similar project is available and is aiming at getting more women involved in the open source world: Rails Girls Summer of Code. Unlike GSoC, this project is exclusively about Ruby on Rails, it’s not restricted to students and there are no age limitations. Applications are open and will close very soon though, on March 8.

Useful resources: