Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

active_decorator

> 后端框架
Open source

ORM agnostic truly Object-Oriented view helper for Rails 4, 5, 6, 7, and 8

1.1K stars0 likes3 views
WebsiteGitHub

About

ORM agnostic truly Object-Oriented view helper for Rails 4, 5, 6, 7, and 8

ActiveDecorator

A simple and Rubyish view helper for Rails 4, Rails 5, Rails 6, Rails 7, and Rails 8. Keep your helpers and views Object-Oriented!

Features

  1. automatically mixes decorator module into corresponding model only when:
    1. passing a model or collection of models or an instance of ActiveRecord::Relation from controllers to views
    2. rendering partials with models (using :collection or :object or :locals explicitly or implicitly)
    3. fetching already decorated Active Record model object's association
  2. the decorator module runs in the model's context. So, you can directly call any attributes or methods in the decorator module
  3. since decorators are considered as sort of helpers, you can also call any ActionView's helper methods such as content_tag or link_to

Supported versions

  • Ruby 2.1.x, 2.2.x, 2.3.x, 2.4.x, 2.5.x, 2.6.x, 2.7.x, 3.0.x, 3.1.x, 3.2.x, 3.3.x, 3.4.x, 4.0.x, and 4.1 (trunk)

  • Rails 4.2.x, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0, 7.1, 7.2, 8.0, 8.1, and 8.2 (edge)

Supported ORMs

ActiveRecord, ActiveResource, and any kind of ORMs that uses Ruby Objects as model objects

Usage

  1. bundle 'active_decorator' gem
  2. create a decorator module for each AR model. For example, a decorator for a model User should be named UserDecorator. You can use the generator for doing this ( % rails g decorator user )
  3. Then it's all done. Without altering any single line of the existing code, the decorator modules will be automatically mixed into your models only in the view context.

Examples

Auto-decorating via render

  • Model
ruby
class Author 

Auto-decorating via AR model's associated objects

  • Models
ruby
class Author 

  
  

Using ActiveDecorator outside of Action View

Sometimes you may want to use decorators outside of Action View, for example, for background tasks for ActiveJob. For such use case, ActiveDecorator module provides run_with method that takes some kind of Action View and a block.

ruby
ActiveDecorator::ViewContext.run_with ApplicationController.new.view_context do
  ## perform some heavy jobs here
end

Testing

You can test a decorator using your favorite test framework by decorating the model instance with

ruby
ActiveDecorator::Decorator.instance.decorate(model_instance)

Considering an Organization model and its simple decorator:

ruby
module OrganizationDecorator
  def full_name
    "#{first_name} #{last_name}"
  end
end

An RSpec test would look like:

ruby
describe '#full_name' do
  it 'returns the full organization name' do
    organization = Organization.new(first_name: 'John', last_name: 'Doe')
    decorated_organization = ActiveDecorator::Decorator.instance.decorate(organization)

    expect(decorated_organization.full_name).to eq('John Doe')
  end
end

Configuring the decorator suffix

By default, ActiveDecorator searches a decorator module named target_class.name + "Decorator"

If you would like a different rule, you can configure in your initializer.

ruby
ActiveDecorator.configure do |config|
  config.decorator_suffix = 'Presenter'
end

Contributing to ActiveDecorator

  • Fork, fix, then send me a pull request.

Copyright

Copyright (c) 2011 Akira Matsuda. See MIT-LICENSE for further details.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubydecoratorsrailsruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架