This commit is contained in:
Toks
2015-05-02 11:12:30 -04:00
parent c8b41b4045
commit 5a8674d342
7 changed files with 89 additions and 4 deletions

View File

@@ -47,9 +47,11 @@ class PoolsController < ApplicationController
# need to do this in order for synchronize! to work correctly
@pool = Pool.find(params[:id])
@pool.attributes = params[:pool]
@pool.synchronize!
@pool.synchronize
@pool.save
flash[:notice] = "Pool updated"
unless @pool.errors.any?
flash[:notice] = "Pool updated"
end
respond_with(@pool)
end

View File

@@ -43,6 +43,9 @@ module DelayedJobsHelper
when "Class#decrement_post_counts"
"<strong>decrement post counts</strong>"
when "Pool#update_category_pseudo_tags_for_posts"
"<strong>update pool category pseudo tags for posts</strong>"
else
h(job.name)
end
@@ -89,6 +92,9 @@ module DelayedJobsHelper
when "Class#decrement_post_counts"
h(job.payload_object.args.join(" "))
when "Pool#update_category_pseudo_tags_for_posts"
%{<a href="/pools/#{job.payload_object.id}">#{h(job.payload_object.name)}</a>}
else
h(job.handler)
end

View File

@@ -4,6 +4,7 @@ class Pool < ActiveRecord::Base
validates_uniqueness_of :name, :case_sensitive => false
validates_format_of :name, :with => /\A[^,]+\Z/, :message => "cannot have commas"
validates_inclusion_of :category, :in => %w(series collection)
validate :updater_can_change_category
belongs_to :creator, :class_name => "User"
belongs_to :updater, :class_name => "User"
has_many :versions, lambda {order("pool_versions.id ASC")}, :class_name => "PoolVersion", :dependent => :destroy
@@ -12,6 +13,7 @@ class Pool < ActiveRecord::Base
before_validation :initialize_is_active, :on => :create
before_validation :initialize_creator, :on => :create
before_validation :strip_name
after_save :update_category_pseudo_tags_for_posts_async
after_save :create_version
after_create :synchronize!
before_destroy :create_mod_action_for_destroy
@@ -262,7 +264,7 @@ class Pool < ActiveRecord::Base
end
end
def synchronize!
def synchronize
added = post_id_array - post_id_array_was
removed = post_id_array_was - post_id_array
@@ -279,6 +281,10 @@ class Pool < ActiveRecord::Base
normalize_post_ids
clear_post_id_array
self.post_count = post_id_array.size
end
def synchronize!
synchronize
save
end
@@ -372,4 +378,31 @@ class Pool < ActiveRecord::Base
"updated_at" => updated_at
}
end
def update_category_pseudo_tags_for_posts_async
if category_changed?
delay(:queue => "default").update_category_pseudo_tags_for_posts
end
end
def update_category_pseudo_tags_for_posts
Post.where("id in (?)", post_id_array).find_each do |post|
post.reload
post.set_pool_category_pseudo_tags
Post.where(:id => post.id).update_all(:pool_string => post.pool_string)
end
end
def category_changeable_by?(user)
user.is_builder? || (user.is_member? && post_count <= 100)
end
def updater_can_change_category
if category_changed? && !category_changeable_by?(CurrentUser.user)
errors[:base] << "You cannot change the category of pools with greater than 100 posts"
false
else
true
end
end
end

View File

@@ -19,6 +19,7 @@ class Post < ActiveRecord::Base
before_save :normalize_tags
before_save :update_tag_post_counts
before_save :set_tag_counts
before_save :set_pool_category_pseudo_tags
before_validation :strip_source
before_validation :initialize_uploader, :on => :create
before_validation :parse_pixiv_id
@@ -835,6 +836,7 @@ class Post < ActiveRecord::Base
return if belongs_to_pool?(pool)
return if pool.is_deleted? && !force
self.pool_string = "#{pool_string} pool:#{pool.id}".strip
set_pool_category_pseudo_tags
update_column(:pool_string, pool_string) unless new_record?
pool.add!(self)
end
@@ -843,6 +845,7 @@ class Post < ActiveRecord::Base
return unless belongs_to_pool?(pool)
return if pool.is_deleted? && !force
self.pool_string = pool_string.gsub(/(?:\A| )pool:#{pool.id}(?:\Z| )/, " ").strip
set_pool_category_pseudo_tags
update_column(:pool_string, pool_string) unless new_record?
pool.remove!(self)
end
@@ -852,6 +855,18 @@ class Post < ActiveRecord::Base
pool.remove!(self)
end
end
def set_pool_category_pseudo_tags
self.pool_string = (pool_string.scan(/\S+/) - ["pool:series", "pool:collection"]).join(" ")
pool_categories = pools.select("category").map(&:category)
if pool_categories.include?("series")
self.pool_string = "#{pool_string} pool:series".strip
end
if pool_categories.include?("collection")
self.pool_string = "#{pool_string} pool:collection".strip
end
end
end
module VoteMethods

View File

@@ -426,13 +426,23 @@ class Tag < ActiveRecord::Base
q[:artcomm_ids] << user_id unless user_id.blank?
when "-pool"
q[:tags][:exclude] << "pool:#{Pool.name_to_id($2)}"
if $2.downcase == "series"
q[:tags][:exclude] << "pool:series"
elsif $2.downcase == "collection"
q[:tags][:exclude] << "pool:collection"
else
q[:tags][:exclude] << "pool:#{Pool.name_to_id($2)}"
end
when "pool"
if $2.downcase == "none"
q[:pool] = "none"
elsif $2.downcase == "any"
q[:pool] = "any"
elsif $2.downcase == "series"
q[:tags][:related] << "pool:series"
elsif $2.downcase == "collection"
q[:tags][:related] << "pool:collection"
elsif $2.include?("*")
pools = Pool.name_matches($2).select("id").limit(Danbooru.config.tag_query_limit).order("post_count DESC")
q[:tags][:include] += pools.map {|pool| "pool:#{pool.id}"}

View File

@@ -2,6 +2,9 @@
<div id="a-edit">
<%= simple_form_for(@pool) do |f| %>
<h1>Edit Pool: <%= @pool.pretty_name %></h1>
<%= error_messages_for "pool" %>
<%= f.input :name, :as => :string, :input_html => { :value => @pool.pretty_name } %>
<%= dtext_field "pool", "description" %>
<%= dtext_preview_button "pool", "description" %>

View File

@@ -0,0 +1,16 @@
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment'))
ActiveRecord::Base.connection.execute("set statement_timeout = 0")
CurrentUser.user = User.admins.first
CurrentUser.ip_addr = "127.0.0.1"
CurrentUser.without_safe_mode do
Post.tag_match("pool:any").find_each do |post|
post.reload
post.set_pool_category_pseudo_tags
Post.where(:id => post.id).update_all(:pool_string => post.pool_string)
end
end