Module: Resubject::Extensions::TemplateMethods

Included in:
Presenter
Defined in:
lib/resubject/extensions/template_methods.rb

Overview

All Rails extensions

Instance Method Summary collapse

Instance Method Details

#currency(attribute, options = {}) ⇒ Object

Generates an attribute using ‘number_to_currency` helper from rails

Examples:


class ProductPresenter < Resubject::Presenter
  currency :price
end

# Will create a `price` attribute using `number_to_currency`

product.price
# => '$10.00'

Also, any number_to_currency options are accepted


currency :price, precision: 3   # => '$123.456'
currency :price, locale: :fr    # => '123,51 €'

Parameters:

  • attribute (Symbol)

    the name of the presented attribute to be generated

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

    the options for ‘number_to_currency` method

See Also:



28
29
30
31
32
# File 'lib/resubject/extensions/template_methods.rb', line 28

def currency(attribute, options = {})
  define_method attribute do
    template.number_to_currency to_model.send(attribute), options
  end
end

#date_format(attribute, format = :default) ⇒ Object

Formats a date/time attribtue using ‘to_s` helper from ActiveSupport::TimeWithZone

Examples:


class PostPresenter < Resubject::Presenter
  date_format :created_at, :short
end

# Will generate a `created_at` method in the presenter

post.created_at
# => '13 Jan 14:10'

Other accepted formats


:db           # => 2008-12-25 14:35:05
:number       # => 20081225143505
:time         # => 14:35
:short        # => 25 Dec 14:35
:long         # => December 25, 2008 14:35
:long_ordinal # => December 25th, 2008 14:35
:rfc822       # => Thu, 25 Dec 2008 14:35:05 +0000

Parameters:

  • attribute (Symbol)

    the name of the presented attribute to be generated

  • format (Symbol) (defaults to: :default)

    the format defined in Time::DATE_FORMATS

See Also:



116
117
118
119
120
121
# File 'lib/resubject/extensions/template_methods.rb', line 116

def date_format(attribute, format = :default)
  define_method attribute do
    return if to_model.send(attribute).nil?
    to_model.send(attribute).to_s(format)
  end
end

#percentage(attribute, options = {}) ⇒ Object

Generates an attribute using ‘number_to_percentage` helper from rails

Examples:


class ProductPresenter < Resubject::Presenter
  percentage :rating
end

# Will create a `rating` attribute using `number_to_percentage`

product.rating
# => '95.000%'

Also, any number_to_percentage options are accepted


currency :rating, precision: 0   # => '95%'
currency :rating, locale: :fr    # => '1 000,000%'

Parameters:

  • attribute (Symbol)

    the name of the presented attribute to be generated

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

    the options for ‘number_to_percentage` method

See Also:



84
85
86
87
88
# File 'lib/resubject/extensions/template_methods.rb', line 84

def percentage(attribute, options = {})
  define_method attribute do
    template.number_to_percentage to_model.send(attribute), options
  end
end

#time_ago(attribute, include_seconds = false) ⇒ Object

Generates an attribute using ‘time_ago_in_words` helper from rails

Examples:


class ProductPresenter < Resubject::Presenter
  time_ago :posted_at
end

# Will redefine the `posted_at` attribute using `time_ago_in_words`

product.posted_at
# => 'about 1 hour'

Parameters:

  • attribute (Symbol)

    the name of the attribute to be generated

See Also:



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/resubject/extensions/template_methods.rb', line 49

def time_ago(attribute, include_seconds = false)
  if Gem::Version.new(ActiveSupport::VERSION::STRING) < Gem::Version.new('4')
    define_method attribute do
      return if to_model.send(attribute).nil?
      template.time_ago_in_words to_model.send(attribute), include_seconds
    end
  else
    define_method attribute do
      return if to_model.send(attribute).nil?
      template.time_ago_in_words to_model.send(attribute), include_seconds: include_seconds
    end
  end
end