Class: Tramway::Core::ApplicationDecorator
- Inherits:
-
Object
- Object
- Tramway::Core::ApplicationDecorator
- Includes:
- ActionView::Context, ActionView::Helpers, FontAwesome5::Rails::IconHelper, Concerns::AttributesDecoratorHelper, CopyToClipboardHelper
- Defined in:
- app/decorators/tramway/core/application_decorator.rb
Constant Summary collapse
- RESERVED_WORDS =
['fields'].freeze
Class Method Summary collapse
- .collections ⇒ Object
- .decorate(object_or_array) ⇒ Object
- .decorate_association(association_name, decorator: nil, as: nil) ⇒ Object
- .list_attributes ⇒ Object
- .model_class ⇒ Object
- .model_name ⇒ Object
- .show_associations ⇒ Object
- .show_attributes ⇒ Object
Instance Method Summary collapse
- #associations(associations_type) ⇒ Object
- #attributes ⇒ Object
-
#initialize(object) ⇒ ApplicationDecorator
constructor
A new instance of ApplicationDecorator.
- #link ⇒ Object
- #model ⇒ Object
- #name ⇒ Object
- #public_path ⇒ Object
- #title ⇒ Object
- #yes_no(boolean_attr) ⇒ Object
Methods included from CopyToClipboardHelper
Constructor Details
#initialize(object) ⇒ ApplicationDecorator
Returns a new instance of ApplicationDecorator.
11 12 13 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 11 def initialize(object) @object = object end |
Class Method Details
.collections ⇒ Object
25 26 27 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 25 def collections [:all] end |
.decorate(object_or_array) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 41 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 |
.decorate_association(association_name, decorator: nil, as: nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 52 def decorate_association(association_name, decorator: nil, as: nil) @@decorated_associations ||= [] @@decorated_associations << association_name define_method association_name do association = object.class.reflect_on_association(association_name) if association.nil? error = Tramway::Error.new(plugin: :core, method: :decorate_association, message: "Model #{object.class} does not have association named `#{association_name}`") raise error. end class_name = if association.polymorphic? object.send(association_name).class else unless association.[:class_name] error = Tramway::Error.new(plugin: :core, method: :decorate_association, message: "Please, specify `#{association_name}` association class_name in #{object.class} model. For example: `has_many :#{association_name}, class_name: '#{association_name.to_s.singularize.camelize}'`") raise error. end association.[:class_name] end decorator_class_name = decorator || "#{class_name.to_s.singularize}Decorator".constantize if association.class == ActiveRecord::Reflection::HasManyReflection return object.send(association_name).active.map do |association_object| decorator_class_name.decorate association_object end end if association.class == ActiveRecord::Reflection::BelongsToReflection return decorator_class_name.decorate object.send association_name end end define_method "#{association_name}_as" do as end end |
.list_attributes ⇒ Object
29 30 31 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 29 def list_attributes [] end |
.model_class ⇒ Object
87 88 89 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 87 def model_class to_s.sub(/Decorator$/, '').constantize end |
.model_name ⇒ Object
91 92 93 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 91 def model_name model_class.try(:model_name) end |
.show_associations ⇒ Object
37 38 39 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 37 def show_associations [] end |
.show_attributes ⇒ Object
33 34 35 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 33 def show_attributes [] end |
Instance Method Details
#associations(associations_type) ⇒ Object
118 119 120 121 122 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 118 def associations(associations_type) object.class.reflect_on_all_associations(associations_type).map do |association| association unless association.name == :audits end.compact end |
#attributes ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 128 def attributes object.attributes.reduce({}) do |hash, attribute| if attribute[0].in? RESERVED_WORDS error = Tramway::Error.new( plugin: :core, method: :attributes, message: "Method `#{attribute[0]}` is reserved word. Please, create or delegate method in #{self.class.name} with another name." ) raise error. end value = try(attribute[0]) ? send(attribute[0]) : object.send(attribute[0]) if attribute[0].to_s.in? object.class.state_machines.keys.map(&:to_s) hash.merge! attribute[0] => state_machine_view(object, attribute[0]) elsif value.class.in? [ActiveSupport::TimeWithZone, DateTime, Time] hash.merge! attribute[0] => datetime_view(attribute[1]) elsif value.class.superclass == ApplicationUploader hash.merge! attribute[0] => image_view(object.send(attribute[0])) elsif value.is_a? Enumerize::Value hash.merge! attribute[0] => enumerize_view(value) else hash.merge! attribute[0] => value end end end |
#link ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 99 def link if object.try :file object.file.url else error = Tramway::Error.new( plugin: :core, method: :link, message: "Method `link` uses `file` attribute of the decorated object. If decorated object doesn't contain `file`, you shouldn't use `link` method." ) raise error. end end |
#model ⇒ Object
114 115 116 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 114 def model object end |
#name ⇒ Object
15 16 17 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 15 def name object.try(:name) || object.try(:title) || title end |
#public_path ⇒ Object
112 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 112 def public_path; end |
#title ⇒ Object
19 20 21 22 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 19 def title error = Tramway::Error.new(plugin: :core, method: :title, message: "Please, implement `title` method in a #{self.class} or #{object.class}") raise error. end |
#yes_no(boolean_attr) ⇒ Object
153 154 155 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 153 def yes_no(boolean_attr) boolean_attr.to_s == 'true' ? I18n.t('helpers.yes') : I18n.t('helpers.no') end |