Module: Nelumba::EmbeddedObject

Included in:
Article, Comment, Image, Note
Defined in:
lib/nelumba-mongodb/embedded_object.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nelumba-mongodb/embedded_object.rb', line 3

def self.included(klass)
  klass.class_eval do
    include MongoMapper::EmbeddedDocument

    def initialize(*args, &blk)
      init(*args, &blk)

      super(*args, &blk)
    end

    belongs_to :author, :class_name => 'Nelumba::Person'
    key :author_id, ObjectId

    key :title
    key :uid
    key :url
    key :display_name
    key :summary
    key :content
    key :image

    # Automated Timestamps
    key :published, Time
    key :updated,   Time
    before_save :update_timestamps

    def update_timestamps
      now = Time.now.utc
      self[:published] ||= now if !persisted?
      self[:updated]     = now
    end

    def self.find_by_id(id)
      Nelumba::Activity.object_by_id_and_type(id, self)
    end

    embedded_in :'Nelumba::Activity'
  end
end