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, 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.



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

def initialize(object)
  @object = object
end

Class Method Details

.collectionsObject



37
38
39
# File 'app/decorators/tramway/core/application_decorator.rb', line 37

def collections
  [:all]
end

.decorate(object_or_array) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'app/decorators/tramway/core/application_decorator.rb', line 53

def decorate(object_or_array)
  if object_or_array.class.superclass == ActiveRecord::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

.list_attributesObject



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

def list_attributes
  []
end

.list_filtersObject



72
73
74
# File 'app/decorators/tramway/core/application_decorator.rb', line 72

def list_filters
  {}
end

.model_classObject



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

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

.model_nameObject



68
69
70
# File 'app/decorators/tramway/core/application_decorator.rb', line 68

def model_name
  model_class.try(:model_name)
end

.show_associationsObject



49
50
51
# File 'app/decorators/tramway/core/application_decorator.rb', line 49

def show_associations
  []
end

.show_attributesObject



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

def show_attributes
  []
end

Instance Method Details

#associations(associations_type) ⇒ Object



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

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

#attributesObject



101
102
103
104
105
106
107
108
109
110
111
# File 'app/decorators/tramway/core/application_decorator.rb', line 101

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


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

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

#modelObject



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

def model
  object
end

#nameObject



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

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

#public_pathObject



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

def public_path; end

#titleObject



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

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



113
114
115
# File 'app/decorators/tramway/core/application_decorator.rb', line 113

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