almost 9 years ago
We use Decorators for cleaning up view logic and models of a rails application. So if we use decorators, models and views would be less complex. When we use decorator for a project we might have a 1:1 mapping between models and decorators which means all the presentation logic of a Blog would be in BlogDecorator.
Draper gem is used for this approach.
Draper adds an object-oriented layer of presentation logic to Rails application.
Add Draper to your Gemfile and run bundle install
Decorators resides in app/decorators directory and name of a decorator is mapped with the model they are decorating as shown in example below.
# app/decorators/blog_decorator.rb
So we have created a decorator "BlogDecorator". Now we are going to use this.
If you want to create a decorator for a controller by default, Run the generate command below.
And it will create the BlogDecorator for you along with the appropriate tests.
Draper supports RSpec, MiniTest::Rails, and Test::Unit.
0 Comment(s)