Class: ActiveDecorator::Decorator

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/active_decorator/decorator.rb

Instance Method Summary collapse

Constructor Details

#initializeDecorator

Returns a new instance of Decorator.



8
9
10
# File 'lib/active_decorator/decorator.rb', line 8

def initialize
  @@decorators = {}
end

Instance Method Details

#decorate(obj) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_decorator/decorator.rb', line 12

def decorate(obj)
  return if defined?(Jbuilder) && Jbuilder === obj
  return if obj.nil?

  if obj.is_a?(Array)
    obj.each do |r|
      decorate r
    end
  elsif defined?(ActiveRecord) && obj.is_a?(ActiveRecord::Relation) && !obj.is_a?(ActiveDecorator::RelationDecorator)
    # don't call each nor to_a immediately
    obj.extend ActiveDecorator::RelationDecorator
  else
    d = decorator_for obj.class
    return obj unless d
    obj.extend d unless obj.is_a? d
  end
end