Class: HasHelpers::BasePresenter

Inherits:
Object
  • Object
show all
Includes:
Attributes
Defined in:
app/presenters/has_helpers/base_presenter.rb

Defined Under Namespace

Modules: Routing

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes

included

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

#errorsObject



69
70
71
# File 'app/presenters/has_helpers/base_presenter.rb', line 69

def errors
  @errors || wrapped_object_errors || {}
end

#wrapped_objectObject

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, **options)
  if args.length == 1 && args[0].respond_to?(:map)
    args[0].map do |data|
      data_args = ::Array[data]
      options = data_args.last.is_a?(Hash) ? data_args.pop : {}
      new(*data_args, options)
    end
  else
    [new(*args, **options)]
  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.wrap_with_options(collection, **options)
  self.wrap(collection.lazy.map { |values| values.reverse_merge(options) })
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?

  messages =
    if errors.respond_to?(:messages)
      errors.messages
    else
      errors
    end

  if include_dot_keys
    keys = messages.keys.grep(/\A#{Regexp.quote(accessor_name.to_s)}(\z|\.)/)
    keys.flat_map { |key| messages[key] }
  else
    messages[accessor_name.to_sym] || messages[accessor_name.to_s]
  end
end