* Denormalized post versions. Testing has shown it reduces the size

of the table 66%.
This commit is contained in:
albert
2010-11-04 18:17:03 -04:00
parent d0d3487fc8
commit 551c25909c
9 changed files with 157 additions and 114 deletions

View File

@@ -0,0 +1,40 @@
class PostHistory < ActiveRecord::Base
class Error < Exception ; end
before_validation :initialize_revisions, :on => :create
belongs_to :post
def self.build_revision_for_post(post)
hash = {
:source => post.source,
:rating => post.rating,
:tag_string => post.tag_string,
:parent_id => post.parent_id,
:user_id => CurrentUser.id,
:ip_addr => CurrentUser.ip_addr,
:updated_at => revision_time
}
end
def self.revision_time
Time.now
end
def initialize_revisions
write_attribute(:revisions, "[]")
end
def revisions
if read_attribute(:revisions).blank?
[]
else
JSON.parse(read_attribute(:revisions))
end
end
def <<(post)
revision = self.class.build_revision_for_post(post)
write_attribute(:revisions, (revisions << revision).to_json)
save
end
end