Created
August 7, 2017 20:29
-
-
Save philsturgeon/bfabe7461ec6faf9457b6a75f25c0d9c to your computer and use it in GitHub Desktop.
The most basic-ass presenter ever
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 Api::V3::CompanyPresenter | |
attr_accessor :company, :includes | |
def initialize(company, includes: []) | |
@company = company | |
@includes = Array(includes) | |
end | |
def as_json | |
{ | |
uuid: company.uuid, | |
name: company.name, | |
status: company.status, | |
}.tap do |hash| | |
# Don't do big slow stuff if you don't need to | |
hash[:foo] = company.foo_is_slow if (includes & ['foo', 'full']).any? | |
hash[:bar] = company.bar_is_big if (includes & ['bar', 'full']).any? | |
if includes.include? 'full' | |
hash[:bla] = company.bla | |
# all the other shit... including slow stuff | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment