Class: Tramway::Core::ApplicationDecorator
- Inherits:
-
Object
- Object
- Tramway::Core::ApplicationDecorator
- Includes:
- ActionView::Context, ActionView::Helpers, FontAwesome::Rails::IconHelper
- Defined in:
- app/decorators/tramway/core/application_decorator.rb
Class Method Summary collapse
- .decorate(object_or_array) ⇒ Object
- .decorate_association(association_name, decorator: nil) ⇒ Object
- .list_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
- #title ⇒ Object
Constructor Details
#initialize(object) ⇒ ApplicationDecorator
Returns a new instance of ApplicationDecorator.
8 9 10 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 8 def initialize(object) @object = object end |
Class Method Details
.decorate(object_or_array) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 26 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) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 37 def decorate_association(association_name, decorator: nil) @@decorated_associations ||= [] @@decorated_associations << association_name define_method association_name do class_name = object.class.reflect_on_association(association_name).[:class_name] unless 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 object.send(association_name).active.map do |association_object| decorator_class_name = decorator || "#{class_name.singularize}Decorator".constantize decorator_class_name.decorate association_object end end end |
.list_attributes ⇒ Object
22 23 24 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 22 def list_attributes [] end |
Instance Method Details
#associations(associations_type) ⇒ Object
76 77 78 79 80 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 76 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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 82 def attributes object.attributes.reduce({}) do |hash, attribute| 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] => object.send("human_#{attribute[0]}_name") elsif value.class.in? [ ActiveSupport::TimeWithZone, DateTime, Time ] hash.merge! attribute[0] => I18n.l(attribute[1]) elsif value.class.superclass == ApplicationUploader = content_tag(:div) do if value.url.match(/jpg|JPG|png|PNG$/) concat image_tag value.try(:small) ? value.small.url : value.url end concat link_to(fa_icon(:download), value.url, class: 'btn btn-success') end hash.merge! attribute[0] => elsif value.is_a? Enumerize::Value hash.merge! attribute[0] => value.text else hash.merge! attribute[0] => value end end end |
#link ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 60 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
72 73 74 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 72 def model object end |
#name ⇒ Object
12 13 14 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 12 def name object.try(:name) || object.try(:title) || title end |
#title ⇒ Object
16 17 18 19 |
# File 'app/decorators/tramway/core/application_decorator.rb', line 16 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 |