Module: Tramway::Decorators::ClassHelper

Included in:
ApplicationHelper
Defined in:
lib/tramway/decorators/class_helper.rb

Overview

Provides method to determine decorators classes

Class Method Summary collapse

Class Method Details

.decorator_class(object_or_array, decorator = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/tramway/decorators/class_helper.rb', line 9

def decorator_class(object_or_array, decorator = nil)
  if decorator.present?
    decorator
  else
    begin
      class_name = decorator_class_name(object_or_array)
      class_name.constantize
    rescue NameError
      raise NameError, "You should define #{class_name} decorator class."
    end
  end
end

.decorator_class_name(object_or_array) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/tramway/decorators/class_helper.rb', line 22

def decorator_class_name(object_or_array)
  klass = if Tramway::Decorators::CollectionDecorators.collection?(object_or_array)
            object_or_array.first.class
          else
            object_or_array.class
          end

  Tramway::Decorators::NameBuilder.default_decorator_class_name(klass)
end