Draper View Model Library

repository·master·Indexed 26 days ago

https://github.com/drapergem/draper

A view model library for Ruby on Rails that provides decorators to encapsulate presentation logic, keeping models and controllers clean. It supports Rails 5.2 / Ruby 2.4 and later, offering features such as automatic method delegation via `delegate_all`, access to Rails helpers through the `h` method, and the ability to decorate ActiveRecord models, Mongoid documents, and plain old Ruby objects (POROs).

Tokens
3.8K
Snippets
9
Records
34
Agent score
90%

What's inside Draper

  1. Install Draper via Gemfile

    master

    To use Draper in your Rails application, add the gem to your Gemfile. Note that as of version 4.0.0, Draper officially supports Rails 5.2 / Ruby 2.4 and later.

    1. Add gem 'draper' to your Gemfile.
    2. Run bundle install in your application directory.
    gem 'draper'
  2. Share decorator functionality using inheritance

    master

    Since decorators are Ruby objects, you can share common functionality by creating a base decorator class. Create an ApplicationDecorator that inherits from Draper::Decorator, and then have your specific decorators inherit from ApplicationDecorator instead of Draper::Decorator directly.

    # app/decorators/application_decorator.rb
    class ApplicationDecorator < Draper::Decorator
    end
    
    class ArticleDecorator < ApplicationDecorator
      # decorator methods
    end
  3. Create a Draper Decorator

    master

    Decorators inherit from Draper::Decorator and should be placed in the app/decorators directory. They are typically named after the model they decorate.

    For namespaced models like Admin::Catalogue, place the decorator in app/decorators/admin/catalogue_decorator.rb and define it as class Admin::CatalogueDecorator < Draper::Decorator.

    To create a decorator for an existing model, use the Draper generator:

    rails generate decorator Article
  4. Configure Draper for ActionController

    master
    To integrate Draper with your Rails controllers, use Draper.setup_action_controller(base). This method configures the controller to include Draper::ViewContext, extends it with Draper::HelperSupport and Draper::DecoratesAssigned, and adds a before_action to activate Draper. If the base class is ActionController::API, it also includes Draper::Compatibility::ApiOnly.
  5. Enable the .decorate method on plain Ruby objects

    master
    By default, Draper includes the Decoratable module in ActiveRecord::Base and Mongoid::Document. If you are using a different ORM or want to decorate Plain Old Ruby Objects (POROs), you must manually include Draper::Decoratable in your class to enable the .decorate shortcut.
  6. Decorate a model in a controller

    master

    To use a decorator, call the .decorate method on your model instance within your controller before passing it to the view.

    # app/controllers/articles_controller.rb
    def show
      @article = Article.find(params[:id]).decorate
    end
  7. Access the wrapped model in a decorator

    master

    To access the underlying model instance within a decorator method, use the object method or its alias model.

    class ArticleDecorator < Draper::Decorator
      def published_at
        object.published_at.strftime("%A, %B %e")
      end
    end
  8. Proxy class methods to the model

    master

    To proxy class methods from the model to the decorator, Draper typically infers the model name from the decorator name (e.g., CarDecorator -> Car). If the name cannot be inferred, use the decorates method to explicitly link the decorator to the model.

    Once linked, you can access class methods defined in the decorator via ModelName.decorator_class and check if a model is decorated using ModelName.decorator_class?.

  9. Make non-ActiveRecord objects decoratable

    master
    While ActiveRecord::Base and Mongoid::Document include Draper::Decoratable by default, you can manually include the Draper::Decoratable module in any plain old Ruby object (PORO) or other ORM models to enable the .decorate method.
  10. Pass extra data to decorators using context

    master

    You can pass additional data to a decorator using the context option when calling decorate. This data is accessible within the decorator via the context method.

    When using decorates_association, you can pass a static hash to the :context option, or a lambda to dynamically modify the parent decorator's context.