Skip to content

Instantly share code, notes, and snippets.

@jredville
Created September 7, 2012 20:48
Show Gist options
  • Save jredville/3669487 to your computer and use it in GitHub Desktop.
Save jredville/3669487 to your computer and use it in GitHub Desktop.
module Reportable
def as_json
{:meta => meta, :report => report}
end
end
class ReportPresenter
include Reportable
def self.column(name, options={})
end
def meta
#...prepare meta
end
def report
# prepare report based on columns
end
end
class SoftwareOverviewPresenter < ReportPresenter
column :application, :model => Applications, :column => :name
column :publisher, :from => :application, :path => "publisher.name"
column :used, :from => lambda {}
end
class OrganizationalUnitTagCloudPresenter
def as_json
#...
end
end
class CombinationPresenter
def initialize(*items)
@items = items
end
def as_json
@items.each_with_object({}) { |item, res| res.merge(item.as_json) }
end
end
# in sinatra app
get 'report' do
report = company.reports.software_overview(query)
ous = OrganizationalUnitTagCloudPresenter.new company.ous
app.combine(report, ous)
end
@gruntruk
Copy link

gruntruk commented Sep 7, 2012

I like the base presenter class for report. I'd nix the Reportable module given it's tightly dependent on the presenter directly though. I'm still undecided on the column stuff and worry that you may need some sort of setup that might make the column method a bit more complex than a PORO. That being said, having a base class to deal with sorting, pagination, and filtering makes perfect sense.

For the sinatra app portion, I like having the idea of a 1:1 mapping between the report identifier and a presenter... if that presenter wants to delegate to a presenter internally, it's free to do so (as in the case for OU) but having the sinatra be a very simple dispatcher where it just needs to resolve the report and hand it the request context seems better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment