Class: Tramway::BaseDecorator
- Inherits:
-
Object
- Object
- Tramway::BaseDecorator
- Includes:
- Decorators::CollectionDecorators, DuckTyping::ActiveRecordCompatibility, Helpers::ComponentHelper, Helpers::DecorateHelper, Utils::Render
- Defined in:
- lib/tramway/base_decorator.rb
Overview
Provides decorate function for Tramway projects
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Class Method Summary collapse
-
.decorate(object_or_array) ⇒ Object
:reek:NilCheck { enabled: false } because checking for nil is not a type-checking issue but business logic.
- .delegate_attributes(*args) ⇒ Object
- .index_header_content ⇒ Object
- .list_attributes ⇒ Object
Instance Method Summary collapse
-
#initialize(object) ⇒ BaseDecorator
constructor
A new instance of BaseDecorator.
-
#method_missing(method_name) ⇒ Object
:reek:ManualDispatch { enabled: false } because there is the idea to manual dispatch.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
:reek:BooleanParameter { enabled: false } because it’s a part of the duck-typing.
- #show_path ⇒ Object
- #to_partial_path ⇒ Object
Methods included from Decorators::AssociationClassMethods
Methods included from Utils::Render
Methods included from Helpers::ComponentHelper
Methods included from Helpers::DecorateHelper
Methods included from DuckTyping::ActiveRecordCompatibility
Methods included from Decorators::CollectionDecorators
collection?, decorate_collection
Constructor Details
#initialize(object) ⇒ BaseDecorator
Returns a new instance of BaseDecorator.
23 24 25 |
# File 'lib/tramway/base_decorator.rb', line 23 def initialize(object) @object = object end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name) ⇒ Object
:reek:ManualDispatch { enabled: false } because there is the idea to manual dispatch
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/tramway/base_decorator.rb', line 71 def method_missing(method_name, *, &) url_helpers = Rails.application.routes.url_helpers if method_name.to_s.end_with?('_path', '_url') return url_helpers.public_send(method_name, *, &) if url_helpers.respond_to?(method_name) raise NoMethodError, "undefined method `#{method_name}` for #{self}" unless respond_to_missing?(method_name) end super end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
21 22 23 |
# File 'lib/tramway/base_decorator.rb', line 21 def object @object end |
Class Method Details
.decorate(object_or_array) ⇒ Object
:reek:NilCheck { enabled: false } because checking for nil is not a type-checking issue but business logic
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/tramway/base_decorator.rb', line 32 def decorate(object_or_array) return if object_or_array.nil? if Tramway::Decorators::CollectionDecorators.collection?(object_or_array) Tramway::Decorators::CollectionDecorators.decorate_collection( collection: object_or_array, decorator: self ) else new(object_or_array) end end |
.delegate_attributes(*args) ⇒ Object
45 46 47 48 49 |
# File 'lib/tramway/base_decorator.rb', line 45 def delegate_attributes(*args) args.each do |attribute| delegate attribute, to: :object end end |
.index_header_content ⇒ Object
55 56 57 |
# File 'lib/tramway/base_decorator.rb', line 55 def index_header_content nil end |
.list_attributes ⇒ Object
51 52 53 |
# File 'lib/tramway/base_decorator.rb', line 51 def list_attributes [] end |
Instance Method Details
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
:reek:BooleanParameter { enabled: false } because it’s a part of the duck-typing
85 86 87 |
# File 'lib/tramway/base_decorator.rb', line 85 def respond_to_missing?(method_name, include_private = false) method_name.to_s.end_with?('_path', '_url') || super end |
#show_path ⇒ Object
68 |
# File 'lib/tramway/base_decorator.rb', line 68 def show_path = nil |
#to_partial_path ⇒ Object
62 63 64 65 66 |
# File 'lib/tramway/base_decorator.rb', line 62 def to_partial_path underscored_class_name = object.class.name.underscore "#{underscored_class_name.pluralize}/#{underscored_class_name}" end |