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



37
38
39
40
41
42
43
# File 'lib/locomotive/mongoid/presenter.rb', line 37

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.



32
33
34
35
# File 'lib/locomotive/mongoid/presenter.rb', line 32

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

#to_presenter(options = {}) ⇒ Object

Get the presenter corresponding to the current document



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