separated pool/post updates; fixed unit tests

This commit is contained in:
albert
2011-03-13 17:49:34 -04:00
parent 4717726217
commit 90a41a334b
13 changed files with 83 additions and 1334 deletions

View File

@@ -13,7 +13,7 @@ module PostSets
end
def load_posts
@posts = pool.posts(:limit => limit, :offset => offset)
@posts = pool.posts(:limit => limit, :offset => offset).order("posts.id")
end
def sorted_posts

View File

@@ -5,8 +5,10 @@ class Pool < ActiveRecord::Base
belongs_to :updater, :class_name => "User"
has_many :versions, :class_name => "PoolVersion", :dependent => :destroy, :order => "pool_versions.id ASC"
before_validation :normalize_name
before_validation :normalize_post_ids
before_validation :initialize_creator, :on => :create
after_save :create_version
after_save :update_posts
attr_accessible :name, :description, :post_ids, :is_active, :post_id_array
def self.name_to_id(name)
@@ -35,22 +37,36 @@ class Pool < ActiveRecord::Base
self.name = name.downcase
end
def normalize_post_ids
self.post_ids = post_ids.gsub(/\s\s+/, " ")
self.post_ids = post_ids.gsub(/^\s+/, "")
self.post_ids = post_ids.gsub(/\s+$/, "")
end
def revert_to!(version)
self.post_ids = version.post_ids
save
end
def update_posts
post_id_array.each do |post_id|
post = Post.find(post_id)
post.add_pool(self)
end
end
def add_post!(post)
return if post_ids =~ /(?:\A| )#{post.id}(?:\Z| )/
self.post_ids += " #{post.id}"
self.post_ids = post_ids.strip
clear_post_id_array
save
end
def remove_post!(post)
self.post_ids = post_ids.gsub(/(?:\A| )#{post.id}(?:\Z| )/, " ")
self.post_ids = post_ids.strip
clear_post_id_array
save
end

View File

@@ -22,6 +22,7 @@ class Post < ActiveRecord::Base
has_many :notes, :dependent => :destroy
has_many :comments
has_many :children, :class_name => "Post", :foreign_key => "parent_id", :order => "posts.id"
has_many :disapprovals, :class_name => "PostDisapproval"
validates_uniqueness_of :md5
validates_presence_of :parent, :if => lambda {|rec| !rec.parent_id.nil?}
validate :validate_parent_does_not_have_a_parent
@@ -615,19 +616,24 @@ class Post < ActiveRecord::Base
end
module PoolMethods
def pools
@pools ||= begin
pool_ids = pool_string.scan(/\d+/)
Pool.where(["id in (?)", pool_ids])
end
end
def add_pool(pool)
return if pool_string =~ /(?:\A| )pool:#{pool.id}(?:\Z| )/
self.pool_string += " pool:#{pool.id}"
self.pool_string.strip!
execute_sql("UPDATE posts SET pool_string = ? WHERE id = ?", pool_string, id)
pool.add_post!(self)
end
def remove_pool(pool)
self.pool_string.gsub!(/(?:\A| )pool:#{pool.id}(?:\Z| )/, " ")
self.pool_string.strip!
execute_sql("UPDATE posts SET pool_string = ? WHERE id = ?", pool_string, id)
pool.remove_post!(self)
end
end

View File

@@ -198,7 +198,7 @@ class Tag < ActiveRecord::Base
output[:exclude] << tag[1..-1]
elsif tag =~ /\*/
matches = Tag.by_pattern(tag).all(:select => "name", :limit => 25, :order => "post_count DESC").map(&:name)
matches = Tag.name_matches(tag).all(:select => "name", :limit => 25, :order => "post_count DESC").map(&:name)
matches = ["~no_matches~"] if matches.empty?
output[:include] += matches

View File

@@ -1,22 +1,36 @@
<form method="get" action="/post/moderate">
<%= text_field_tag "query", params[:query], :size => 40 %>
<%= submit_tag "Search" %>
</form>
<script type="text/javascript" charset="utf-8">
Post.mod_queue = $A(<%= @posts.map(&:id).to_json %>)
</script>
<div id="search">
<%= simple_form_for(@search) do |f| %>
<%= f.input "tags_match", :input_html => {:size => 40}, :label => false %>
<% end %>
</div>
<div style="margin-bottom: 2em;">
<h2>Moderation Queue</h2>
<div style="margin: 1em 0;">
<h4>Deletion Guidelines</h4>
<p>As a general rule, you should not delete posts. Only approve of posts that you personally like. Posts that are not approved in three days will be automatically deleted. Posts with score -3 or lower are marked red. Posts with score 3 or higher are marked green. Posts tagged as duplicate are marked yellow. <% if params[:hidden] %>Click <%= fast_link_to "here", :action => "moderate", :query => params[:query], :hidden => nil %> to view all posts.<% else %>Click <%= fast_link_to "here", :action => "moderate", :query => params[:query], :hidden => true %> to view hidden posts.<% end %></p>
<p>Mass update: <%= link_to_function "Approve all", "if (confirm('This will approve every pending post on this page. Continue?')) {Post.mass_moderate('Approve')}" %> | <%= link_to_function "Hide all", "if (confirm('This will hide every pending post on this page. Continue?')) {Post.mass_moderate('Hide')}" %></p>
</div>
<% @posts.each do |post| %>
<div>
<div>
<%= link_to(image_tag(post.medium_file_url), post_path(post)) %>
</div>
<div>
<ul>
<li>Rating: <%= post.pretty_rating %></li>
<li>Score: <%= post.score %></li>
<li>Uploader: <%= link_to(post.uploader.name, user_path(post.uploader_id)) %> <%= time_ago_in_words(post.updated_at) %> ago</li>
<% if post.is_flagged? %>
<li>Flagged: <%= post.unapproval.reason %> by <%= post.unapproval.unapprover.name %></li>
<% end %>
<li>Hidden: <%= post.disapprovals.count %></li>
<li>Tags: <%= post.tag_string %></li>
<% if post.pools.any? %>
<li>Pools: </li>
<% end %>
</ul>
</div>
</div>
<% end %>
<table width="100%">
<tbody>
<% @posts.each do |p| %>

View File

@@ -7,7 +7,7 @@
<li>Subscriptions</li>
<li><%= link_to "Changes", post_versions_path %></li>
<li>Approvals</li>
<li>Moderate</li>
<li><%= link_to "Moderate", post_moderation_moderate_path %></li>
<li>Help</li>
</menu>
<% end %>

View File

@@ -0,0 +1,5 @@
<ul>
<% post.pools.each do |pool| %>
<li><span class="ui-icon ui-icon-circle-arrow-w"></span><span class="ui-icon ui-icon-circle-arrow-e"></span> <%= link_to pool_path(pool), pool.name %></li>
<% end %>
</ul>

View File

@@ -5,6 +5,13 @@
<h1>Search</h1>
<%= render :partial => "posts/partials/common/search" %>
</section>
<% if @post.pools.any? %>
<section>
<h1>Pools</h1>
<%= render "posts/partials/show/pools", :locals => {:post => @post} %>
</section>
<% end %>
<section>
<h1>Tags</h1>
@@ -19,7 +26,7 @@
<section>
<h1>Options</h1>
<%= render :partial => "posts/partials/show/options", :locals => {:post => @post} %>
</section>
</section>
</aside>
<section id="content">

View File

@@ -1,4 +1,4 @@
<p><%= h @dmail.fromr.name %> said:</p>
<p><%= h @dmail.from.name %> said:</p>
<div>
<%= h @dmail.body %>

File diff suppressed because one or more lines are too long

View File

@@ -103,7 +103,7 @@ class ArtistTest < ActiveSupport::TestCase
should "have an associated wiki" do
user = Factory.create(:user)
artist = Factory.create(:artist, :name => "max", :wiki_page_attributes => {:body => "this is max"})
artist = Factory.create(:artist, :name => "max", :wiki_page_attributes => {:title => "xxx", :body => "this is max"})
assert_not_nil(artist.wiki_page)
assert_equal("this is max", artist.wiki_page.body)

View File

@@ -42,6 +42,10 @@ class PoolTest < ActiveSupport::TestCase
p1.add_pool(pool)
p2.add_pool(pool)
p3.add_pool(pool)
pool.add_post!(p1)
pool.add_post!(p2)
pool.add_post!(p3)
pool.reload
assert_equal("#{p1.id} #{p2.id} #{p3.id}", pool.post_ids)
assert_equal([p1.id, p2.id, p3.id], pool.post_id_array)
@@ -61,6 +65,10 @@ class PoolTest < ActiveSupport::TestCase
p1.add_pool(pool)
p2.add_pool(pool)
p3.add_pool(pool)
pool.add_post!(p1)
pool.add_post!(p2)
pool.add_post!(p3)
pool.reload
neighbors = pool.neighbor_posts(p1)
assert_nil(neighbors[:previous])
assert_equal(p2.id, neighbors[:next])

View File

@@ -16,6 +16,9 @@ module PostSets
@pool.add_post!(@post_2)
@pool.add_post!(@post_1)
@pool.add_post!(@post_3)
@post_2.add_pool(@pool)
@post_1.add_pool(@pool)
@post_3.add_pool(@pool)
@set = PostSets::Pool.new(@pool, :page => 1)
end