Moves revision generation code to Revision class
| 97 | end | end | 97 |
|---|---|---|---|
| 98 | 98 | ||
| 99 | def revise(author, time, attrs, image = nil) | def revise(author, time, attrs, image = nil) | 99 |
| 100 | #TODO ugly! ugly! ugly! | latest_revision = revisions.last || NullRevision.new(self) | 100 |
| 101 | if !attrs[:happens_at] && attrs['happens_at(1i)'] && attrs['happens_at(2i)'] && attrs['happens_at(3i)'] | rev = latest_revision.next(author, time, attrs, image) | 101 |
| 102 | attrs[:happens_at] = | 102 | |
| 103 | Date.new(attrs['happens_at(1i)'].to_i, attrs['happens_at(2i)'].to_i, | self.kind, self.happens_at = rev.kind, rev.happens_at | 103 |
| 104 | attrs['happens_at(3i)'].to_i) | 104 | |
| 105 | end | ||
| 106 | unless revisions.size == 0 || original_author | unless revisions.size == 0 || original_author | 105 |
| 107 | revisions.first.last_editor = author | revisions.first.last_editor = rev.last_editor | 106 |
| 108 | revisions.first.save | revisions.first.save | 107 |
| 109 | end | end | 108 |
| 110 | rev = Revision.new(:last_editor => author, :modified_at => time, | ||
| 111 | :position => self.revisions.size + 1) | ||
| 112 | rev.editors = if author.can_change_editors?(self) | ||
| 113 | attrs[:editors] | ||
| 114 | else | ||
| 115 | revisions.last.editors | ||
| 116 | end | ||
| 117 | self.kind = attrs[:kind] if attrs[:kind] | ||
| 118 | self.happens_at = attrs[:happens_at] if attrs[:happens_at] | ||
| 119 | rev.kind, rev.happens_at = self.kind, self.happens_at | ||
| 120 | rev.title, rev.text, rev.done = attrs[:title], attrs[:text], attrs[:done] | ||
| 121 | rev.images = revisions.last.images if revisions.last | ||
| 122 | rev.images << image if image | ||
| 123 | 109 | ||
| 124 | update_references(rev.text) if rev.text | update_references(rev.text) if rev.text | 110 |
| 125 | self.revisions << rev | self.revisions << rev | 111 |
| 126 | 112 | ||
| 127 | save | save | 113 |
| 128 | self | self | 114 |
| 129 | end | end | 115 |
| 31 | other.text.split($/)) | other.text.split($/)) | 31 |
|---|---|---|---|
| 32 | end | end | 32 |
| 33 | 33 | ||
def next(author, time, attrs, image = nil) | 34 | ||
rev = self.clone | 35 | ||
rev.images = self.images | 36 | ||
| 37 | |||
attrs[:happens_at] ||= extract_event_date(attrs) | 38 | ||
| 39 | |||
rev.last_editor = author | 40 | ||
rev.modified_at = time | 41 | ||
rev.position = 1 + (self.position || 0) | 42 | ||
rev.editors = attrs[:editors] if author.can_change_editors?(page) | 43 | ||
rev.images << image unless image.nil? | 44 | ||
| 45 | |||
%w{kind happens_at title text done}.each do |k| | 46 | ||
rev.send("#{k}=", attrs[k.to_sym]) unless attrs[k.to_sym].nil? | 47 | ||
end | 48 | ||
| 49 | |||
rev | 50 | ||
end | 51 | ||
| 52 | |||
private | 53 | ||
| 54 | |||
def extract_event_date(attrs) | 55 | ||
if attrs['happens_at(1i)'] && attrs['happens_at(2i)'] && attrs['happens_at(3i)'] | 56 | ||
Date.new(attrs['happens_at(1i)'].to_i, attrs['happens_at(2i)'].to_i, | 57 | ||
attrs['happens_at(3i)'].to_i) | 58 | ||
end | 59 | ||
end | 60 | ||
end | 61 | ||
| 62 | |||
class NullRevision < Revision | 63 | ||
attr_reader :page | 64 | ||
| 65 | |||
def initialize(page) | 66 | ||
@page = page | 67 | ||
end | 68 | ||
| 69 | |||
def position | 70 | ||
0 | 71 | ||
end | 72 | ||
| 73 | |||
def images | 74 | ||
[] | 75 | ||
end | 76 | ||
| 77 | |||
def clone | 78 | ||
Revision.new(:editors => '', :kind => 'common', :done => false) | 79 | ||
end | 80 | ||
| 34 | end | end | 81 |
| 35 | 82 | ||
| 36 | class ChunkDiffer | class ChunkDiffer | 83 |