Skip to content

Instantly share code, notes, and snippets.

@strukturedkaos
Last active August 29, 2015 14:07
Show Gist options
  • Save strukturedkaos/40ec150a2c288aa486d3 to your computer and use it in GitHub Desktop.
Save strukturedkaos/40ec150a2c288aa486d3 to your computer and use it in GitHub Desktop.
Student Presenter example
# stored in app/presenters/student_presenter.rb
class StudentPresenter < SimpleDelegator
delegate :email, to: :user
def initialize(student)
@student = super(student)
end
def self.find(id)
new(Student.find(id))
end
def display_name
name.present? ? name : "-"
end
def display_university
university.present? ? university.name : "University not found"
end
end
class StudetntsController < ApplicationController
def index
@students = Student.all.map{|s| StudentPresenter.new(s)}
end
def show
@student = StudentPresenter.find(params[:id])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment