Last active
December 23, 2015 00:09
-
-
Save warmwaffles/6551814 to your computer and use it in GitHub Desktop.
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 ReportJSONPresenter | |
attr_reader :report | |
def initialize(report) | |
@report = report | |
end | |
def to_h | |
{ | |
name: report.name, | |
template: report.query_template | |
} | |
end | |
def to_json | |
to_h.to_json | |
end | |
end | |
class ReportsJSONPresenter | |
attr_reader :collection | |
def intializer(collection) | |
@collection = collection | |
end | |
def to_a | |
array = [] | |
collection.each do |item| | |
array << ReportJSONPresenter.new(item) | |
end | |
array | |
end | |
def to_json | |
array.to_json | |
end | |
end | |
class ReportsController < ApplicationController | |
def index | |
reports = Report.all | |
render json: ReportsJSONPresenter.new(reports) | |
end | |
def show | |
report = Report.find(params[:id]) | |
render json: ReportSerializer.new(report).to_json | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment