Class: Tramway::BaseDecorator

Overview

Provides decorate function for Tramway projects

Constant Summary

Constants included from Helpers::ViewsHelper

Helpers::ViewsHelper::FORM_SIZES

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::ViewsHelper

#tramway_back_button, #tramway_badge, #tramway_button, #tramway_cell, #tramway_chat, #tramway_container, #tramway_flash, #tramway_form_for, #tramway_header, #tramway_main_container, #tramway_row, #tramway_table, #tramway_title

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.



25
26
27
# File 'lib/tramway/base_decorator.rb', line 25

def initialize(object)
  @object = object
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/tramway/base_decorator.rb', line 99

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.



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

def object
  @object
end

#view_contextObject (readonly)

Returns the value of attribute view_context.



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

def view_context
  @view_context
end

Class Method Details

.decorate(object_or_array) ⇒ Object



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

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



52
53
54
55
56
# File 'lib/tramway/base_decorator.rb', line 52

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

.index_attributesObject



58
59
60
# File 'lib/tramway/base_decorator.rb', line 58

def index_attributes
  []
end

.index_header_contentObject



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

def index_header_content
  nil
end

.table_headers(entity: nil, model_class: nil) ⇒ Object



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

def table_headers(entity: nil, model_class: nil)
  headers = index_attributes.map do |attribute|
    (model_class || entity.model_class).human_attribute_name(attribute)
  end

  headers += ['Actions'] if entity&.page(:update).present? || entity&.page(:destroy).present?

  headers
end

Instance Method Details

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

Returns:

  • (Boolean)


112
113
114
# File 'lib/tramway/base_decorator.rb', line 112

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

#show_associationsObject



91
92
93
# File 'lib/tramway/base_decorator.rb', line 91

def show_associations
  []
end

#show_attributesObject



87
88
89
# File 'lib/tramway/base_decorator.rb', line 87

def show_attributes
  []
end

#show_header_contentObject



95
96
97
# File 'lib/tramway/base_decorator.rb', line 95

def show_header_content
  nil
end

#show_pathObject



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

def show_path = nil

#to_partial_pathObject



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

def to_partial_path
  underscored_class_name = object.class.name.underscore

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

#with(view_context:) ⇒ Object



29
30
31
32
33
# File 'lib/tramway/base_decorator.rb', line 29

def with(view_context:)
  @view_context = view_context

  self
end