Class: BasePresenter

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
app/presenters/base_presenter.rb

Overview

Base presenter class with the basic pattern implementation
initialize with a model instance the rails view context

Instance Attribute Summary collapse

Delegated Instance Attributes collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, view = nil, options = nil) ⇒ BasePresenter

Returns a new instance of BasePresenter.

Parameters:

  • model (Object)

    the wrapped model instance

  • view (Object, nil) (defaults to: nil)

    view context (defaults to
    ApplicationController.helpers)

  • options (Hash, nil) (defaults to: nil)

Options Hash (options):



16
17
18
19
20
21
22
23
24
25
# File 'app/presenters/base_presenter.rb', line 16

def initialize(model, view = nil, options = nil)
  options ||= {}
  @model = model
  @view = view
  @view ||= ApplicationController.helpers
  @url_helper ||= @view || Rails.application.routes.url_helpers
  @options = options
  @current_account = options[:current_account]
  super(@model)
end

Instance Attribute Details

#current_accountObject (readonly)

Returns the value of attribute current_account.



6
7
8
# File 'app/presenters/base_presenter.rb', line 6

def 
  @current_account
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'app/presenters/base_presenter.rb', line 6

def options
  @options
end

#url_helperObject (readonly)

Returns the value of attribute url_helper.



6
7
8
# File 'app/presenters/base_presenter.rb', line 6

def url_helper
  @url_helper
end

Class Method Details

.presents(name) ⇒ void

This method returns an undefined value.

Defines a named accessor returning the wrapped model.

Parameters:

  • name (Symbol, String)

    accessor name (e.g. :product_line)



30
31
32
33
34
# File 'app/presenters/base_presenter.rb', line 30

def self.presents(name)
  define_method(name) do
    @model
  end
end

Instance Method Details

#can?Object

Alias for H#can?

Returns:

  • (Object)

    H#can?

See Also:



8
# File 'app/presenters/base_presenter.rb', line 8

delegate :can?, :fa_icon, :link_to, :content_tag, :concat, :capture, :number_to_currency, :simple_format, to: :h

#captureObject

Alias for H#capture

Returns:

  • (Object)

    H#capture

See Also:



8
# File 'app/presenters/base_presenter.rb', line 8

delegate :can?, :fa_icon, :link_to, :content_tag, :concat, :capture, :number_to_currency, :simple_format, to: :h

#concatObject

Alias for H#concat

Returns:

  • (Object)

    H#concat

See Also:



8
# File 'app/presenters/base_presenter.rb', line 8

delegate :can?, :fa_icon, :link_to, :content_tag, :concat, :capture, :number_to_currency, :simple_format, to: :h

#content_tagObject

Alias for H#content_tag

Returns:

  • (Object)

    H#content_tag

See Also:



8
# File 'app/presenters/base_presenter.rb', line 8

delegate :can?, :fa_icon, :link_to, :content_tag, :concat, :capture, :number_to_currency, :simple_format, to: :h

#fa_iconObject

Alias for H#fa_icon

Returns:

  • (Object)

    H#fa_icon

See Also:



8
# File 'app/presenters/base_presenter.rb', line 8

delegate :can?, :fa_icon, :link_to, :content_tag, :concat, :capture, :number_to_currency, :simple_format, to: :h

#hObject

The view context / helpers proxy.

Returns:

  • (Object)

    the view context



44
45
46
# File 'app/presenters/base_presenter.rb', line 44

def h
  @view
end

Alias for H#link_to

Returns:

  • (Object)

    H#link_to

See Also:



8
# File 'app/presenters/base_presenter.rb', line 8

delegate :can?, :fa_icon, :link_to, :content_tag, :concat, :capture, :number_to_currency, :simple_format, to: :h

#number_to_currencyObject

Alias for H#number_to_currency

Returns:

  • (Object)

    H#number_to_currency

See Also:



8
# File 'app/presenters/base_presenter.rb', line 8

delegate :can?, :fa_icon, :link_to, :content_tag, :concat, :capture, :number_to_currency, :simple_format, to: :h

#present(attribute_name) ⇒ Object?

Helper method which will first see if model responds to this method
otherwise will simply return nil

Parameters:

  • attribute_name (Symbol, String)

    attribute or method name

Returns:

  • (Object, nil)

    the attribute value, or nil when undefined



58
59
60
61
62
63
# File 'app/presenters/base_presenter.rb', line 58

def present(attribute_name)
  attribute_key = attribute_name.to_sym
  return unless respond_to?(attribute_key) || r.respond_to?(attribute_key)

  send(attribute_key)
end

#rObject

The wrapped model instance (short alias).

Returns:

  • (Object)

    the wrapped model



38
39
40
# File 'app/presenters/base_presenter.rb', line 38

def r
  @model
end

#safe_present(attribute_name) ⇒ Object?

Like #present but swallows errors and returns nil.

Parameters:

  • attribute_name (Symbol, String)

    attribute or method name

Returns:

  • (Object, nil)

    the attribute value, or nil on error



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

def safe_present(attribute_name)
  present(attribute_name)
rescue StandardError
  nil
end

#simple_formatObject

Alias for H#simple_format

Returns:

  • (Object)

    H#simple_format

See Also:



8
# File 'app/presenters/base_presenter.rb', line 8

delegate :can?, :fa_icon, :link_to, :content_tag, :concat, :capture, :number_to_currency, :simple_format, to: :h

#uObject

The URL helper for route generation.

Returns:

  • (Object)

    the URL helper



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

def u
  @url_helper
end