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

.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



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

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

#attributesObject



97
98
99
100
101
102
103
104
105
106
107
# File 'app/decorators/tramway/core/application_decorator.rb', line 97

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


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

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



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

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



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

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



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

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