* 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

@@ -34,7 +34,7 @@ class DateTag
end
def next_week
DateTag.new_from_range(1.week.since(start_date)), 1.week.since(end_date)
DateTag.new_from_range(1.week.since(start_date), 1.week.since(end_date))
end
def previous_month
@@ -42,7 +42,7 @@ class DateTag
end
def next_month
DateTag.new_from_range(1.month.since(start_date)), 1.month.since(end_date)
DateTag.new_from_range(1.month.since(start_date), 1.month.since(end_date))
end
def date

View File

@@ -1,7 +1,7 @@
class Post < ActiveRecord::Base
attr_accessor :old_tag_string, :old_parent_id
after_destroy :delete_files
after_save :create_version
after_save :update_history
after_save :update_parent_on_save
before_save :merge_old_tags
before_save :normalize_tags
@@ -15,7 +15,7 @@ class Post < ActiveRecord::Base
has_one :unapproval, :dependent => :destroy
has_one :upload, :dependent => :destroy
has_one :moderation_detail, :class_name => "PostModerationDetail", :dependent => :destroy
has_many :versions, :class_name => "PostVersion", :dependent => :destroy
has_one :history, :class_name => "PostHistory"
has_many :votes, :class_name => "PostVote", :dependent => :destroy
has_many :notes, :dependent => :destroy
has_many :comments
@@ -233,24 +233,21 @@ class Post < ActiveRecord::Base
end
end
module VersionMethods
def create_version
version = versions.create(
:source => source,
:rating => rating,
:tag_string => tag_string,
:updater_id => CurrentUser.user.id,
:updater_ip_addr => CurrentUser.ip_addr
)
raise PostVersion::Error.new(version.errors.full_messages.join("; ")) if version.errors.any?
module HistoryMethods
def revisions
if history.nil?
update_history
end
history.revisions
end
def revert_to!(version)
self.source = version.source
self.rating = version.rating
self.tag_string = version.tag_string
save!
def update_history
if history.nil?
create_history
end
history << self
end
end
@@ -788,7 +785,7 @@ class Post < ActiveRecord::Base
include ImageMethods
include ApprovalMethods
include PresenterMethods
include VersionMethods
include HistoryMethods
include TagMethods
include FavoriteMethods
include UploaderMethods

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

View File

@@ -1,6 +0,0 @@
class PostVersion < ActiveRecord::Base
class Error < Exception ; end
validates_presence_of :updater_id, :updater_ip_addr
belongs_to :post
end

View File

@@ -1,5 +1,5 @@
<div id="search-form" style="margin-bottom: 1em;">
<% form_tag(artists_path, :method => :get) do %>
<%= form_tag(artists_path, :method => :get) do %>
<%= text_field_tag "name", params[:name], :size => 40 %>
<%= submit_tag "Search" %>
<% end %>