module Livery::Controller::ClassMethods

Public Instance Methods

use_presenters!() click to toggle source

Overrides +AbstractController#view_assigns+ to return presenters set with the present method.

When you call this method in a controller class, instance variables set in actions will no longer be shared with views.

Examples

# app/controllers/posts_controller.rb
class PostController < ApplicationController
  include Livery::Controler
  use_presenters!

  def show
    @post = Post.find(params[:id])
    present(post_presenter: @post)
  end
end

In this example, the instance variable +@post+ will not be shared with the view. Instead, the present method will create a presenter-wrapped object available as +@post_presenter+ in the view.

# File lib/livery/controller.rb, line 31
def use_presenters!
  define_method "view_assigns" do
    @_presenters || {}
  end
end