Class: Tramway::BaseDecorator

Overview

Provides decorate function for Tramway projects

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Decorators::AssociationClassMethods

association, associations

Methods included from Utils::Render

render

Methods included from Helpers::ComponentHelper

component

Methods included from Helpers::DecorateHelper

#tramway_decorate

Methods included from DuckTyping::ActiveRecordCompatibility

included

Methods included from Decorators::CollectionDecorators

collection?, decorate_collection

Constructor Details

#initialize(object) ⇒ BaseDecorator

Returns a new instance of BaseDecorator.



23
24
25
# File 'lib/tramway/base_decorator.rb', line 23

def initialize(object)
  @object = object
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object

:reek:ManualDispatch { enabled: false } because there is the idea to manual dispatch



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tramway/base_decorator.rb', line 71

def method_missing(method_name, *, &)
  url_helpers = Rails.application.routes.url_helpers

  if method_name.to_s.end_with?('_path', '_url')
    return url_helpers.public_send(method_name, *, &) if url_helpers.respond_to?(method_name)

    raise NoMethodError, "undefined method `#{method_name}` for #{self}" unless respond_to_missing?(method_name)

  end

  super
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



21
22
23
# File 'lib/tramway/base_decorator.rb', line 21

def object
  @object
end

Class Method Details

.decorate(object_or_array) ⇒ Object

:reek:NilCheck { enabled: false } because checking for nil is not a type-checking issue but business logic



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tramway/base_decorator.rb', line 32

def decorate(object_or_array)
  return if object_or_array.nil?

  if Tramway::Decorators::CollectionDecorators.collection?(object_or_array)
    Tramway::Decorators::CollectionDecorators.decorate_collection(
      collection: object_or_array,
      decorator: self
    )
  else
    new(object_or_array)
  end
end

.delegate_attributes(*args) ⇒ Object



45
46
47
48
49
# File 'lib/tramway/base_decorator.rb', line 45

def delegate_attributes(*args)
  args.each do |attribute|
    delegate attribute, to: :object
  end
end

.index_header_contentObject



55
56
57
# File 'lib/tramway/base_decorator.rb', line 55

def index_header_content
  nil
end

.list_attributesObject



51
52
53
# File 'lib/tramway/base_decorator.rb', line 51

def list_attributes
  []
end

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

:reek:BooleanParameter { enabled: false } because it’s a part of the duck-typing

Returns:

  • (Boolean)


85
86
87
# File 'lib/tramway/base_decorator.rb', line 85

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.end_with?('_path', '_url') || super
end

#show_pathObject



68
# File 'lib/tramway/base_decorator.rb', line 68

def show_path = nil

#to_partial_pathObject



62
63
64
65
66
# File 'lib/tramway/base_decorator.rb', line 62

def to_partial_path
  underscored_class_name = object.class.name.underscore

  "#{underscored_class_name.pluralize}/#{underscored_class_name}"
end