forked from publify/publify
/
112_fixes_notes_redirects.rb
32 lines (29 loc) · 1.06 KB
/
112_fixes_notes_redirects.rb
1
2
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
class FixesNotesRedirects < ActiveRecord::Migration
def self.up
say "Fixes notes redirects, it may take some time"
notes = Note.find(:all)
notes.each do |note|
if note.redirects.size > 0
old_from = note.redirects.first.to_path
from = note.redirects.first.from_path
to = note.redirects.first.to_path.gsub(File.join(Blog.default.base_url, "st"), File.join(Blog.default.base_url, "note"))
redirect = note.redirects.first
redirect.update_attribute('to_path', to)
redirect.save!
Redirect.create(from_path: old_from, to_path: to)
end
end
end
def self.down
say "Fixes statuses redirects"
notes = Note.find(:all)
notes.each do |note|
if note.redirects.size > 0
redirect = Redirect.find.where(to_path: note.redirects.first.to_path)
redirect.destroy
note.redirects.first.to_path = note.redirects.first.to_path.gsub!(File.join(Blog.default.base_url, "note"), File.join(Blog.default.base_url, "st"))
note.redirects.first.save
end
end
end
end