Codecast: Getting Started with Rails 7 Episode 16: Refactor Repeated Code Using Concerns

The concerns section on the Rails Getting Started guide

This is the 16th video in the Getting Started with Rails 7 series. In this video Mike refactors the code written in the last video into a module called Visible that extends ActiveSupport::Concern. This module becomes the new home for the status-related code for both articles and comments. The two models both include the Visible module, and we can eliminate the previously duplicated code.

This video covers:

  • 00:00:10 Introduction and recap of the last video
  • 00:01:49 Create the Visible module and add the archived? instance method to it and include in Article and Comment models
  • 00:04:07 Extend ActiveSupport::Concern and move the VALID_STATUSES constant to the module
  • 00:05:39 Move the validation to an included block in Visible
  • 00:07:49 Add tests for public_count concern class method
  • 00:09:39 Add public_count class method to module
  • 00:10:39 Add test for display of public articles to controller test, but make the count agree with the noun
  • 00:12:37 Incorporate code from the guide, but it still fails “Our blog has one articles and counting!”
  • 00:13:38 Use the pluralize helper to make the count and noun agree
  • 00:15:00 Use a test-driven approach to add status to the article and comment forms
  • 00:19:18 Test the changes in the application. Doesn’t work as intended. Restart the server and it works.
  • 00:23:35 Review, commit, and push code changes

The code for this series is open source and available on GitHub. View the commit for this video here.


Leave a Reply