Class: Tramway::Core::ApplicationDecorator

Inherits:
Object
  • Object
show all
Extended by:
Tramway::Core::Associations::ClassHelper, Default::ValuesHelper, Delegating::ClassHelper
Includes:
ActionView::Context, ActionView::Helpers, FontAwesome5::Rails::IconHelper, Tramway::ClassNameHelpers, Tramway::Core::Associations::ObjectHelper, Tramway::Core::Attributes::ViewHelper, Concerns::AttributesDecoratorHelper, Concerns::TableBuilder, CopyToClipboardHelper
Defined in:
app/decorators/tramway/core/application_decorator.rb

Constant Summary collapse

RESERVED_WORDS =
['fields'].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tramway::ClassNameHelpers

#decorator_class_name, #form_class_name, #model_class_name

Methods included from CopyToClipboardHelper

#copy_to_clipboard

Constructor Details

#initialize(object) ⇒ ApplicationDecorator

Returns a new instance of ApplicationDecorator.



16
17
18
# File 'app/decorators/tramway/core/application_decorator.rb', line 16

def initialize(object)
  @object = object
end

Class Method Details

.decorate(object_or_array) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/decorators/tramway/core/application_decorator.rb', line 57

def decorate(object_or_array)
  is_activerecord_relation = object_or_array.class.superclass == ActiveRecord::Relation
  is_activerecord_association_relation = object_or_array.class.to_s.include?('ActiveRecord_AssociationRelation')
  if is_activerecord_relation || is_activerecord_association_relation
    decorated_array = object_or_array.map do |obj|
      new obj
    end
    Tramway::Core::ApplicationDecoratedCollection.new decorated_array, object_or_array
  else
    new object_or_array
  end
end

.model_classObject



70
71
72
# File 'app/decorators/tramway/core/application_decorator.rb', line 70

def model_class
  to_s.sub(/Decorator$/, '').constantize
end

.model_nameObject



74
75
76
# File 'app/decorators/tramway/core/application_decorator.rb', line 74

def model_name
  model_class.try(:model_name)
end

Instance Method Details

#additional_buttonsObject



87
88
89
# File 'app/decorators/tramway/core/application_decorator.rb', line 87

def additional_buttons
  { show: [], index: [] }
end

#associations(associations_type) ⇒ Object



97
98
99
100
101
# File 'app/decorators/tramway/core/application_decorator.rb', line 97

def associations(associations_type)
  object.class.reflect_on_all_associations(associations_type).map do |association|
    association unless association.name == :audits
  end.compact
end

#attributesObject



107
108
109
110
111
112
113
114
115
116
117
# File 'app/decorators/tramway/core/application_decorator.rb', line 107

def attributes
  object.attributes.reduce({}) do |hash, attribute|
    if attribute[0].in? RESERVED_WORDS
      Tramway::Error.raise_error(
        :tramway, :core, :application_decorator, :attributes, :method_is_reserved_word,
        attribute_name: attribute[0], class_name: self.class.name
      )
    end
    hash.merge! attribute[0] => build_viewable_value(object, attribute)
  end
end


79
80
81
82
83
84
85
# File 'app/decorators/tramway/core/application_decorator.rb', line 79

def link
  if object.try :file
    object.file.url
  else
    Tramway::Error.raise_error :tramway, :core, :application_decorator, :link, :method_link_uses_file_attribute
  end
end

#listed_state_machinesObject



46
47
48
# File 'app/decorators/tramway/core/application_decorator.rb', line 46

def listed_state_machines
  nil
end

#modelObject



93
94
95
# File 'app/decorators/tramway/core/application_decorator.rb', line 93

def model
  object
end

#nameObject



20
21
22
# File 'app/decorators/tramway/core/application_decorator.rb', line 20

def name
  object.try(:name) || object.try(:title) || title
end

#present?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'app/decorators/tramway/core/application_decorator.rb', line 31

def present?
  object.present?
end

#public_pathObject



91
# File 'app/decorators/tramway/core/application_decorator.rb', line 91

def public_path; end

#render(view, **locals) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'app/decorators/tramway/core/application_decorator.rb', line 35

def render(view, **locals)
  view_name = view.split('/').map.with_index do |dir, index|
    if index == view.split('/').length - 1
      "_#{dir}"
    else
      dir
    end
  end.join('/')
  Haml::Engine.new(File.read("#{Rails.root}/app/views/#{view_name}.html.haml")).render(self, locals)
end

#titleObject



24
25
26
27
28
29
# File 'app/decorators/tramway/core/application_decorator.rb', line 24

def title
  Tramway::Error.raise_error(
    :tramway, :core, :application_decorator, :title, :please_implement_title,
    class_name: self.class, object_class: object.class
  )
end