banner

Ruby on Rails has long been admired for its convention-over-configuration approach, which enables rapid development and ease of use. However, as applications grow in size and complexity, code duplication becomes an issue that can lead to maintenance challenges, bugs, and technical debt. One of the best ways to mitigate this problem is by applying advanced design patterns that focus on maximizing code reusability.

In this blog post, we will dive into advanced patterns and techniques that you can adopt to create more reusable and maintainable code in Ruby on Rails. By understanding and applying these patterns, you’ll be able to develop systems that are easier to extend, modify, and scale.

1. Service Objects for Business Logic

One of the most common issues in Rails applications is placing business logic inside models or controllers, which leads to bloated classes and difficulty in testing. Service objects are a great solution for separating business logic from these layers and making it more reusable.

A service object is a plain Ruby object that encapsulates a specific business task, such as processing an order or sending an email. Here’s an example of a service object that handles the logic of creating a user profile:

class CreateUserProfile
def initialize(user)
@user = user
end

def call
ActiveRecord::Base.transaction do
@user.profile = Profile.create!(user: @user)
@user.save!
end
end
end

You can use this service object in your controller like so:

class UsersController < ApplicationController
def create
user = User.new(user_params)
if user.save
CreateUserProfile.new(user).call
redirect_to user_path(user)
else
render :new
end
end
end

By isolating the business logic in a service object, you improve code reusability, testability, and readability.

2. Concerns for Reusable Modules

Rails offers a feature called concerns, which allow you to extract common functionality into reusable modules. These concerns can be used across controllers, models, or other parts of the application. They are ideal for reducing duplication in large applications.

For example, if you have a set of methods that handle logging, you can create a concern like this:

module Loggable
extend ActiveSupport::Concern

included do
before_action :log_action
end

private

def log_action
Rails.logger.info "Action #{action_name} was called at #{Time.now}"
end
end

Then, include this concern in any controller:

class ProductsController < ApplicationController
include Loggable

def index
# Action logic
end
end

Using concerns in this way reduces redundancy and ensures that code remains dry, making it easy to add or modify functionality across different parts of the application.

3. Decorators for View Logic Separation

Rails views can quickly become cluttered with logic, especially when dealing with complex user interfaces. The decorator pattern allows you to separate view-related logic from the models, enhancing reusability and maintainability.

A decorator is a plain Ruby class that “decorates” a model, providing additional methods for views without modifying the original model. One common use case for decorators is to handle formatting or other view-related tasks that should not reside in the model.

To implement a decorator in Rails, you can use the draper gem:

class UserDecorator < Draper::Decorator
delegate_all

def formatted_name
"#{object.first_name} #{object.last_name}"
end
end

Then, in your view:

<%= @user.decorate.formatted_name %>

This keeps the view logic separated from the model, making your application cleaner and more maintainable.

4. Rails Engines for Modularization

If you want to create reusable components or plugins, Rails Engines are an excellent choice. An engine is essentially a miniature Rails application that can be embedded into your main application. It can contain its own models, controllers, views, migrations, and routes, providing a powerful way to build modular applications.

For example, if you need to add a feature like an authentication system across multiple applications, you could implement it as a Rails engine:

rails plugin new authentication --mountable

This generates a fully-contained authentication engine that can be reused across different Rails applications. By using engines, you can break up your codebase into smaller, more manageable pieces, and reuse those pieces across projects.

5. Policy Objects for Authorization Logic

Authorization logic can often become tangled with business logic in controllers or models. The policy object pattern helps separate concerns by encapsulating authorization logic into its own class. This keeps your code cleaner and ensures that authorization rules are easy to modify and test.

For example, if you need to check if a user can update a particular post, you could create a PostPolicy:

class PostPolicy
attr_reader :user, :post

def initialize(user, post)
@user = user
@post = post
end

def update?
user.admin? || user == post.author
end
end

In your controller, you can use the policy object to check if the user is authorized:

class PostsController < ApplicationController
def update
@post = Post.find(params[:id])
authorize @post

    # Proceed with the update logic
end
end

This pattern keeps your controllers lean and makes it easier to adjust authorization logic when necessary.

6. Custom Middleware for Reusable Application Logic

Middleware is a powerful feature of Rails that allows you to execute code between the request and response cycle. By creating custom middleware, you can reuse application logic across different parts of your application, such as authentication, logging, or request tracking.

For example, you could write a custom middleware to log the IP address of every incoming request:

class LogIpMiddleware
def initialize(app)
@app = app
end

def call(env)
Rails.logger.info "Request from IP: #{env['REMOTE_ADDR']}"
@app.call(env)
end
end

Then, register it in the config/application.rb file:

config.middleware.use "LogIpMiddleware"

This allows the middleware to be applied globally, ensuring that IP logging is available across your application.

Conclusion

Building reusable and maintainable code is essential for scaling Ruby on Rails applications efficiently. By applying advanced design patterns such as service objects, concerns, decorators, engines, and policy objects, you can significantly enhance code reusability and simplify future development. These patterns not only make your codebase cleaner but also allow you to implement new features faster and with fewer bugs.

By incorporating these techniques into your Rails applications, you’ll be equipped to handle complexity and grow your projects with ease. Embrace the power of design patterns, and transform your Rails applications into more robust, scalable systems.