Module: MongoMapper::Plugins::Draft

Extended by:
ActiveSupport::Concern
Includes:
Callbacks, Keys
Defined in:
lib/mongo_mapper/plugins/draft/keys.rb,
lib/mongo_mapper/plugins/draft/draft.rb,
lib/mongo_mapper/plugins/draft/callbacks.rb

Defined Under Namespace

Modules: Callbacks, Keys

Instance Method Summary collapse

Instance Method Details

#draft?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/mongo_mapper/plugins/draft/draft.rb', line 14

def draft?
  self.draft
end

#draft_recordObject



118
119
120
121
122
123
124
# File 'lib/mongo_mapper/plugins/draft/draft.rb', line 118

def draft_record
  if draft?
    self
  else
    self.class.all(:conditions => { :draft => true, :draft_record_published_id => self._id }).first
  end
end

#publishObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mongo_mapper/plugins/draft/draft.rb', line 28

def publish
  return false if (draft? == false) # don't publish non-drafts...

  if (self.changed?) # save any changes, in case publish is called directly instead of save
    return false if (self.save == false)
  end


  # if already published, update the record
  if (self.published?)
    # old_published_record = self.published_record
    # support for mm-tree
    # remove parents from previous published record
    # TREE Support disabled for now
    # if (self.respond_to?("parent_id_field") && self.respond_to?("path_field") && self.respond_to?("depth_field"))
    #   old_published_record.parent = nil
    #   old_published_record.save
    # end
    # destroy old published record... Not ideal, but should work
    # self.published_record.destroy if self.published_record != nil
    # live_record = self.clone
    # live_record.created_at = old_published_record.created_at if self.respond_to?("created_at")
    update_attribs = self.attributes.clone
    ["_id", "created_at", "updated_at"].each do |rmkey|
      update_attribs.delete(rmkey)
    end
    update_attribs["draft"] = false
    update_attribs["draft_record_published_id"] = nil
    live_record = self.published_record
    live_record.update_attributes!(update_attribs)
  else # clone the record

    live_record = self.clone
    if self.respond_to?("created_at")
      time_proc = lambda { Time.now.utc }
      live_record.created_at = time_proc.call
    end

    if self.respond_to?("published_at")
      time_proc = lambda { Time.now.utc }
      tmp_time = time_proc.call
      self.set(published_at: tmp_time)
      live_record.published_at = tmp_time
    end

    live_record.draft_record_published_id = nil
    live_record.draft = false
    live_record.save!
  end

  self.set(draft_record_published_id: live_record._id)
  self.reload

  # TREE Support disabled for now
  # if (self.respond_to?("parent_id_field") && self.respond_to?("path_field") && self.respond_to?("depth_field"))
  #   # if so, remove the current parent (should have already been don)
  #   # set parent to nil for live_record before setting "real" parent.
  #   # live_record.parent = nil

  #   if (self.parent != nil)
  #     # no need to copy order value, as it's copied in the clone process
  #     # check draft.parent.published_record != nil, and set as parent
  #     if (self.parent != nil)
  #       if (self.parent.published_record != nil)
  #         live_record.parent = self.parent.published_record
  #       end
  #     end
  #   end
  # end

  return true
end

#published?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
# File 'lib/mongo_mapper/plugins/draft/draft.rb', line 18

def published?
  if draft?
    return false if self.draft_record_published_id == nil # save a query and return false
    return true if (self.class.find(self.draft_record_published_id) != nil)
  else
    return true
  end
  false
end

#published_recordObject



101
102
103
104
105
106
107
108
# File 'lib/mongo_mapper/plugins/draft/draft.rb', line 101

def published_record
  if draft?
    return nil if self.draft_record_published_id == nil # save a query and return nil of draft_record_published_id == nil
    self.class.find(self.draft_record_published_id)
  else
    self
  end
end

#published_record_idObject



110
111
112
113
114
115
116
# File 'lib/mongo_mapper/plugins/draft/draft.rb', line 110

def published_record_id
  if draft?
    self.draft_record_published_id
  else
    self._id
  end
end

#unpublishObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/mongo_mapper/plugins/draft/draft.rb', line 126

def unpublish
  published_rec = self.published_record
  draft_rec     = self.draft_record

  self.class.skip_callback(:destroy, :before, :unpublish)
  published_rec.destroy if published_rec != nil # destroy published record
  self.class.set_callback(:destroy, :before, :unpublish)
  draft_rec.set(published_at: nil) if self.respond_to?("published_at")
  draft_rec.set(draft_record_published_id: nil)
end