reverted post version tag change for danbooru2

This commit is contained in:
albert
2011-10-22 21:34:32 -04:00
parent 0de90d487b
commit 69044f3389
13 changed files with 100 additions and 61 deletions

View File

@@ -2,7 +2,7 @@ $link_color: #006FFA;
$link_hover_color: #9093FF;
$border_color: #CCC;
$highlight_color: #F0F0F0;
$reverse_highlight_color: #FFFDF6;
$reverse_highlight_color: #FFFDF4;
$h1_size: 2em;
$h2_size: 1.5em;
$h3_size: 1.16667em;

View File

@@ -35,3 +35,4 @@ table.striped {
background-color: #FAFAFA;
}
}

View File

@@ -27,7 +27,7 @@ module ApplicationHelper
zone = time.strftime("%z")
datetime = time.strftime("%Y-%m-%dT%H:%M" + zone[0, 3] + ":" + zone[3, 2])
content_tag(:time, content || datetime, :datetime => datetime)
content_tag(:time, content || datetime, :datetime => datetime, :title => time.to_formatted_s)
end
def compact_time(time)

View File

@@ -1,2 +1,16 @@
module PostVersionsHelper
def post_version_diff(post_version)
diff = post_version.diff(post_version.previous)
html = []
diff[:added_tags].each do |tag|
html << '<ins>' + tag + '</ins>'
end
diff[:removed_tags].each do |tag|
html << '<del>' + tag + '</del>'
end
diff[:unchanged_tags].each do |tag|
html << '<span>' + tag + '</span>' unless tag =~ /^(?:rating|source):/
end
return html.join(" ").html_safe
end
end

View File

@@ -934,40 +934,24 @@ class Post < ActiveRecord::Base
versions.create(
:rating => rating,
:source => source,
:add_tags => tag_string,
:tags => tag_string,
:parent_id => parent_id
)
elsif rating_changed? || source_changed? || parent_id_changed? || tag_string_changed?
versions.create(
:rating => rating_changed? ? rating : nil,
:source => source_changed? ? source : nil,
:add_tags => (tag_array - tag_array_was).join(" "),
:del_tags => (tag_array_was - tag_array).join(" "),
:parent_id => parent_id_changed? ? parent_id : nil
:rating => rating,
:source => source,
:tags => tag_string,
:parent_id => parent_id
)
end
end
def revert_to(target)
base_tags = []
base_rating = "q"
base_source = nil
base_parent_id = nil
versions.each do |version|
if version.id <= target.id
base_tags += version.add_tag_array
base_tags -= version.del_tag_array
base_rating = version.rating if version.rating
base_source = version.source if version.source
base_parent_id = version.parent_id if version.parent_id
end
end
self.tag_string = base_tags.sort.join(" ")
self.rating = base_rating
self.source = base_source
self.parent_id = base_parent_id
self.tag_string = target.tags
self.rating = target.rating
self.source = target.source
self.parent_id = target.parent_id
end
def revert_to!(target)

View File

@@ -17,12 +17,8 @@ class PostVersion < ActiveRecord::Base
self.updater_ip_addr = CurrentUser.ip_addr
end
def add_tag_array
@add_tag_array ||= add_tags.scan(/\S+/)
end
def del_tag_array
@del_tag_array ||= del_tags.scan(/\S+/)
def tag_array
@tag_array ||= tags.scan(/\S+/)
end
def presenter
@@ -30,8 +26,45 @@ class PostVersion < ActiveRecord::Base
end
def reload
@add_tag_array = nil
@del_tag_array = nil
@tag_array = nil
super
end
def sequence_for_post
versions = PostVersion.where(:post_id => post_id).order("id desc").all
diffs = []
versions.each_index do |i|
if i < versions.size - 1
diffs << versions[i].diff(versions[i + 1])
end
end
return diffs
end
def diff(version)
latest_tags = post.tag_array
new_tags = tag_array
new_tags << "rating:#{rating}" if rating.present?
new_tags << "parent:#{parent_id}" if parent_id.present?
new_tags << "source:#{source}" if source.present?
old_tags = version.present? ? version.tag_array : []
if version.present?
old_tags << "rating:#{version.rating}" if version.rating.present?
old_tags << "parent:#{version.parent_id}" if version.parent_id.present?
old_tags << "source:#{version.source}" if version.source.present?
end
return {
:added_tags => new_tags - old_tags,
:removed_tags => old_tags - new_tags,
:unchanged_tags => new_tags & old_tags,
:obsolete_added_tags => new_tags - latest_tags,
:obsolete_removed_tags => old_tags & latest_tags,
}
end
def previous
PostVersion.where("post_id = ? and id < ?", post_id, id).order("id desc").first
end
end

View File

@@ -7,8 +7,7 @@ class PostVersionPresenter < Presenter
def changes
html = []
html << post_version.del_tag_array.map {|x| "<del>#{h(x)}</del>"}
html << post_version.add_tag_array.map {|x| "<ins>#{h(x)}</ins>"}
html << post_version.tag_array
html << "<ins>source:#{h(post_version.source)}</ins>" if post_version.source
html << "<ins>rating:#{h(post_version.rating)}</ins>" if post_version.rating
html << "<ins>parent:#{post_version.parent_id}</ins>" if post_version.parent_id

View File

@@ -16,7 +16,7 @@
<% post_versions.each do |post_version| %>
<tr id="post-version-<%= post_version.id %>">
<td><%= link_to(post_version.post_id, post_path(post_version.post_id)) %></td>
<td><%= post_version.updated_at.strftime("%Y-%m-%d %H:%M") %></td>
<td><%= compact_time(post_version.updated_at) %></td>
<td><%= link_to(post_version.updater.name, user_path(post_version.updater_id)) %></td>
<td><%= post_version.rating %></td>
<td><%= post_version.parent_id %></td>
@@ -25,15 +25,7 @@
<%= post_version.updater_ip_addr %>
<% end %>
</td>
<td>
<% post_version.add_tag_array.each do |tag| %>
<ins><%= tag %></ins>
<% end %>
<% post_version.del_tag_array.each do |tag| %>
<del><%= tag %></del>
<% end %>
</td>
<td><%= post_version_diff(post_version) %></td>
<td>
<%= link_to "Revert", revert_post_path(post_version.post_id, :version_id => post_version.id), :method => :put, :remote => true %>
</td>

View File

@@ -2142,8 +2142,7 @@ CREATE TABLE post_versions (
created_at timestamp without time zone,
updated_at timestamp without time zone,
post_id integer NOT NULL,
add_tags text DEFAULT ''::text NOT NULL,
del_tags text DEFAULT ''::text NOT NULL,
tags text DEFAULT ''::text NOT NULL,
rating character(1),
parent_id integer,
source text,

View File

@@ -4,8 +4,7 @@ class CreatePostVersions < ActiveRecord::Migration
t.timestamps
t.column :post_id, :integer, :null => false
t.column :add_tags, :text, :null => false, :default => ""
t.column :del_tags, :text, :null => false, :default => ""
t.column :tags, :text, :null => false, :default => ""
t.column :rating, :char
t.column :parent_id, :integer
t.column :source, :text

View File

@@ -250,15 +250,11 @@ alter table post_versions rename column created_at to updated_at;
alter table post_versions rename column user_id to updater_id;
alter table post_versions rename column ip_addr to updater_ip_addr;
alter table post_versions add column source text;
alter table post_versions add column add_tags text not null default '';
alter table post_versions add column del_tags text not null default '';
alter index idx_post_tag_histories__post rename to index_post_versions_on_post_id;
alter index index_post_tag_histories_on_user_id rename to index_post_versions_on_updater_id;
create index index_post_versions_on_updater_ip_addr on post_versions (updater_ip_addr);
alter table post_versions drop column id;
alter table post_versions add column id serial primary key;
-- update post_versions.add_tags
-- update post_versions.del_tags
alter table post_votes drop constraint post_votes_post_id_fkey;
alter table post_votes drop constraint post_votes_user_id_fkey;
@@ -412,5 +408,4 @@ alter table wiki_pages drop constraint fk_wiki_pages__user;
drop table dmails_orig;
drop table favorites_orig;
drop table pools_posts;
alter table post_versions drop column tags;
alter table users drop column show_samples;

View File

@@ -53,7 +53,7 @@ class PostsControllerTest < ActionController::TestCase
should "work" do
@version = @post.versions(true).first
assert_equal("aaaa", @version.add_tags)
assert_equal("aaaa", @version.tags)
post :revert, {:id => @post.id, :version_id => @version.id}, {:user_id => @user.id}
assert_redirected_to post_path(@post)
@post.reload

View File

@@ -14,6 +14,31 @@ class PostVersionTest < ActiveSupport::TestCase
CurrentUser.ip_addr = nil
end
context "that has multiple versions: " do
setup do
@post = Factory.create(:post, :tag_string => "1")
@post.update_attributes(:tag_string => "1 2")
@post.update_attributes(:tag_string => "2 3")
end
context "a version record" do
setup do
@version = PostVersion.last
end
should "know its previous version" do
assert_not_nil(@version.previous)
assert_equal("1 2", @version.previous.tags)
end
should "know the seuqence of all versions for the post" do
assert_equal(2, @version.sequence_for_post.size)
assert_equal(%w(3), @version.sequence_for_post[0][:added_tags])
assert_equal(%w(2), @version.sequence_for_post[1][:added_tags])
end
end
end
context "that has been created" do
setup do
@parent = Factory.create(:post)
@@ -23,8 +48,7 @@ class PostVersionTest < ActiveSupport::TestCase
should "also create a version" do
assert_equal(1, @post.versions.size)
@version = @post.versions.last
assert_equal("aaa bbb ccc", @version.add_tags)
assert_equal("", @version.del_tags)
assert_equal("aaa bbb ccc", @version.tags)
assert_equal(@post.rating, @version.rating)
assert_equal(@post.parent_id, @version.parent_id)
assert_equal(@post.source, @version.source)
@@ -41,8 +65,7 @@ class PostVersionTest < ActiveSupport::TestCase
should "also create a version" do
assert_equal(2, @post.versions.size)
@version = @post.versions.last
assert_equal("xxx", @version.add_tags)
assert_equal("aaa", @version.del_tags)
assert_equal("bbb ccc xxx", @version.tags)
assert_nil(@version.rating)
assert_equal("", @version.source)
assert_nil(@version.parent_id)