Sha256: 6e030fd5ba29c3403cef27548144a4b3304ef4a5172ed391232389e568259bda

Contents?: true

Size: 1.3 KB

Versions: 13

Compression:

Stored size: 1.3 KB

Contents

---
redirect_from: /docs/11-decorators.html
---

# Decorators

Active Admin allows you to use the decorator pattern to provide view-specific
versions of a resource. [Draper](https://github.com/drapergem/draper) is
recommended but not required.

## Example usage

```ruby
# app/models/post.rb
class Post < ActiveRecord::Base
  # has title, content, and image_url
end

# app/decorators/post_decorator.rb
class PostDecorator < Draper::Decorator
  delegate_all

  def image
    h.image_tag model.image_url
  end
end

# app/admin/post.rb
ActiveAdmin.register Post do
  decorate_with PostDecorator

  index do
    column :title
    column :image
    actions
  end
end
```

You can pass any decorator class as an argument to `decorate_with`
as long as it accepts the record to be decorated as a parameter in
the initializer, and responds to all the necessary methods.

```ruby
# app/decorators/post_decorator.rb
class PostDecorator
  attr_reader :post
  delegate_missing_to :post

  def initialize(post)
    @post = post
  end
end
```

## Forms

By default, ActiveAdmin does *not* decorate the resource used to render forms.
If you need ActiveAdmin to decorate the forms, you can pass `decorate: true` to the
form block.

```ruby
ActiveAdmin.register Post do
  decorate_with PostDecorator

  form decorate: true do |f|
    # ...
  end
end
```

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
activeadmin-2.13.1 docs/11-decorators.md
activeadmin-2.13.0 docs/11-decorators.md
activeadmin-2.12.0 docs/11-decorators.md
activeadmin-2.11.2 docs/11-decorators.md
activeadmin-2.11.1 docs/11-decorators.md
activeadmin-2.11.0 docs/11-decorators.md
activeadmin-2.10.1 docs/11-decorators.md
activeadmin-2.10.0 docs/11-decorators.md
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activeadmin-2.9.0/docs/11-decorators.md
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activeadmin-2.9.0/docs/11-decorators.md
activeadmin-2.9.0 docs/11-decorators.md
activeadmin-2.8.1 docs/11-decorators.md
activeadmin-2.8.0 docs/11-decorators.md