Class: HasHelpers::BasePresenter
- Inherits:
-
Object
- Object
- HasHelpers::BasePresenter
- Includes:
- Attributes
- Defined in:
- app/presenters/has_helpers/base_presenter.rb
Direct Known Subclasses
BarcodeBasePresenter, ItemPresenter, ItemsPresenter, Resource::ActionPresenter, Resource::CellPresenter, Resource::FormPresenter, Resource::InfoCollectionPresenter, Resource::ListPresenter, Resource::RowPresenter, Resource::TagPresenter, SearchPresenter, SearchPresenter::DetailListPresenter, SearchPresenter::DetailListPresenter::DetailItemPresenter, SearchPresenter::ResultPresenter
Defined Under Namespace
Modules: Routing
Instance Attribute Summary collapse
- #errors ⇒ Object
-
#wrapped_object ⇒ Object
Returns the value of attribute wrapped_object.
Class Method Summary collapse
-
.wrap(*args, **options) ⇒ Object
Accepts an object or collection of objects and returns an Array of presenter instances wrapping each object.
-
.wrap_with_options(collection, **options) ⇒ Object
Merges options with each element of the collection argument, and then calls .wrap passing the result.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #error_on(accessor_name, include_dot_keys: true) ⇒ Object
-
#initialize(*args) ⇒ BasePresenter
constructor
A new instance of BasePresenter.
Methods included from Attributes
Constructor Details
#initialize(*args) ⇒ BasePresenter
Returns a new instance of BasePresenter.
39 40 41 42 43 |
# File 'app/presenters/has_helpers/base_presenter.rb', line 39 def initialize(*args) attrs = args.last.is_a?(Hash) ? args.pop : {} self.wrapped_object = args.first super(attrs) # run Attributes initializer end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/presenters/has_helpers/base_presenter.rb', line 81 def method_missing(name, *args, &block) if (accessor_name = name.to_s[/\Ahas_error_on_(.*)\Z/, 1]) # Check for names such as `has_error_on_field_name` error_on(accessor_name).present? elsif (accessor_name = name.to_s[/\Ahas_(.*)\Z/, 1]) && respond_to?(accessor_name) # Check for names such as `has_some_method` and strip the leading `has_` portion. result = send(accessor_name) if result.is_a?(Enumerator) # Enumerable#present? does not return the expected value, e.g. `[].to_enum.present? #=> true` result.size && result.size > 0 else result.present? end else super end end |
Instance Attribute Details
#errors ⇒ Object
69 70 71 |
# File 'app/presenters/has_helpers/base_presenter.rb', line 69 def errors @errors || wrapped_object_errors || {} end |
#wrapped_object ⇒ Object
Returns the value of attribute wrapped_object.
16 17 18 |
# File 'app/presenters/has_helpers/base_presenter.rb', line 16 def wrapped_object @wrapped_object end |
Class Method Details
.wrap(*args, **options) ⇒ Object
Accepts an object or collection of objects and returns an Array of presenter instances wrapping each object.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/presenters/has_helpers/base_presenter.rb', line 21 def self.wrap(*args, **) if args.length == 1 && args[0].respond_to?(:map) args[0].map do |data| data_args = ::Array[data] = data_args.last.is_a?(Hash) ? data_args.pop : {} new(*data_args, ) end else [new(*args, **)] end end |
.wrap_with_options(collection, **options) ⇒ Object
Merges options with each element of the collection argument, and then calls .wrap passing the result
35 36 37 |
# File 'app/presenters/has_helpers/base_presenter.rb', line 35 def self.(collection, **) self.wrap(collection.lazy.map { |values| values.reverse_merge() }) end |
Instance Method Details
#==(other) ⇒ Object
45 46 47 48 49 |
# File 'app/presenters/has_helpers/base_presenter.rb', line 45 def ==(other) other.is_a?(self.class) && instance_variables.all? do |ivar| instance_variable_get(ivar) == other.instance_variable_get(ivar) end end |
#error_on(accessor_name, include_dot_keys: true) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/presenters/has_helpers/base_presenter.rb', line 51 def error_on(accessor_name, include_dot_keys: true) return if errors.blank? = if errors.respond_to?(:messages) errors. else errors end if include_dot_keys keys = .keys.grep(/\A#{Regexp.quote(accessor_name.to_s)}(\z|\.)/) keys.flat_map { |key| [key] } else [accessor_name.to_sym] || [accessor_name.to_s] end end |