• Stars
    star
    1,051
  • Rank 43,859 (Top 0.9 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 13 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

ActiveDecorator Build Status Code Climate

A simple and Rubyish view helper for Rails 4, Rails 5, Rails 6, and Rails 7. 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, and 3.3 (trunk)

  • Rails 4.2.x, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0, and 7.1 (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
class Author < ActiveRecord::Base
  # first_name:string last_name:string
end
  • Controller
class AuthorsController < ApplicationController
  def show(id)  # powered by action_args
    @author = Author.find id
  end
end
  • Decorator
module AuthorDecorator
  def full_name
    "#{first_name} #{last_name}"
  end
end
  • View
<%# @author here is auto-decorated in between the controller and the view %>
<p><%= @author.full_name %></p>

Auto-decorating via AR model's associated objects

  • Models
class Author < ActiveRecord::Base
  # name:string
  has_many :books
end

class Book < ActiveRecord::Base
  # title:string url:string
  belongs_to :author
end
  • Controller
class AuthorsController < ApplicationController
  def show(id)
    @author = Author.find id
  end
end
  • Decorator
module BookDecorator
  def link
    link_to title, url
  end
end
  • View
<p><%= @author.name %></p>
<ul>
<% @author.books.order(:id).each do |book| %>
  <%# `book` here is auto-decorated because @author is a decorated instance %>
  <li><%= book.link %></li>
<% end %>
</ul>

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.

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

ActiveDecorator::Decorator.instance.decorate(model_instance)

Considering an Organization model and its simple decorator:

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

An RSpec test would look like:

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.

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.

More Repositories

1

jb

A simple and fast JSON API template engine for Ruby on Rails
Ruby
1,235
star
2

traceroute

A Rake task gem that helps you find the unused routes and controller actions for your Rails 3+ app
Ruby
877
star
3

heavens_door

Capybara test scenario recorder for Rails
JavaScript
863
star
4

database_rewinder

minimalist's tiny and ultra-fast database cleaner
Ruby
807
star
5

stateful_enum

A very simple state machine plugin built on top of ActiveRecord::Enum
Ruby
606
star
6

kaminari_themes

HTML
354
star
7

erd

A Rails engine for drawing your app's ER diagram
Ruby
332
star
8

html5_validators

A gem/plugin for Rails 3, Rails 4, Rails 5, and Rails 6 that enables client-side validation using ActiveModel + HTML5 Form Validation
Ruby
304
star
9

i18n_generators

A pack of Rails generators gem plugin that generates Rails 3 and Rails 2 I18n locale files for almost every known locale.
Ruby
285
star
10

himl

HTML-based Indented Markup Language for Ruby
Ruby
235
star
11

still_life

Rails upgrade's best friend
Ruby
216
star
12

gem-src

Gem.post_install { `git clone gem_source src` }
Ruby
208
star
13

motorhead

A Rails Engine framework that helps safe and rapid feature prototyping
Ruby
181
star
14

nested_scaffold

Nested scaffold generator for Rails 4.2 and 5
Ruby
176
star
15

roundabout

A Rails Engine that generates a page transition diagram for your Rails app from request specs
Ruby
153
star
16

rfd

Ruby on Files & Directories
Ruby
152
star
17

routes_lazy_routes

A boot time booster for Ruby on Rails that defers loading the whole bloody routes so the app can spin up quickly 🤘
Ruby
141
star
18

string_template

A template engine for Rails, focusing on speed, using Ruby's String interpolation syntax
Ruby
125
star
19

kawaii_validation

An ActiveRecord extension that adds more kawaii validation syntax
Ruby
117
star
20

interactive_rspec

RSpec on IRB
Ruby
86
star
21

hocus_pocus

A magical isolated engine gem for Rails 3.1+
Ruby
80
star
22

ljax_rails

render :partial lazy-loader for Rails
Ruby
66
star
23

everywhere

Hash condition syntax for AR query everywhere!
Ruby
58
star
24

kaminari_example

A tutorial project for the basic and advanced usage of Kaminari paginator
Ruby
45
star
25

async_partial

Ruby
33
star
26

turbo_partial

Ruby
27
star
27

future_records

Ruby
25
star
28

lightweight_attributes

Ruby
24
star
29

more_optimized_resolver

Ruby
23
star
30

turbo_urls

Ruby
22
star
31

teriyaki

Automatically imports *_path definitions from config/routes.rb for acceptance testing
Ruby
22
star
32

kawaii_association

An ActiveRecord DSL extension that provides kawaii association syntax
Ruby
21
star
33

arel_ruby

ARel Ruby visitor
Ruby
20
star
34

activerecord-refinements

ActiveRecord + Ruby 2.0 refinements
Ruby
20
star
35

polymorphic_url_cache

Ruby
17
star
36

gem_i

A RubyGems plugin that explicitly aliases `gem i` to `gem install` to avoid ambiguity
Ruby
17
star
37

speed_king

Ruby
14
star
38

nested_layouts

The only fork of "nested_layouts" Rails plugin in Github that correctly bug fixed for Rails 2.3
Ruby
13
star
39

rspec-refinements

RSpec + Ruby 2.0 refinements
Ruby
10
star
40

arenai

Ruby
8
star
41

activecalendar

Rails 2.2.2 ready javascript calendar date renderer
JavaScript
8
star
42

bundler-squash

Ruby
8
star
43

rbenv-gem-shared

Ruby
8
star
44

tatsuzine

Live coded app at Rails勉強会@東京#59
Ruby
8
star
45

bot_for_ruby-lang

Ruby
7
star
46

factory_factory

a script that transfers existing AR models into factories for factory_girl
Shell
6
star
47

automagic

Ruby
6
star
48

snowman_meltdown

A simple middleware for Rails 3 to vanish _snowman parameter☃☃☃
Ruby
5
star
49

activesupport-refinements

Ruby
4
star
50

webdb073_tutorial

WEB+DB Press Vol. 73 特集2「詳解Rails 4」のチュートリアルのサンプルコード
Ruby
4
star
51

gitrockets

Ruby
3
star
52

rails3_hands_on

東京Ruby会議03のワークショップ「Rails 3ハンズオン」のサンプルアプリケーション
Ruby
3
star
53

prsnt

prsnt prttyp
3
star
54

git_commands

3
star
55

internationalization

Ruby
3
star
56

qwik

qwik
2
star
57

hfrails

hfrails
2
star
58

atode_yomu

A gem plugin that cleverly installs rdoc and ri for the latest versions of already installed gems
Ruby
2
star
59

gem-diet

Ruby
2
star
60

action_args_with_rbs

Ruby
1
star