Skip to content

Instantly share code, notes, and snippets.

@keikun17
Created February 24, 2015 17:26
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 keikun17/66730eef92b351e96beb to your computer and use it in GitHub Desktop.
Save keikun17/66730eef92b351e96beb to your computer and use it in GitHub Desktop.
decorator
# from https://github.com/keikun17/yapos/blob/master/app/decorators/decorator.rb
require 'delegate'
class Decorator < SimpleDelegator
include ActionView::Helpers
include ActionView::Context
include Rails.application.routes.url_helpers
def self.decorate_collection(arr)
decorated_collection = []
arr.each do |item|
decorated_collection << self.new(item)
end
decorated_collection
end
def decorated_object
__getobj__
end
end
@rstacruz
Copy link

Example usage, I think:

class UserDecorator < Decorator
  def full_name
    "#{first_name} #{last_name}"
  end
end
<!-- views -->
<% user = UserDecorator.new(@user) %>
<%= user.first_name %> <!-- "John" -->
<%= user.last_name %>  <!-- "Smith" -->
<%= user.full_name %>  <!-- "John Smith" -->

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