Class: Tramway::Core::ApplicationDecorator

Inherits:
Object
  • Object
show all
Extended by:
Tramway::Core::Associations::ClassHelper, 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

.collectionsObject



53
54
55
# File 'app/decorators/tramway/core/application_decorator.rb', line 53

def collections
  [:all]
end

.decorate(object_or_array) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'app/decorators/tramway/core/application_decorator.rb', line 69

def decorate(object_or_array)
  if object_or_array.class.superclass == ActiveRecord::Relation || object_or_array.class.to_s.include?('ActiveRecord_AssociationRelation')
    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

.list_attributesObject



57
58
59
# File 'app/decorators/tramway/core/application_decorator.rb', line 57

def list_attributes
  []
end

.list_filtersObject



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

def list_filters
  {}
end

.model_classObject



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

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

.model_nameObject



84
85
86
# File 'app/decorators/tramway/core/application_decorator.rb', line 84

def model_name
  model_class.try(:model_name)
end

.show_associationsObject



65
66
67
# File 'app/decorators/tramway/core/application_decorator.rb', line 65

def show_associations
  []
end

.show_attributesObject



61
62
63
# File 'app/decorators/tramway/core/application_decorator.rb', line 61

def show_attributes
  []
end

Instance Method Details

#additional_buttonsObject



101
102
103
104
105
106
# File 'app/decorators/tramway/core/application_decorator.rb', line 101

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

#associations(associations_type) ⇒ Object



114
115
116
117
118
# File 'app/decorators/tramway/core/application_decorator.rb', line 114

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

#attributesObject



124
125
126
127
128
129
130
131
132
133
134
# File 'app/decorators/tramway/core/application_decorator.rb', line 124

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


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

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



42
43
44
# File 'app/decorators/tramway/core/application_decorator.rb', line 42

def listed_state_machines
  nil
end

#modelObject



110
111
112
# File 'app/decorators/tramway/core/application_decorator.rb', line 110

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

#public_pathObject



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

def public_path; end

#render(view, **locals) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'app/decorators/tramway/core/application_decorator.rb', line 31

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

#yes_no(boolean_attr) ⇒ Object



136
137
138
# File 'app/decorators/tramway/core/application_decorator.rb', line 136

def yes_no(boolean_attr)
  boolean_attr.to_s == 'true' ? I18n.t('helpers.yes') : I18n.t('helpers.no')
end