{"id":5397,"date":"2016-05-12T17:02:42","date_gmt":"2016-05-12T20:02:42","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=5397"},"modified":"2016-05-12T17:08:54","modified_gmt":"2016-05-12T20:08:54","slug":"keeping-your-ruby-on-rails-app-easy-to-update","status":"publish","type":"post","link":"http:\/\/blog.plataformatec.com.br\/2016\/05\/keeping-your-ruby-on-rails-app-easy-to-update\/","title":{"rendered":"Keeping your Ruby on Rails app easy to update"},"content":{"rendered":"
The Rails 5 release candidate is out<\/a>, bringing new improvements that will make your life as a developer easier. Probably you are excited to update your application to the new major Rails release, but you may have some concerns. It is normal, updating your application to fit the new version may bring an unknown number of changes to work. On this blog post, I’ll give you some tips to keep the workload of updating at a minimum.<\/p>\n You can try the new Rails version using the published guides<\/a> and report any problem you may find on Rails issue tracker<\/a>.<\/p>\n I think it is an important question you may need to answer to your team members. My favorite reasons are: security<\/strong>, bug fixes<\/strong>, and features<\/strong>.<\/p>\n Rails has supported versions to receive security fix releases. If you are in a version not supported, you may be vulnerable. You should read the security policies<\/a> and check if your app is using the supported versions.<\/p>\n The framework also has maintenance policies<\/a> that you should be aware of. Performance improvements and bug fixes you may miss if your app is too old. You need to code by yourself and do workarounds to have the same benefits, a code that would be more reliable being inside the framework.<\/p>\n We usually hear developers complaining about applications that use old versions of the framework. The reason is new versions of the tool usually bring improvements to make the developer’s life easier. In the developer’s perspective, it’s demotivating knowing there’s a robust, elegant and productive way to do things and they are not able to use it.<\/p>\n Keeping your Rails up to date will help your code base health and also can be a factor of motivation for your team.<\/p>\n We have been maintaining and evolving several old Rails apps in different contexts for years, and we have seen some practices that make it easier to keep your app updated:<\/p>\n Adding new dependencies using RubyGems and bundler is awesome, but overusing gems can not only slow your app down but slow you with the amount of work you need to update your Rails version. Some gems are coupled to the framework when you update it. These gems may break. When they break, you need to update them together.<\/p>\n I recommend you considering these points before putting a new gem in your Gemfile:<\/p>\n Is this gem well maintained?<\/strong> It\u2019s good checking if the commits are recent, the project is well documented, the changelog contains helpful messages between releases, the maintainers often close issues and answer them respectfully. These are good signs that the gem you are adding won’t give you trouble in the future.<\/p>\n<\/li>\n<\/ul>\n Adding a gem to your project is adding a coupling. Software coupling has its downsides, for example, the ripple effect of changes. The downside of more work to update your dependencies will be worth it if you accurately added them. If you want to learn more about downsides of dependencies, you can read this blog post about Ruby dependencies<\/a> and this one about Node<\/a>.<\/p>\n Monkey patches are often used to change the behavior of the code you don’t own. For example, rewriting or adding Ruby standard library classes or methods to fit your application needs. Careless monkey patches can bring you serious problems while updating Rails.<\/p>\n Adding code that changes the behavior of gem classes can result in hidden bugs and turn any upgrade into a painful experience. It’s often uncertain how many objects are depending on your change and predicting all the effects of the monkey patched method. For example, in a new version of the gem, your monkey patch can change a method that was updated or doesn\u2019t even exist anymore.<\/p>\n We have seen some monkey patches that are a gem fix or enhancement. These changes may benefit others users of the gem and were hidden in application code. You may get in touch with gem maintainers and propose your changes. If your contribution is accepted, your fix will have a solid and proper place to be.<\/p>\n If your behavior is specific for your app, you should consider extending the gem and applying your wanted behavior using the conventional OO practices. For example, you can create an adapter class, subclass or composition. You may also take a look at Ruby Refinements<\/a>. Using this structure, you can create scoped changes that must be used explicitly. Explicit code reduces costs to maintain the app and the effort of updating to a new Rails version.<\/p>\n After checking all previous solutions and you still think that a monkey patch better suits your needs, you should know there are organized ways of adding them<\/a>. Regardless of coding, I recommend you to add a great documentation to keep it sane for your teammates and your future self. In this documentation, you should describe why it is necessary, when it can be removed and how it can be removed. It will help a better understanding of the app and provide useful information while updating your Rails app.<\/p>\n We all need to pay special attention to the tests. Some changes while updating Rails will be required and having a trustful test suite will help you discover problems before deploying your app to your end users.<\/p>\n Having 100% of test coverage is no guarantee of testing the correct behavior<\/a>. I would say the team should have a test coverage that they feel confident about.<\/p>\n Some symptoms that your application doesn\u2019t have good coverage is when recurrent errors are found while the application is running and all your automated tests are passing. You may use a tool like simplecov<\/a> to check your coverage rate, having a very low coverage is a bad sign.<\/p>\n Adding proper tests is the only solution for low test coverage. If you are in this situation, you should start adding tests focused on the most used and important features. Having a good test coverage is essential to evolve because its main purpose is providing fast feedback that previously working features are still working.<\/p>\n Keeping the application\u2019s Rails version updated is a responsibility that should not be ignored since it brings improvements for your team and security for your users.<\/p>\n I hope these tips help you keep your code easy to update.<\/p>\n Do you have more tips? Please leave comments below and tell us about your experience to keep your application up to date.<\/p>\n The Rails 5 release candidate is out, bringing new improvements that will make your life as a developer easier. Probably you are excited to update your application to the new major Rails release, but you may have some concerns. It is normal, updating your application to fit the new version may bring an unknown number … \u00bb<\/a><\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[7,248,60],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/5397"}],"collection":[{"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/comments?post=5397"}],"version-history":[{"count":18,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/5397\/revisions"}],"predecessor-version":[{"id":5488,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/5397\/revisions\/5488"}],"wp:attachment":[{"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/media?parent=5397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/categories?post=5397"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/tags?post=5397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}Why update?<\/h2>\n
What I should do?<\/h2>\n
\n
Gems<\/h3>\n
\n
Avoid monkey patches<\/h3>\n
Keep a good test coverage<\/h3>\n
Conclusion<\/h2>\n