Class: Locomotive::BasePresenter
- Inherits:
-
Object
- Object
- Locomotive::BasePresenter
- 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
Direct Known Subclasses
AccountPresenter, ContentAssetPresenter, ContentEntryPresenter, ContentFieldPresenter, ContentTypePresenter, EditableElementPresenter, MembershipPresenter, PagePresenter, SitePresenter, SnippetPresenter, ThemeAssetPresenter, TranslationPresenter
Instance Attribute Summary collapse
-
#__ability ⇒ Object
readonly
utility accessors ##.
-
#__depth ⇒ Object
readonly
utility accessors ##.
Class Method Summary collapse
- .getters_or_setters_to_hash(name) ⇒ Object
-
.getters_to_hash ⇒ Hash
Return the set of getters with their options.
-
.setters_to_hash(options = {}) ⇒ Hash
Return the set of setters with their options.
Instance Method Summary collapse
-
#_id ⇒ String
(also: #id)
Get the id of the source only if it has been persisted or if it’s an embedded document.
-
#ability? ⇒ Boolean
Check if there is an ability object used for permissions.
-
#after_initialize ⇒ Object
Set the ability object to check if we can or not get a property.
-
#errors ⇒ Hash
Shortcut to the errors of the source.
-
#formatted_time(text) ⇒ Time
Mimic format used by to_json.
-
#html_view? ⇒ Boolean
Tell if the presenter is aimed to be used in a html view.
-
#include_errors? ⇒ Boolean
Tell if we have to include errors or not.
-
#site ⇒ Object
Shortcut to the site taken from the source.
-
#true ⇒ Object
default properties ##.
Methods included from Presentable
#as_json, #attributes=, #getters, #initialize, #property_options, #setters
Instance Attribute Details
#__ability ⇒ Object (readonly)
utility accessors ##
17 18 19 |
# File 'app/presenters/locomotive/base_presenter.rb', line 17 def __ability @__ability end |
#__depth ⇒ Object (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) = self.[name] || {} attributes = { type: [:type] || 'String', required: [:required].nil? ? true : [:required], alias_of: self.alias_of(name), description: [:description] } if [:presenter] attributes[:collection_of] = { name: [:presenter].to_s.demodulize.gsub(/Presenter$/, '').underscore, params: [:presenter].setters_to_hash } end attributes end |
.getters_to_hash ⇒ Hash
Return the set of getters with their options.
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.
100 101 102 103 104 105 106 107 108 109 |
# File 'app/presenters/locomotive/base_presenter.rb', line 100 def self.setters_to_hash( = {}) = { without_ids: true }.merge() {}.tap do |hash| self.setters.each do |name| next if %w(id _id).include?(name.to_s) && [:without_ids] hash[name] = self.getters_or_setters_to_hash(name) end end end |
Instance Method Details
#_id ⇒ String Also known as: id
Get the id of the source only if it has been persisted or if it’s an embedded document.
36 37 38 |
# File 'app/presenters/locomotive/base_presenter.rb', line 36 def _id self.__source.persisted? || self.__source. ? self.__source._id.to_s : nil end |
#ability? ⇒ Boolean
Check if there is an ability object used for permissions.
46 47 48 |
# File 'app/presenters/locomotive/base_presenter.rb', line 46 def ability? self.__ability.present? end |
#after_initialize ⇒ Object
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.[:depth] || 0 @__ability = self.[:ability] if self.[:current_account] && self.[:current_site] @__ability = Locomotive::Ability.new self.[:current_account], self.[:current_site] end end |
#errors ⇒ Hash
Shortcut to the errors of the source
62 63 64 |
# File 'app/presenters/locomotive/base_presenter.rb', line 62 def errors self.__source.errors..to_hash.stringify_keys end |
#formatted_time(text) ⇒ Time
Mimic format used by to_json
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
78 79 80 |
# File 'app/presenters/locomotive/base_presenter.rb', line 78 def html_view? !!self.[:html_view] end |
#include_errors? ⇒ Boolean
Tell if we have to include errors or not
70 71 72 |
# File 'app/presenters/locomotive/base_presenter.rb', line 70 def include_errors? !!self.[:include_errors] end |
#site ⇒ Object
Shortcut to the site taken from the source.
54 55 56 |
# File 'app/presenters/locomotive/base_presenter.rb', line 54 def site self.__source.try(:site) end |
#true ⇒ Object
default properties ##
11 12 13 |
# File 'app/presenters/locomotive/base_presenter.rb', line 11 allow_nil: true do |presenter| presenter.properties :id, :_id end |