DEV Community

Afaq Shahid Khan
Afaq Shahid Khan

Posted on

Simplifying Rails Applications with Concerns

Hello Dev Community!

Today, I need to proportion a few insights on the usage of Concerns in Ruby on Rails to hold your codebase easy, modular, and maintainable. So let's dive into concerns..

What are Concerns?

In Rails, Concerns are modules that assist you to encapsulate reusable portions of code and consist of them in models, controllers, or different lessons. They assist in separating business logic and promoting the DRY (Don't Repeat Yourself) principle through heading off code duplication.

Why Use Concerns?

  1. Modularity: Break down large fashions and controllers into smaller, conceivable modules.
  2. Reusability: Share commonplace functionality across specific elements of your application.
  3. Maintainability: Make your codebase less complicated to examine, understand, and keep.

How to Create and Use Concerns

Let's walk via a easy example to look how we are able to create and use Concerns in a Rails utility.

Step 1: Create a Concern

First, we're going to create a Concern for a common functionality. Suppose we want to feature a technique to print a greeting message in special fashions.

# app/models/concerns/greetable.rb
module Greetable
  extend ActiveSupport::Concern

  def greet
    "Hello, #{name}!"
  end
end
Enter fullscreen mode Exit fullscreen mode
Step 2: Include the Concern in a Model

Next, we will consist of the Greetable Concern in a model. Let's say we've got a User model:

# app/models/user.rb
class User < ApplicationRecord
  include Greetable

  # Other model code...
end
Enter fullscreen mode Exit fullscreen mode
Step 3: Using the Concern in Your Application

You can now use the greet technique within the User model:

# Create a new user
user = User.create(name: "Afaq")

# Print a greeting message
puts user.greet
# Output: Hello, Afaq!
Enter fullscreen mode Exit fullscreen mode

Another Example: Adding Timestamps

Let's create every other simple Concern to add timestamps for while a report is viewed:

# app/models/concerns/viewable.rb
module Viewable
  extend ActiveSupport::Concern

  included do
    before_save :set_viewed_at
  end

  def view
    self.viewed_at = Time.current
    save
  end

  private

  def set_viewed_at
    self.viewed_at ||= Time.current
  end
end
Enter fullscreen mode Exit fullscreen mode
Using Viewable in a Model
# app/models/article.rb
class Article < ApplicationRecord
  include Viewable

  # Other model code...
end
Enter fullscreen mode Exit fullscreen mode
Using the Concern in Your Application
# Create a new article
article = Article.create(title: "Rails Concerns")

# Mark the article as viewed
article.view

# Check the viewed_at timestamp
puts article.viewed_at
Enter fullscreen mode Exit fullscreen mode

Best Practices for Using Concerns

  • Single Responsibility: Each Concern need to encapsulate a unmarried piece of functionality.
  • Avoid Overloading: Don’t overload Concerns with too many obligations or strategies.
  • Naming: Use clear and descriptive names to your Concerns to mirror their motive.
  • Testing: Write exams for every Concern to ensure their functionality is working as anticipated.

Conclusion

Concerns are a terrific way to keep your Rails utility modular, clean, and maintainable. By extracting common functionality into Concerns, you can lessen code duplication and beautify the readability of your code. I wish this manual has given you a very good evaluate of the way to use Concerns successfully in your Rails projects.

Feel free to reach out when you have any questions or in case you'd want to share how you use Concerns for your Rails applications. Happy coding!


Thank you for studying, and live tuned for greater Rails tips and hints!

Concerns #ROR #RailsDevelopment #Rails #TechInsights

Top comments (0)