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 ##



15
16
17
# File 'app/presenters/locomotive/base_presenter.rb', line 15

def ability
  @ability
end

#depthObject (readonly)

utility accessors ##



15
16
17
# File 'app/presenters/locomotive/base_presenter.rb', line 15

def depth
  @depth
end

Class Method Details

.getters_to_hashHash

Return the set of getters with their options.

Returns:

  • (Hash)

    The getters



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/presenters/locomotive/base_presenter.rb', line 82

def self.getters_to_hash
  {}.tap do |hash|
    self.getters.each do |name|
      next if %w(created_at updated_at).include?(name)

      options = self.property_options[name] || {}

      hash[name] = options[:type] || 'String'
    end

    %w(created_at updated_at).each do |name|
      options = self.property_options[name] || {}
      hash[name] = options[:type] || 'String'
    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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/presenters/locomotive/base_presenter.rb', line 60

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]

      options   = self.property_options[name] || {}

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

Instance Method Details

#ability?Boolean

Check if there is an ability object used for permissions.

Returns:

  • (Boolean)

    True if defined, false otherwise



42
43
44
# File 'app/presenters/locomotive/base_presenter.rb', line 42

def ability?
  self.ability.present?
end

#after_initializeObject

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



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

def after_initialize
  @depth    = self.options[:depth] || 0
  @ability  = self.options[:ability]

  if @options[:current_account] && @options[:current_site]
    @ability = Locomotive::Ability.new @options[:current_account], @options[:current_site]
  end
end

#idString

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.



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

property    :id, alias: :_id

#siteObject

Shortcut to the site taken from the source.

Parameters:

  • The (Object)

    site or nil if not found



50
51
52
# File 'app/presenters/locomotive/base_presenter.rb', line 50

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