ORM agnostic truly Object-Oriented view helper for Rails 4, 5, 6, 7, and 8
ORM agnostic truly Object-Oriented view helper for Rails 4, 5, 6, 7, and 8
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!
:collection or :object or :locals explicitly or implicitly)content_tag or link_toRuby 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)
ActiveRecord, ActiveResource, and any kind of ORMs that uses Ruby Objects as model objects
User should be named UserDecorator.
You can use the generator for doing this ( % rails g decorator user )renderclass Author
class Author
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
endYou 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
endAn 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
endBy 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'
endCopyright (c) 2011 Akira Matsuda. See MIT-LICENSE for further details.
No open issues yet, or sync has not completed.