* Refactored post history. Each post now has a single history record. This history record has multiple revisions, serialized as JSON in a text field.
This commit is contained in:
@@ -1,5 +1,60 @@
|
||||
class PostHistory < ActiveRecord::Base
|
||||
class Error < Exception ; end
|
||||
|
||||
class Revision
|
||||
attr_accessor :prev, :hash, :diff, :tag_array
|
||||
|
||||
def initialize(hash)
|
||||
@hash = hash
|
||||
@diff = {}
|
||||
@tag_array = Tag.scan_tags(@hash["tag_string"])
|
||||
end
|
||||
|
||||
def calculate_diff
|
||||
if prev.nil?
|
||||
diff[:add] = tag_array
|
||||
diff[:del] = []
|
||||
diff[:rating] = rating
|
||||
diff[:source] = source
|
||||
diff[:parent_id] = parent_id
|
||||
else
|
||||
diff[:del] = prev.tag_array - tag_array
|
||||
diff[:add] = tag_array - prev.tag_array
|
||||
|
||||
if prev.rating != rating
|
||||
diff[:rating] = rating
|
||||
end
|
||||
|
||||
if prev.source != source
|
||||
diff[:source] = source
|
||||
end
|
||||
|
||||
if prev.parent_id != parent_id
|
||||
diff[:parent_id]= parent_id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def rating
|
||||
hash["rating"]
|
||||
end
|
||||
|
||||
def source
|
||||
hash["source"]
|
||||
end
|
||||
|
||||
def parent_id
|
||||
hash["parent_id"]
|
||||
end
|
||||
|
||||
def updated_at
|
||||
hash["updated_at"]
|
||||
end
|
||||
|
||||
def updater_id
|
||||
hash["user_id"]
|
||||
end
|
||||
end
|
||||
|
||||
before_validation :initialize_revisions, :on => :create
|
||||
belongs_to :post
|
||||
@@ -37,4 +92,19 @@ class PostHistory < ActiveRecord::Base
|
||||
write_attribute(:revisions, (revisions << revision).to_json)
|
||||
save
|
||||
end
|
||||
|
||||
def each_revision(&block)
|
||||
array = revisions.map {|x| Revision.new(x)}
|
||||
link_revisions(array)
|
||||
array.each {|x| x.calculate_diff}
|
||||
array.each(&block)
|
||||
end
|
||||
|
||||
private
|
||||
def link_revisions(array)
|
||||
1.upto(array.size - 1) do |i|
|
||||
array[i].prev = array[i - 1]
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user