Class: Enki::DeletePostUndo

Inherits:
UndoItem
  • Object
show all
Defined in:
app/models/enki/delete_post_undo.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_undo(post) ⇒ Object



31
32
33
# File 'app/models/enki/delete_post_undo.rb', line 31

def create_undo(post)
  DeletePostUndo.create!(:data => {:post => post.attributes, :comments => Enki.config.comments? && post.comments.collect(&:attributes)}.to_yaml)
end

Instance Method Details

#complete_descriptionObject



26
27
28
# File 'app/models/enki/delete_post_undo.rb', line 26

def complete_description
  "Recreated post '#{loaded_data[:post]["title"]}'"
end

#descriptionObject



22
23
24
# File 'app/models/enki/delete_post_undo.rb', line 22

def description
  "Deleted post '#{loaded_data[:post]["title"]}'"
end

#loaded_dataObject



18
19
20
# File 'app/models/enki/delete_post_undo.rb', line 18

def loaded_data
  @loaded_data ||= YAML.load(data)
end

#process!Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/models/enki/delete_post_undo.rb', line 3

def process!
  post_attributes = loaded_data[:post]
  raise('Post already exists') if Post.find_by_id(post_attributes.delete('id').to_i)

  post = nil
  transaction do
    post = Post.create!(post_attributes)
    loaded_data[:comments].each do |comment|
      post.comments.create!(comment.except('id', 'post_id'))
    end
    self.destroy
  end
  post
end