<h1>@post.title</h1>
<span>@post.publication_status</span>
class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
end
end
class Post < ActiveRecord::Base
def publication_status
published_at || 'Draft'
end
end
<h1>@post.title</h1>
<span>@post.publication_status</span>
class PostsController < ApplicationController
def show
post = Post.find(params[:id])
@post = PostPresenter.new(post)
end
end
class Post < ActiveRecord::Base
end
class PostPresenter
def initialze(post)
@post = post
end
def publication_status
@post.published_at || 'Draft'
end
end
<% present(@post) do |post| %>
<h1>post.title</h1>
<span>post.publication_status</span>
<% end %>
class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
end
end
class Post < ActiveRecord::Base
end
class PostPresenter
def initialze(post)
@post = post
end
def publication_status
@post.published_at || 'Draft'
end
end
module ApplicationHelper
def present(model)
klass = "#{model.class}Presenter".constantize
presenter = klass.new(model)
yield(presenter) if block_given?
end
end
Source : http://nithinbekal.com/posts/rails-presenters/