Class: Locomotive::BasePresenter

Inherits:
Object
  • Object
show all
Extended by:
ActionView::Helpers::SanitizeHelper::ClassMethods
Includes:
ActionView::Helpers::NumberHelper, ActionView::Helpers::SanitizeHelper, ActionView::Helpers::TextHelper, Presentable
Defined in:
app/presenters/locomotive/base_presenter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Presentable

#as_json, #attributes=, #getters, #initialize, #property_options, #setters

Instance Attribute Details

#__abilityObject (readonly)

utility accessors ##



17
18
19
# File 'app/presenters/locomotive/base_presenter.rb', line 17

def __ability
  @__ability
end

#__depthObject (readonly)

utility accessors ##



17
18
19
# File 'app/presenters/locomotive/base_presenter.rb', line 17

def __depth
  @__depth
end

Class Method Details

.getters_or_setters_to_hash(name) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/presenters/locomotive/base_presenter.rb', line 128

def self.getters_or_setters_to_hash(name)
  options   = self.property_options[name] || {}

  attributes = {
    type:         options[:type] || 'String',
    required:     options[:required].nil? ? true : options[:required],
    alias_of:     self.alias_of(name),
    description:  options[:description]
  }

  if options[:presenter]
    attributes[:collection_of] = {
      name:   options[:presenter].to_s.demodulize.gsub(/Presenter$/, '').underscore,
      params: options[:presenter].setters_to_hash
    }
  end

  attributes
end

.getters_to_hashHash

Return the set of getters with their options.

Returns:

  • (Hash)

    The getters



115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/presenters/locomotive/base_presenter.rb', line 115

def self.getters_to_hash
  {}.tap do |hash|
    self.getters.each do |name|
      next if %w(_id created_at updated_at).include?(name)
      hash[name] = self.getters_or_setters_to_hash(name)
    end

    %w(created_at updated_at).each do |name|
      hash[name] = self.getters_or_setters_to_hash(name)
    end
  end
end

.setters_to_hash(options = {}) ⇒ Hash

Return the set of setters with their options.

Parameters:

  • options (Hash) (defaults to: {})

    Some options: with_ids (add id and _id)

Returns:

  • (Hash)

    The setters



100
101
102
103
104
105
106
107
108
109
# File 'app/presenters/locomotive/base_presenter.rb', line 100

def self.setters_to_hash(options = {})
  options = { without_ids: true }.merge(options)

  {}.tap do |hash|
    self.setters.each do |name|
      next if %w(id _id).include?(name.to_s) && options[:without_ids]
      hash[name] = self.getters_or_setters_to_hash(name)
    end
  end
end

Instance Method Details

#_idString Also known as: id

Get the id of the source only if it has been persisted or if it’s an embedded document.

Returns:

  • (String)

    The id of the source, nil if not persisted or not embedded.



36
37
38
# File 'app/presenters/locomotive/base_presenter.rb', line 36

def _id
  self.__source.persisted? || self.__source.embedded? ? self.__source._id.to_s : nil
end

#ability?Boolean

Check if there is an ability object used for permissions.

Returns:

  • (Boolean)

    True if defined, false otherwise



46
47
48
# File 'app/presenters/locomotive/base_presenter.rb', line 46

def ability?
  self.__ability.present?
end

#after_initializeObject

Set the ability object to check if we can or not get a property.



22
23
24
25
26
27
28
29
# File 'app/presenters/locomotive/base_presenter.rb', line 22

def after_initialize
  @__depth    = self.__options[:depth] || 0
  @__ability  = self.__options[:ability]

  if self.__options[:current_account] && self.__options[:current_site]
    @__ability = Locomotive::Ability.new self.__options[:current_account], self.__options[:current_site]
  end
end

#errorsHash

Shortcut to the errors of the source

Returns:

  • (Hash)

    The errors or an empty hash if no errors



62
63
64
# File 'app/presenters/locomotive/base_presenter.rb', line 62

def errors
  self.__source.errors.messages.to_hash.stringify_keys
end

#formatted_time(text) ⇒ Time

Mimic format used by to_json

Parameters:

  • text (String)

    The string representing the time

Returns:

  • (Time)

    The time or nil if the input is nil or empty



88
89
90
91
92
# File 'app/presenters/locomotive/base_presenter.rb', line 88

def formatted_time(text)
  return nil if text.blank?
  format = '%Y-%m-%dT%H:%M:%S%Z'
  ::Time.strptime(text, format)
end

#html_view?Boolean

Tell if the presenter is aimed to be used in a html view

Returns:

  • (Boolean)

    True if used in a HTML view



78
79
80
# File 'app/presenters/locomotive/base_presenter.rb', line 78

def html_view?
  !!self.__options[:html_view]
end

#include_errors?Boolean

Tell if we have to include errors or not

Returns:

  • (Boolean)

    True if the source has errors.



70
71
72
# File 'app/presenters/locomotive/base_presenter.rb', line 70

def include_errors?
  !!self.__options[:include_errors]
end

#siteObject

Shortcut to the site taken from the source.

Returns:

  • (Object)

    The site or nil if not found



54
55
56
# File 'app/presenters/locomotive/base_presenter.rb', line 54

def site
  self.__source.try(:site)
end

#trueObject

default properties ##



11
12
13
# File 'app/presenters/locomotive/base_presenter.rb', line 11

with_options allow_nil: true do |presenter|
  presenter.properties    :id, :_id
end