Module: Adorn::Helper

Defined in:
lib/adorn/helpers.rb

Instance Method Summary collapse

Instance Method Details

#presenting(object, klass = nil, options = {}) ⇒ Adorn::Decorator

A helper method to present objects with an implicit or explicilty set Adorn::Decorator Accepts single or multiple objects and Decorators for presentation.

ex: Single Object, dynamic presenter loading presenting @bar do |bar_presenter|

bar_presenter will respond to methods from the BarPresenter

end

ex: Single Object, explicit Decorator Class

presenting @bar, BazPresenter do |bar_presenter|

bar_presenter will respond to methods from the BazPresenter

end

ex: Multiple Objects NOTE: implicit detection only

presenting([@foo, @bar]) do |foo_presenter, bar_presenter|

@foo will respond to methods from the FooPresenter
@bar will respond to methods from the BarPresenter

end

Parameters:

  • (Object, Class, Hash)

Returns:

  • (Adorn::Decorator)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/adorn/helpers.rb', line 33

def presenting(object, klass=nil, options={})
  if object.is_a?(Array)
    object.each do |presentable|

      proxy = "#{presentable.class}Presenter"
      yield Object.const_get(proxy.to_s).new(object, self, options) if Object.const_defined?(proxy.to_s)

      provided_klass = Object.const_get(proxy.to_s, false)
      presenter      = provided_klass.new(presentable, self, options) # self is context
      yield presenter
      proxy = nil
    end
    nil
  else
    proxy = klass || "#{object.class}Presenter"
    yield Object.const_get(proxy.to_s).new(object, self, options) if Object.const_defined?(proxy.to_s)

    provided_klass = Object.const_get(proxy.to_s, false)
    presenter = provided_klass.new(object, self, options) # self is context
    yield presenter
    proxy = nil
  end
end