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



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/tramway/base_decorator.rb', line 87

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

#view_contextObject (readonly)

Returns the value of attribute view_context.



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

def view_context
  @view_context
end

Class Method Details

.decorate(object_or_array) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tramway/base_decorator.rb', line 37

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



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

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

.index_attributesObject



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

def index_attributes
  []
end

.index_header_contentObject



60
61
62
# File 'lib/tramway/base_decorator.rb', line 60

def index_header_content
  nil
end

Instance Method Details

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

Returns:

  • (Boolean)


100
101
102
# File 'lib/tramway/base_decorator.rb', line 100

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

#show_associationsObject



79
80
81
# File 'lib/tramway/base_decorator.rb', line 79

def show_associations
  []
end

#show_attributesObject



75
76
77
# File 'lib/tramway/base_decorator.rb', line 75

def show_attributes
  []
end

#show_header_contentObject



83
84
85
# File 'lib/tramway/base_decorator.rb', line 83

def show_header_content
  nil
end

#show_pathObject



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

def show_path = nil

#table_headersObject



104
105
106
107
108
# File 'lib/tramway/base_decorator.rb', line 104

def table_headers
  self.class.index_attributes.map do |attribute|
    object.class.human_attribute_name(attribute)
  end
end

#to_partial_pathObject



67
68
69
70
71
# File 'lib/tramway/base_decorator.rb', line 67

def to_partial_path
  underscored_class_name = object.class.name.underscore

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

#with(view_context:) ⇒ Object



27
28
29
30
31
# File 'lib/tramway/base_decorator.rb', line 27

def with(view_context:)
  @view_context = view_context

  self
end