ActiveSupport
Rails 5.1: Awesome Features and Breaking Changes

Summarized using AI

Rails 5.1: Awesome Features and Breaking Changes

Claudio B • May 26, 2017 • Phoenix, AZ

In this presentation at RailsConf 2017, Claudio B discusses the new features and breaking changes introduced in Rails 5.1. He explains the evolution of Rails, highlighting how minor releases balance between introducing exciting features and requiring developers to adapt their applications.

Key Points Discussed:
- Breaking Changes: Claudio addresses several breaking changes that developers may encounter when upgrading from Rails 5.0 to 5.1, such as:
- Change in the underlying engine for reading EOB files, which may affect certain gems.
- Changes in conditions for callbacks, which shift from string usage to proc or symbol-based conditions.
- Modifications in routing syntax, eliminating wildcard routes for improved security.
- New Features:
- Support for using big integers as primary keys in databases and adding virtual columns in MySQL.
- Improvements to Active Support, including better Unicode compliance for handling emojis and the introduction of new date methods.
- A new delegate_missing_to method that simplifies method delegation between classes.
- Changes in Action View with the introduction of new HTML tag helpers that simplify creating tags.
- A new form helper that consolidates previous form creation methods, making it easier for developers to implement forms.
- With Rails 5.1, jQuery is no longer a default dependency, instead, native JavaScript is leveraged for unobtrusive scripting.
- Integration of Yarn and Webpack to streamline JavaScript asset management in Rails applications.
- Enhanced Action Pack functionalities, including built-in Capybara support, simplifying the setup for system tests.
- Secret Management: The introduction of encrypted secrets to store sensitive data securely without exposing it in plaintext files, making deployment easier and more secure.

Claudio invites developers to explore the open-source nature of Rails and participate in its community, emphasizing the importance of staying informed about version changes to enhance their applications effectively. He concludes by encouraging upgrades to Rails 5.1 and thanking all contributors to the framework. This talk is particularly relevant for developers looking to improve their applications by leveraging the latest improvements in Rails 5.1.

Rails 5.1: Awesome Features and Breaking Changes
Claudio B • May 26, 2017 • Phoenix, AZ

RailsConf 2017: Rails 5.1: awesome features and breaking changes by Claudio B

Each minor release of Rails brings shiny new features and mild headaches for developers required to upgrade their applications and gems.

Rails 5.1 will support Yarn and modern Javascript transpilers, remove jQuery from the default stack, integrate system testing and a concurrent test runner, introduce a new helper to create forms, provide encrypted secrets and more.

In this talk, I will cover the improvements brought by Rails 5.1, explain the Core team’s motivations behind each feature, and illustrate the upgrade process to smoothly transition gems and apps from Rails 5.0 to Rails 5.1.

RailsConf 2017

00:00:17.010 Rails 5.1 is the latest release of the framework that we all love and enjoy.
00:00:25.050 The slides are available online, and you might want to check them out as they contain links to the pull requests I will be discussing. I only have 40 minutes, so I can't go into too much detail, but I invite you to look there for the commits and discussions, and hopefully get involved with open source.
00:00:38.850 To give you a little context, Rails 5.1 is going to be released any minute now. When Rails 5.0 was released, it was the result of 18 months of work and about 10,000 commits from around a thousand contributors. In contrast, you can think of 5.1 as kind of half of that—about nine months of work and around 4,000 commits.
00:01:04.140 One of the reasons you may see many lines changed in the codebase is that the Rails team is now using RuboCop for styling, which includes aspects like spaces, single quotes, and double quotes. However, this won't really affect you as users of Rails.
00:01:16.259 Today, I'll be discussing how this new version is going to affect you if you use Rails, particularly focusing on breaking changes in Active Record, Active Support, Action View, Railties, and Action Pack. As a last disclaimer, I'd like to mention that I have cats in this talk!
00:02:15.400 Let's start with breaking changes. I've upgraded my apps to 5.1, and I haven't encountered many issues that have affected me. There are some changes in the code which I will talk about, but they are not that significant.
00:02:22.790 The first change is in the engine that reads your ERB files. It has changed from 'erb' to 'erb', but the key difference is that 'erb' was not actually being maintained since 2011, while 'erb' has simpler internals and a smaller memory footprint. If your app is just using ERB files, you won't be affected at all because the result remains the same. However, if you are explicitly calling 'erb' in your code, like having a gem such as Haml or something that touches Action View, you'll want to look into this change. Otherwise, it simply benefits you by making things faster.
00:02:53.000 Another breaking change is about setting or skipping callbacks. You can now add conditions in a more straightforward way by passing them as strings. For example, if you have a post in your database, before saving that post, you might want to set 'no title' unless the title is present. Previously, you could pass a string, but in 5.1 you won't be able to use a string anymore. Instead, you can switch to using a proc or a symbol.
00:03:39.860 Still discussing breaking changes, these also affect your configuration routes. When you set your routes, you will need to use strings. For instance, you might say 'get dashboard index to home'. In these strings, there are a couple of special symbols you can use in your routes, such as 'get :controller/:action/:id'. This kind of wildcard syntax was valid until Rails 5.0, but it led to several security issues that have been addressed in previous versions. Moving forward, this syntax will not be acceptable anymore. If you have many controllers or actions, you'll need to specify them explicitly, which ultimately makes for cleaner and better code.
00:05:22.250 That's what I have regarding breaking changes. I'm not claiming these are all of them, and I encourage you to look at the change logs of the libraries. It's probably not going to be hard for you to upgrade to Rails 5.1. Now, let's start looking at the features that have been added or improved and what you can benefit from in your everyday work. Let's start with Active Record.
00:06:08.459 In Rails 5.1, primary keys in your databases can now be a big integer or big serial. If you import square, you'll basically have 64-bit identifiers, which is useful when you need to reference many records in your database. This is now the default behavior.
00:06:25.669 Still talking about databases, this is specifically for MySQL or equivalent MariaDB, which now supports virtual columns. For example, if you have a user table where every user has a name, you can now say that each user also has an uppercase version of their name as a virtual column.
00:06:39.360 MySQL allows defining this virtual column, so you don't duplicate data; you calculate the uppercase version directly. You can reference this column in your migration by using a simple line of Ruby code. What’s even better is that you can add indexing to this virtual column, allowing for queries that sort users by name length to be efficient and straightforward to implement.
00:07:35.920 If you have to deal with many records, such as thousands, Active Record provides methods to handle this better now, like 'find_each' in batches. In Rails 5.0, the limit method didn't really work properly with 'find_each', so you could end up getting warnings about scoped order and limit being ignored. Now, in Rails 5.1, the limit method is supported, which is a significant improvement.
00:08:01.560 Continuing with Active Record, there is now support for Unicode 9. An example of this is if you have a string containing a single emoji—particularly the family emoji, which represents two mothers, one daughter, and one son—wherein Rails 5.0, asking for the length of this string would have returned four due to how it counted characters. In Rails 5.1, this behavior has changed to accurately reflect the character count.
00:08:48.590 Active Support has introduced two new methods for date durations: 'since' and 'until', with aliases 'after' and 'before'. This makes it easier to express time intervals in natural language. For instance, you can write 'two weeks since Christmas Day' or 'five days until Christmas Day' and use the corresponding method for clarity in your tests.
00:10:15.630 One of the bigger features added in Rails 5.1 is a new method in the module called 'delegate_missing_to'. This method will be very useful for people employing the delegator pattern in their code. With it, you can delegate several methods to another class without having to specify each one.
00:10:37.920 For example, if you have an order Active Record model and another class like OrderCompletion that wraps around the Order class, it can delegate methods to Order without explicitly listing each one. In place of using the traditional 'delegate' method, you can simply call 'delegate_missing_to', providing a clean and efficient way to manage method delegation.
00:11:34.640 Moving on to Action View, there are several changes, the first being how you output an HTML tag. Previously, you had two helpers: 'tag' and 'content_tag'. For example, if you wished to output a break tag, you could do 'tag(:br, nil, true)'. The order was important: the first argument is the type of tag, the second is the content, and the third is whether it is a self-closing tag.
00:12:09.600 In Rails 5.1, we now have a more intuitive syntax with 'tag.br()' which reduces confusion by negating the need to remember the order of arguments. This is a less error-prone approach, especially useful for newcomers to Rails.
00:12:52.830 Continuing with forms, one of the common questions about Rails is the difference between 'form_for' and 'form_tag'. In Rails 5.1, there is now a new method called 'form_with' that serves as a unified approach, consolidating the other two. The idea is that 'form_with' will become the standard over time, enhancing clarity on how forms are constructed.
00:13:30.350 Specifically, when creating forms with 'form_with', it simplifies the transition and syntax from 'form_for' or 'form_tag'. If you do a brand new Rails application with 5.1, it will default to using 'form_with', while existing applications can still leverage the other methods during the transition.
00:14:07.839 I want to encourage everyone to explore the source code in relation to these features and consider upgrading your applications to Rails 5.1. I'd like to take a moment to express my gratitude to the many contributors who have significantly shaped these enhancements.
00:14:35.589 Now, let's move on to Railties. If you run the command 'rails initializers' in your terminal, it prints out all the initializers of your app. In Rails 5.0, it showed the methods' names only, but now in 5.1 it will output the class name along with the method name, providing more context and making it clearer, especially if you are using Rails engines.
00:15:06.150 Let's talk about secrets now. Every Rails app has a configuration file under 'config/secrets.yml' where you can store sensitive information like API keys. In Rails 5.1, they introduced an easier way to share variables among different environments. You can define shared secrets under a 'shared' section in your secrets.yml, making your code shorter and more readable.
00:15:43.030 Another significant addition is the capability to store encrypted secrets. In the secrets file, you might not want to store your production secrets in plain text. With Secrets in Rails 5.1, you can encrypt your secrets, making it safe to include them in your repository without exposing sensitive information.
00:16:44.490 Let's shift gears and talk about dependencies. jQuery is no longer a required dependency of Rails 5.1. While it doesn't mean that jQuery is obsolete—it’s still usable—it's a significant move toward a leaner framework. The initial inclusion of jQuery stemmed from the need for UJS (unobtrusive JavaScript) functionalities that allowed Rails to interact with the DOM in a seamless manner.
00:17:12.060 However, the same JavaScript functions can now be implemented without needing jQuery. This was corroborated by a Google Summer of Code student who rewrote the UJS library without it. Now, you can still access those same capabilities with the Rails UJS library, which is smaller and has a reduced footprint.
00:18:02.490 Another key feature introduced in Rails 5.1 is built-in support for Yarn, which is a package manager for JavaScript libraries, aligning with modern front-end development practices. When you create a new Rails project with 5.1, you automatically gain Yarn support without additional setup required. This makes managing JavaScript libraries within Rails much more efficient.
00:19:16.350 You also have the option of leveraging Webpacker, providing the ability to set up a more complex JavaScript architecture if you have single-page applications or extensive JavaScript usage in your Rails application.
00:19:49.350 When you set up a new Rails application with Webpacker, a variety of default configurations and installations are pre-defined for you, making it easier to manage your JavaScript dependencies and structure your app accordingly.
00:20:34.970 Additionally, Rails 5.1 provides a new command: 'webpack dev server', which will watch your JavaScript folder for changes, enabling a smoother development process for JavaScript applications. You can use JavaScript Pack Tags to easily include JavaScript packs in your views.
00:20:57.920 Finally, in Action Pack, Rails 5.1 introduces system tests that integrate Capybara for a richer testing experience. System tests allow developers to test their applications from an end-user perspective by simulating real user interactions in the browser…
00:21:31.150 System tests are easier to establish, as they now inherit from a base test case that sets everything up for you. Also, the older functional controller tests are still available but will not be generated for new applications. This means a more streamlined approach overall.
00:21:51.730 In summary, the major highlights of Rails 5.1 include Apple support for Yarn, system tests, and a more intuitive way to manage secrets. I wish you all the best in upgrading your Apps, exploring the features of Rails 5.1, and contributing continually to the community.
00:22:15.620 The change log for Rails can serve as a guide during your upgrade process, and I'd like to thank all the contributors who have made these changes possible.
00:22:40.600 Thank you very much for your time!
Explore all talks recorded at RailsConf 2017
+109