Module: Locomotive::Mongoid::Presenter

Extended by:
ActiveSupport::Concern
Included in:
CustomFields::Field
Defined in:
lib/locomotive/mongoid/presenter.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/locomotive/mongoid/presenter.rb', line 34

def as_json(options = {})
  if presenter = self.to_presenter(options)
    presenter.as_json
  else
    self.as_json_from_mongoid(options)
  end
end

#from_presenter(attributes = {}) ⇒ Object

Set the attributes of the document by using the corresponding presenter.

Parameters:

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

    The document attributes



30
31
32
# File 'lib/locomotive/mongoid/presenter.rb', line 30

def from_presenter(attributes = {})
  self.to_presenter.attributes = attributes
end

#to_presenter(options = {}) ⇒ Object

Get the presenter corresponding to the current document

Returns:

  • (Object)

    The presenter



20
21
22
23
# File 'lib/locomotive/mongoid/presenter.rb', line 20

def to_presenter(options = {})
  return nil unless self.class.presenter_class
  self.class.presenter_class.new(self, options)
end