Skip to content

Instantly share code, notes, and snippets.

@factore
Created June 28, 2012 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save factore/3008146 to your computer and use it in GitHub Desktop.
Save factore/3008146 to your computer and use it in GitHub Desktop.
class ListProfileDecorator < Decorator
include ::Rails.application.routes.url_helpers
include ::ActionDispatch::Routing::PolymorphicRoutes
json_methods :tiny_photo, :path
def tiny_photo
@component.photo.tiny.url
end
def path
polymorphic_path(@component)
end
end
class Decorator < BasicObject
# .. snip ..
def self.json_methods(*args)
@@json_methods = args.to_a
end
def as_json(options = {})
json = @component.as_json(options).merge(::Hash[@@json_methods.map { |k| [k, self.send(k)] }])
if @component.class.try(:include_root_in_json)
{@component.class.to_s.underscore => json}
else
json
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment