Last active
August 29, 2015 14:07
-
-
Save strukturedkaos/40ec150a2c288aa486d3 to your computer and use it in GitHub Desktop.
Student Presenter example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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