Created
June 28, 2012 01:34
-
-
Save factore/3008146 to your computer and use it in GitHub Desktop.
This file contains 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 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