Skip to content

Instantly share code, notes, and snippets.

@sighmin
Created September 29, 2015 19:51
Show Gist options
  • Save sighmin/34837102b3421dbc286f to your computer and use it in GitHub Desktop.
Save sighmin/34837102b3421dbc286f to your computer and use it in GitHub Desktop.
def decorate(object, decorater = nil)
if object.is_a? Array
return [] if object.empty?
decorater_class = decorater || (object[0].class.name.split("::").last + "View").constantize
object.map { |o| decorater_class.new(o) }
else
decorater_class = decorater || (object.class.name + "View").constantize
decorater_class.new(object)
end
end
user = decorate(current_user)
users = decorate(User.popular(10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment