* meta_search now pulls directly from GitHub

* Updated gems
* [inprogress] New pagination helpers used instead of pagination presenters
* [inprogress] Favorites refactored to use ActiveRecord
* [inprogress] PostSets refactored to use a decorator/dependency injection pattern
* [inprogress] Made pool/post interaction more robust
* Pool#posts now returns an ActiveRelation object
* Fixed unit tests
This commit is contained in:
albert
2011-06-07 17:34:09 -04:00
parent 435d3bf6e2
commit 49b3d43ddd
17 changed files with 248 additions and 136 deletions

View File

@@ -3,7 +3,7 @@ class Favorite < ActiveRecord::Base
validates_uniqueness_of :post_id, :scope => :user_id
def self.model_for(user_id)
mod = user_id % TABLE_COUNT
mod = user_id.to_i % TABLE_COUNT
Object.const_get("Favorite#{mod}")
end

View File

@@ -38,8 +38,10 @@ class ForumPost < ActiveRecord::Base
end
def update_topic_updated_at
topic.update_attribute(:updater_id, CurrentUser.id)
topic.touch
if topic
topic.update_attribute(:updater_id, CurrentUser.id)
topic.touch
end
end
def initialize_creator

View File

@@ -6,9 +6,10 @@ class Pool < ActiveRecord::Base
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_post_count
before_validation :initialize_creator, :on => :create
after_save :create_version
after_save :update_posts
after_save :balance_post_ids
attr_accessible :name, :description, :post_ids, :is_active, :post_id_array
def self.name_to_id(name)
@@ -29,58 +30,90 @@ class Pool < ActiveRecord::Base
end
end
def self.normalize_name(name)
name.downcase.gsub(/\s+/, "_")
end
def self.normalize_post_ids(post_ids)
post_ids.gsub(/\s{2,}/, " ").strip
end
def initialize_creator
self.creator_id = CurrentUser.id
end
def normalize_name
self.name = name.downcase
self.name = Pool.normalize_name(name)
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+$/, "")
self.post_ids = Pool.normalize_post_ids(post_ids)
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
def contains_post?(post_id)
post_ids =~ /(?:\A| )#{post_id}(?:\Z| )/
end
def add_post!(post)
return if post_ids =~ /(?:\A| )#{post.id}(?:\Z| )/
self.post_ids += " #{post.id}"
self.post_ids = post_ids.strip
return if contains_post?(post.id)
increment!(:post_count)
update_attribute(:post_ids, "#{post_ids} #{post.id}".strip)
post.add_pool!(self)
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
return unless contains_post?(post.id)
decrement!(:post_count)
update_attribute(:post_ids, Pool.normalize_post_ids(post_ids.gsub(/(?:\A| )#{post.id}(?:\Z| )/, " ")))
post.remove_pool!(self)
clear_post_id_array
save
end
def posts(options = {})
offset = options[:offset] || 0
limit = options[:limit] || Danbooru.config.posts_per_page
ids = post_id_array[offset, limit]
Post.where(["id IN (?)", ids]).order(Favorite.sql_order_clause(ids))
if options[:offset]
limit = options[:limit] || Danbooru.config.posts_per_page
slice = post_id_array.slice(options[:offset], limit)
if slice && slice.any?
Post.where("id in (?)", slice).order(arbitrary_sql_order_clause(slice, "posts"))
else
Post.where("false")
end
else
Post.where("id IN (?)", post_id_array).order(arbitrary_sql_order_clause(post_id_array, "posts"))
end
end
def balance_post_ids
added = post_id_array - post_id_array_was
removed = post_id_array_was - post_id_array
added.each do |post_id|
post = Post.find(post_id)
post.add_pool!(self)
end
removed.each do |post_id|
post = Post.find(post_id)
post.remove_pool!(self)
end
end
def post_id_array
@post_id_array ||= post_ids.scan(/\d+/).map(&:to_i)
end
def post_id_array_was
@post_id_array_was ||= post_ids_was.scan(/\d+/).map(&:to_i)
end
def post_id_array=(array)
self.post_ids = array.join(" ")
clear_post_id_array
@@ -88,6 +121,11 @@ class Pool < ActiveRecord::Base
def clear_post_id_array
@post_id_array = nil
@post_id_array_was = nil
end
def initialize_post_count
self.post_count = post_id_array.size
end
def neighbor_posts(post)
@@ -95,13 +133,13 @@ class Pool < ActiveRecord::Base
post_ids =~ /\A#{post.id} (\d+)|(\d+) #{post.id} (\d+)|(\d+) #{post.id}\Z/
if $2 && $3
{:previous => $2.to_i, :next => $3.to_i}
OpenStruct.new(:previous => $2.to_i, :next => $3.to_i)
elsif $1
{:next => $1.to_i}
OpenStruct.new(:next => $1.to_i)
elsif $4
{:previous => $4.to_i}
OpenStruct.new(:previous => $4.to_i)
else
{}
OpenStruct.new
end
end
end

View File

@@ -632,17 +632,20 @@ class Post < ActiveRecord::Base
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)
def belongs_to_pool?(pool)
pool_string =~ /(?:\A| )pool:#{pool.id}(?:\Z| )/
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)
def add_pool!(pool)
return if belongs_to_pool?(pool)
update_attribute(:pool_string, "#{pool_string} pool:#{pool.id}".strip)
pool.add_post!(self)
end
def remove_pool!(pool)
return unless belongs_to_pool?(pool)
update_attribute(:pool_string, pool_string.gsub(/(?:\A| )pool:#{pool.id}(?:\Z| )/, " ").strip)
pool.remove_post!(self)
end
end