refactored search
This commit is contained in:
@@ -29,7 +29,7 @@ class Advertisement < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def date_prefix
|
||||
created_at.strftime("%Y%m%d")
|
||||
created_at.try(:strftime, "%Y%m%d")
|
||||
end
|
||||
|
||||
def image_path
|
||||
@@ -43,6 +43,7 @@ class Advertisement < ActiveRecord::Base
|
||||
def file=(f)
|
||||
if f.size > 0
|
||||
self.file_name = unique_identifier + File.extname(f.original_filename)
|
||||
FileUtils.mkdir_p(File.dirname(image_path))
|
||||
|
||||
if f.local_path
|
||||
FileUtils.cp(f.local_path, image_path)
|
||||
|
||||
@@ -14,8 +14,6 @@ class Artist < ActiveRecord::Base
|
||||
accepts_nested_attributes_for :wiki_page
|
||||
attr_accessible :body, :name, :url_string, :other_names, :group_name, :wiki_page_attributes, :notes, :is_active, :as => [:member, :privileged, :contributor, :janitor, :moderator, :default, :admin]
|
||||
attr_accessible :is_banned, :as => :admin
|
||||
scope :active, where("is_active = true")
|
||||
scope :banned, where("is_banned = true")
|
||||
|
||||
module UrlMethods
|
||||
extend ActiveSupport::Concern
|
||||
@@ -191,67 +189,72 @@ class Artist < ActiveRecord::Base
|
||||
end
|
||||
|
||||
module SearchMethods
|
||||
extend ActiveSupport::Concern
|
||||
def active
|
||||
where("is_active = true")
|
||||
end
|
||||
|
||||
def banned
|
||||
where("is_banned = true")
|
||||
end
|
||||
|
||||
def url_matches(string)
|
||||
matches = find_all_by_url(string).map(&:id)
|
||||
|
||||
if matches.any?
|
||||
where("id in (?)", matches)
|
||||
else
|
||||
where("false")
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def url_matches(string)
|
||||
matches = find_all_by_url(string).map(&:id)
|
||||
def other_names_match(string)
|
||||
where("other_names_index @@ to_tsquery('danbooru', ?)", Artist.normalize_name(string))
|
||||
end
|
||||
|
||||
def group_name_matches(name)
|
||||
stripped_name = normalize_name(name).to_escaped_for_sql_like
|
||||
where("group_name LIKE ? ESCAPE E'\\\\'", stripped_name)
|
||||
end
|
||||
|
||||
def name_matches(name)
|
||||
stripped_name = normalize_name(name).to_escaped_for_sql_like
|
||||
where("name LIKE ? ESCAPE E'\\\\'", stripped_name)
|
||||
end
|
||||
|
||||
def any_name_matches(name)
|
||||
stripped_name = normalize_name(name).to_escaped_for_sql_like
|
||||
where("(name LIKE ? ESCAPE E'\\\\' OR other_names_index @@ to_tsquery('danbooru', ?))", stripped_name, normalize_name(name))
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = active
|
||||
return q if params.blank?
|
||||
|
||||
if matches.any?
|
||||
where("id in (?)", matches)
|
||||
else
|
||||
where("false")
|
||||
end
|
||||
end
|
||||
|
||||
def other_names_match(string)
|
||||
where("other_names_index @@ to_tsquery('danbooru', ?)", Artist.normalize_name(string))
|
||||
end
|
||||
|
||||
def group_name_matches(name)
|
||||
stripped_name = normalize_name(name).to_escaped_for_sql_like
|
||||
where("group_name LIKE ? ESCAPE E'\\\\'", stripped_name)
|
||||
end
|
||||
|
||||
def name_matches(name)
|
||||
stripped_name = normalize_name(name).to_escaped_for_sql_like
|
||||
where("name LIKE ? ESCAPE E'\\\\'", stripped_name)
|
||||
end
|
||||
|
||||
def any_name_matches(name)
|
||||
stripped_name = normalize_name(name).to_escaped_for_sql_like
|
||||
where("(name LIKE ? ESCAPE E'\\\\' OR other_names_index @@ to_tsquery('danbooru', ?))", stripped_name, normalize_name(name))
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = active
|
||||
case params[:name]
|
||||
when /^http/
|
||||
q = q.url_matches(params[:name])
|
||||
|
||||
case params[:name]
|
||||
when /^http/
|
||||
q = q.url_matches(params[:name])
|
||||
|
||||
when /name:(.+)/
|
||||
q = q.name_matches($1)
|
||||
|
||||
when /other:(.+)/
|
||||
q = q.other_names_match($1)
|
||||
|
||||
when /group:(.+)/
|
||||
q = q.group_name_matches($1)
|
||||
|
||||
when /status:banned/
|
||||
q = q.banned
|
||||
|
||||
when /./
|
||||
q = q.any_name_matches(params[:name])
|
||||
end
|
||||
|
||||
if params[:id]
|
||||
q = q.where("id = ?", params[:id])
|
||||
end
|
||||
when /name:(.+)/
|
||||
q = q.name_matches($1)
|
||||
|
||||
q
|
||||
when /other:(.+)/
|
||||
q = q.other_names_match($1)
|
||||
|
||||
when /group:(.+)/
|
||||
q = q.group_name_matches($1)
|
||||
|
||||
when /status:banned/
|
||||
q = q.banned
|
||||
|
||||
when /./
|
||||
q = q.any_name_matches(params[:name])
|
||||
end
|
||||
|
||||
if params[:id]
|
||||
q = q.where("id = ?", params[:id])
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
@@ -263,7 +266,7 @@ class Artist < ActiveRecord::Base
|
||||
include NoteMethods
|
||||
include TagMethods
|
||||
include BanMethods
|
||||
include SearchMethods
|
||||
extend SearchMethods
|
||||
|
||||
def status
|
||||
if is_banned?
|
||||
|
||||
@@ -4,6 +4,7 @@ class ArtistVersion < ActiveRecord::Base
|
||||
|
||||
def self.search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:artist_id]
|
||||
q = q.where("artist_id = ?", params[:artist_id].to_i)
|
||||
|
||||
@@ -13,6 +13,7 @@ class Ban < ActiveRecord::Base
|
||||
|
||||
def self.search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:banner_name]
|
||||
q = q.where("banner_id = (select _.id from users _ where lower(_.name) = ?)", params[:banner_name].downcase)
|
||||
|
||||
@@ -9,57 +9,60 @@ class Comment < ActiveRecord::Base
|
||||
attr_accessible :body, :post_id
|
||||
attr_accessor :do_not_bump_post
|
||||
|
||||
scope :recent, :order => "comments.id desc", :limit => 6
|
||||
|
||||
module SearchMethods
|
||||
extend ActiveSupport::Concern
|
||||
def recent
|
||||
order("comments.id desc").limit(6)
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def body_matches(query)
|
||||
where("body_index @@ plainto_tsquery(?)", query).order("comments.id DESC")
|
||||
def body_matches(query)
|
||||
where("body_index @@ plainto_tsquery(?)", query).order("comments.id DESC")
|
||||
end
|
||||
|
||||
def hidden(user)
|
||||
where("score < ?", user.comment_threshold)
|
||||
end
|
||||
|
||||
def visible(user)
|
||||
where("score >= ?", user.comment_threshold)
|
||||
end
|
||||
|
||||
def post_tags_match(query)
|
||||
joins(:post).where("posts.tag_index @@ to_tsquery('danbooru', ?)", query)
|
||||
end
|
||||
|
||||
def for_creator(user_id)
|
||||
where("creator_id = ?", user_id)
|
||||
end
|
||||
|
||||
def for_creator_name(user_name)
|
||||
where("creator_id = (select _.id from users _ where lower(_.name) = lower(?))", user_name)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:body_matches]
|
||||
q = q.body_matches(params[:body_matches])
|
||||
end
|
||||
|
||||
def hidden(user)
|
||||
where("score < ?", user.comment_threshold)
|
||||
if params[:post_tags_match]
|
||||
q = q.post_tags_match(params[:post_tags_match])
|
||||
end
|
||||
|
||||
def visible(user)
|
||||
where("score >= ?", user.comment_threshold)
|
||||
if params[:creator_name]
|
||||
q = q.for_user_name(params[:creator_name])
|
||||
end
|
||||
|
||||
def post_tags_match(query)
|
||||
joins(:post).where("posts.tag_index @@ to_tsquery('danbooru', ?)", query)
|
||||
if params[:creator_id]
|
||||
q = q.for_creator(params[:creator_id].to_i)
|
||||
end
|
||||
|
||||
def for_creator(user_id)
|
||||
where("creator_id = ?", user_id)
|
||||
end
|
||||
|
||||
def for_creator_name(user_name)
|
||||
where("creator_id = (select _.id from users _ where lower(_.name) = lower(?))", user_name)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
|
||||
if params[:body_matches]
|
||||
q = q.body_matches(params[:body_matches])
|
||||
end
|
||||
|
||||
if params[:post_tags_match]
|
||||
q = q.post_tags_match(params[:post_tags_match])
|
||||
end
|
||||
|
||||
if params[:creator_name]
|
||||
q = q.for_user_name(params[:creator_name])
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
include SearchMethods
|
||||
extend SearchMethods
|
||||
|
||||
def initialize_creator
|
||||
self.creator_id = CurrentUser.user.id
|
||||
|
||||
@@ -10,16 +10,6 @@ class Dmail < ActiveRecord::Base
|
||||
after_create :update_recipient
|
||||
after_create :send_dmail
|
||||
attr_accessible :title, :body, :is_deleted, :to_id, :to, :to_name
|
||||
scope :for, lambda {|user| where(["owner_id = ?", user])}
|
||||
scope :inbox, where("to_id = owner_id")
|
||||
scope :sent, where("from_id = owner_id")
|
||||
scope :active, where(["is_deleted = ?", false])
|
||||
scope :deleted, where(["is_deleted = ?", true])
|
||||
scope :search_message, lambda {|query| where(["message_index @@ plainto_tsquery(?)", query])}
|
||||
scope :unread, where("is_read = false and is_deleted = false")
|
||||
scope :visible, lambda {where("owner_id = ?", CurrentUser.id)}
|
||||
scope :to_name_matches, lambda {|name| where("to_id = (select _.id from users _ where lower(_.name) = ?)", name.downcase)}
|
||||
scope :from_name_matches, lambda {|name| where("from_id = (select _.id from users _ where lower(_.name) = ?)", name.downcase)}
|
||||
|
||||
module AddressMethods
|
||||
def to_name
|
||||
@@ -82,8 +72,82 @@ class Dmail < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
module SearchMethods
|
||||
def for(user)
|
||||
where("owner_id = ?", user)
|
||||
end
|
||||
|
||||
def inbox
|
||||
where("to_id = owner_id")
|
||||
end
|
||||
|
||||
def sent
|
||||
where("from_id = owner_id")
|
||||
end
|
||||
|
||||
def active
|
||||
where("is_deleted = ?", false)
|
||||
end
|
||||
|
||||
def deleted
|
||||
where("is_deleted = ?", true)
|
||||
end
|
||||
|
||||
def search_message(query)
|
||||
where("message_index @@ plainto_tsquery(?)", query)
|
||||
end
|
||||
|
||||
def unread
|
||||
where("is_read = false and is_deleted = false")
|
||||
end
|
||||
|
||||
def visible
|
||||
where("owner_id = ?", CurrentUser.id)
|
||||
end
|
||||
|
||||
def to_name_matches(name)
|
||||
where("to_id = (select _.id from users _ where lower(_.name) = ?)", name.downcase)
|
||||
end
|
||||
|
||||
def from_name_matches(name)
|
||||
where("from_id = (select _.id from users _ where lower(_.name) = ?)", name.downcase)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:message_matches]
|
||||
q = q.search_message(params[:message_matches])
|
||||
end
|
||||
|
||||
if params[:owner_id]
|
||||
q = q.for(params[:owner_id].to_i)
|
||||
end
|
||||
|
||||
if params[:to_name]
|
||||
q = q.to_name_matches(params[:to_name])
|
||||
end
|
||||
|
||||
if params[:to_id]
|
||||
q = q.where("to_id = ?", params[:to_id].to_i)
|
||||
end
|
||||
|
||||
if params[:from_name]
|
||||
q = q.from_name_matches(params[:from_name])
|
||||
end
|
||||
|
||||
if params[:from_id]
|
||||
q = q.where("from_id = ?", params[:from_id].to_i)
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
include AddressMethods
|
||||
include FactoryMethods
|
||||
extend SearchMethods
|
||||
|
||||
def quoted_body
|
||||
"[quote]#{body}[/quote]"
|
||||
|
||||
@@ -10,9 +10,53 @@ class ForumPost < ActiveRecord::Base
|
||||
validates_presence_of :body, :creator_id
|
||||
validate :validate_topic_is_unlocked
|
||||
before_destroy :validate_topic_is_unlocked
|
||||
scope :body_matches, lambda {|body| where(["forum_posts.text_index @@ plainto_tsquery(?)", body])}
|
||||
scope :for_user, lambda {|user_id| where("forum_posts.creator_id = ?", user_id)}
|
||||
scope :active, where("is_deleted = false")
|
||||
|
||||
module SearchMethods
|
||||
def body_matches(body)
|
||||
where("forum_posts.text_index @@ plainto_tsquery(?)", body)
|
||||
end
|
||||
|
||||
def for_user(user_id)
|
||||
where("forum_posts.creator_id = ?", user_id)
|
||||
end
|
||||
|
||||
def creator_name(name)
|
||||
where("forum_posts.creator_id = (select _.id from users _ where lower(_.name) = ?)", name)
|
||||
end
|
||||
|
||||
def active
|
||||
where("is_deleted = false")
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:creator_id]
|
||||
q = q.where("creator_id = ?", params[:creator_id].to_i)
|
||||
end
|
||||
|
||||
if params[:topic_id]
|
||||
q = q.where("topic_id = ?", params[:topic_id].to_i)
|
||||
end
|
||||
|
||||
if params[:topic_title_matches]
|
||||
q = q.joins(:topic).where("forum_topics.text_index @@ plainto_tsquery(?)", params[:topic_title_matches])
|
||||
end
|
||||
|
||||
if params[:body_matches]
|
||||
q = q.body_matches(params[:body_matches])
|
||||
end
|
||||
|
||||
if params[:creator_name]
|
||||
q = q.creator_name(params[:creator_name])
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
|
||||
def self.new_reply(params)
|
||||
if params[:topic_id]
|
||||
|
||||
@@ -10,10 +10,35 @@ class ForumTopic < ActiveRecord::Base
|
||||
before_validation :initialize_is_deleted, :on => :create
|
||||
validates_presence_of :title, :creator_id
|
||||
validates_associated :original_post
|
||||
scope :title_matches, lambda {|title| where(["text_index @@ plainto_tsquery(?)", title])}
|
||||
scope :active, where("is_deleted = false")
|
||||
accepts_nested_attributes_for :original_post
|
||||
|
||||
module SearchMethods
|
||||
def title_matches(title)
|
||||
where("text_index @@ plainto_tsquery(?)", title)
|
||||
end
|
||||
|
||||
def active
|
||||
where("is_deleted = false")
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:title_matches]
|
||||
q = q.title_matches(params[:title_matches])
|
||||
end
|
||||
|
||||
if params[:title]
|
||||
q = q.where("title = ?", params[:title])
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
|
||||
def editable_by?(user)
|
||||
creator_id == user.id || user.is_moderator?
|
||||
end
|
||||
|
||||
@@ -7,6 +7,17 @@ class IpBan < ActiveRecord::Base
|
||||
def self.is_banned?(ip_addr)
|
||||
exists?(["ip_addr = ?", ip_addr])
|
||||
end
|
||||
|
||||
def self.search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:ip_addr]
|
||||
q = q.where("ip_addr = ?", params[:ip_addr])
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
|
||||
def self.query(user_ids)
|
||||
comments = count_by_ip_addr("comments", user_ids, "creator_id", "ip_addr")
|
||||
|
||||
@@ -6,6 +6,17 @@ class JanitorTrial < ActiveRecord::Base
|
||||
after_destroy :create_feedback
|
||||
validates_presence_of :user
|
||||
before_validation :initialize_creator
|
||||
|
||||
def self.search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:user_name]
|
||||
q = q.where("user_id = (select _.id from users _ where lower(_.name) = ?)", params[:user_name].downcase)
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
|
||||
def initialize_creator
|
||||
self.creator_id = CurrentUser.id
|
||||
|
||||
@@ -12,9 +12,45 @@ class Note < ActiveRecord::Base
|
||||
after_save :create_version
|
||||
validate :post_must_not_be_note_locked
|
||||
attr_accessible :x, :y, :width, :height, :body, :updater_id, :updater_ip_addr, :is_active, :post_id, :html_id
|
||||
scope :active, where("is_active = TRUE")
|
||||
scope :body_matches, lambda {|query| where("body_index @@ plainto_tsquery(?)", query.scan(/\S+/).join(" & "))}
|
||||
scope :post_tag_match, lambda {|query| joins(:post).where("posts.tag_index @@ to_tsquery('danbooru', ?)", query)}
|
||||
|
||||
module SearchMethods
|
||||
def active
|
||||
where("is_active = TRUE")
|
||||
end
|
||||
|
||||
def body_matches(query)
|
||||
where("body_index @@ plainto_tsquery(?)", query.scan(/\S+/).join(" & "))
|
||||
end
|
||||
|
||||
def post_tags_match(query)
|
||||
joins(:post).where("posts.tag_index @@ to_tsquery('danbooru', ?)", query)
|
||||
end
|
||||
|
||||
def creator_name(name)
|
||||
where("creator_id = (select _.id from users _ where lower(_.name) = ?)", name)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:body_matches]
|
||||
q = q.body_matches(params[:body_matches])
|
||||
end
|
||||
|
||||
if params[:post_tags_match]
|
||||
q = q.post_tags_match(params[:post_tags_match])
|
||||
end
|
||||
|
||||
if params[:creator_name]
|
||||
q = q.creator_name(params[:creator_name])
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
|
||||
def presenter
|
||||
@presenter ||= NotePresenter.new(self)
|
||||
|
||||
@@ -2,6 +2,21 @@ class NoteVersion < ActiveRecord::Base
|
||||
before_validation :initialize_updater
|
||||
belongs_to :updater, :class_name => "User"
|
||||
scope :for_user, lambda {|user_id| where("updater_id = ?", user_id)}
|
||||
|
||||
def self.search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:updater_id]
|
||||
q = q.where("updater_id = ?", params[:updater_id].to_i)
|
||||
end
|
||||
|
||||
if params[:post_id]
|
||||
q = q.where("post_id = ?", params[:post_id].to_i)
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
|
||||
def initialize_updater
|
||||
self.updater_id = CurrentUser.id
|
||||
|
||||
@@ -14,7 +14,33 @@ class Pool < ActiveRecord::Base
|
||||
before_destroy :create_mod_action_for_destroy
|
||||
attr_accessible :name, :description, :post_ids, :post_id_array, :post_count, :is_active, :as => [:member, :privileged, :contributor, :janitor, :moderator, :admin, :default]
|
||||
attr_accessible :is_deleted, :as => [:janitor, :moderator, :admin]
|
||||
scope :active, where("is_active = true and is_deleted = false")
|
||||
|
||||
module SearchMethods
|
||||
def active
|
||||
where("is_active = true and is_deleted = false")
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:name_matches]
|
||||
q = q.where("name like ? escape E'\\\\'", params[:name_matches])
|
||||
end
|
||||
|
||||
if params[:description_matches]
|
||||
q = q.where("description like ? escape E'\\\\'", params[:description_matches])
|
||||
end
|
||||
|
||||
if params[:creator_name]
|
||||
q = q.where("creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name])
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
|
||||
def self.name_to_id(name)
|
||||
if name =~ /^\d+$/
|
||||
|
||||
@@ -5,7 +5,25 @@ class PoolVersion < ActiveRecord::Base
|
||||
belongs_to :pool
|
||||
belongs_to :updater, :class_name => "User"
|
||||
before_validation :initialize_updater
|
||||
scope :for_user, lambda {|user_id| where("updater_id = ?", user_id)}
|
||||
|
||||
module SearchMethods
|
||||
def for_user(user_id)
|
||||
where("updater_id = ?", user_id)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:updater_id]
|
||||
q = q.for_user(params[:updater_id].to_i)
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
|
||||
def initialize_updater
|
||||
self.updater_id = CurrentUser.id
|
||||
|
||||
@@ -33,34 +33,6 @@ class Post < ActiveRecord::Base
|
||||
attr_accessible :source, :rating, :tag_string, :old_tag_string, :last_noted_at, :parent_id, :as => [:member, :privileged, :contributor, :janitor, :moderator, :admin, :default]
|
||||
attr_accessible :is_rating_locked, :is_note_locked, :as => [:janitor, :moderator, :admin]
|
||||
attr_accessible :is_status_locked, :as => [:admin]
|
||||
scope :pending, where(["is_pending = ?", true])
|
||||
scope :pending_or_flagged, where(["(is_pending = ? OR is_flagged = ?)", true, true])
|
||||
scope :undeleted, where(["is_deleted = ?", false])
|
||||
scope :deleted, where(["is_deleted = ?", true])
|
||||
scope :visible, lambda {|user| Danbooru.config.can_user_see_post_conditions(user)}
|
||||
scope :commented_before, lambda {|date| where("last_commented_at < ?", date).order("last_commented_at DESC")}
|
||||
scope :has_notes, where("last_noted_at is not null")
|
||||
scope :for_user, lambda {|user_id| where(["uploader_id = ?", user_id])}
|
||||
scope :available_for_moderation, lambda {|hidden| hidden.present? ? where(["id IN (SELECT pd.post_id FROM post_disapprovals pd WHERE pd.user_id = ?)", CurrentUser.id]) : where(["id NOT IN (SELECT pd.post_id FROM post_disapprovals pd WHERE pd.user_id = ?)", CurrentUser.id])}
|
||||
scope :hidden_from_moderation, lambda {where(["id IN (SELECT pd.post_id FROM post_disapprovals pd WHERE pd.user_id = ?)", CurrentUser.id])}
|
||||
scope :tag_match, lambda {|query| PostQueryBuilder.new(query).build}
|
||||
scope :positive, where("score > 1")
|
||||
scope :negative, where("score < -1")
|
||||
scope :updater_name_matches, lambda {|name| where("updater_id = (select _.id from users _ where lower(_.name) = ?)", name.downcase)}
|
||||
scope :after_id, Proc.new {|num|
|
||||
if num.present?
|
||||
where("id > ?", num.to_i).reorder("id asc")
|
||||
else
|
||||
where("true")
|
||||
end
|
||||
}
|
||||
scope :before_id, Proc.new {|num|
|
||||
if num.present?
|
||||
where("id < ?", num.to_i).reorder("id desc")
|
||||
else
|
||||
where("true")
|
||||
end
|
||||
}
|
||||
|
||||
module FileMethods
|
||||
def distribute_files
|
||||
@@ -619,13 +591,13 @@ class Post < ActiveRecord::Base
|
||||
tags = tags.to_s.strip
|
||||
count = get_count_from_cache(tags)
|
||||
if count.nil?
|
||||
if tags.blank?
|
||||
count = 1_000_000
|
||||
if tags.blank? && Danbooru.config.blank_tag_search_fast_count
|
||||
count = Danbooru.config.blank_tag_search_fast_count
|
||||
else
|
||||
begin
|
||||
count = Post.tag_match(tags).undeleted.count
|
||||
rescue ActiveRecord::StatementInvalid
|
||||
count = 1_000_000
|
||||
count = Danbooru.config.blank_tag_search_fast_count || 1_000_000
|
||||
end
|
||||
end
|
||||
|
||||
@@ -867,6 +839,103 @@ class Post < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
module SearchMethods
|
||||
def pending
|
||||
where("is_pending = ?", true)
|
||||
end
|
||||
|
||||
def pending_or_flagged
|
||||
where("(is_pending = ? OR is_flagged = ?)", true, true)
|
||||
end
|
||||
|
||||
def undeleted
|
||||
where("is_deleted = ?", false)
|
||||
end
|
||||
|
||||
def deleted
|
||||
where("is_deleted = ?", true)
|
||||
end
|
||||
|
||||
def visible(user)
|
||||
Danbooru.config.can_user_see_post_conditions(user)
|
||||
end
|
||||
|
||||
def commented_before(date)
|
||||
where("last_commented_at < ?", date).order("last_commented_at DESC")
|
||||
end
|
||||
|
||||
def has_notes
|
||||
where("last_noted_at is not null")
|
||||
end
|
||||
|
||||
def for_user(user_id)
|
||||
where("uploader_id = ?", user_id)
|
||||
end
|
||||
|
||||
def available_for_moderation(hidden)
|
||||
if hidden.present?
|
||||
where("id IN (SELECT pd.post_id FROM post_disapprovals pd WHERE pd.user_id = ?)", CurrentUser.id)
|
||||
else
|
||||
where("id NOT IN (SELECT pd.post_id FROM post_disapprovals pd WHERE pd.user_id = ?)", CurrentUser.id)
|
||||
end
|
||||
end
|
||||
|
||||
def hidden_from_moderation
|
||||
where("id IN (SELECT pd.post_id FROM post_disapprovals pd WHERE pd.user_id = ?)", CurrentUser.id)
|
||||
end
|
||||
|
||||
def tag_match(query)
|
||||
PostQueryBuilder.new(query).build
|
||||
end
|
||||
|
||||
def positive
|
||||
where("score > 1")
|
||||
end
|
||||
|
||||
def negative
|
||||
where("score < -1")
|
||||
end
|
||||
|
||||
def updater_name_matches(name)
|
||||
where("updater_id = (select _.id from users _ where lower(_.name) = ?)", name.downcase)
|
||||
end
|
||||
|
||||
def after_id(num)
|
||||
if num.present?
|
||||
where("id > ?", num.to_i).reorder("id asc")
|
||||
else
|
||||
where("true")
|
||||
end
|
||||
end
|
||||
|
||||
def before_id(num)
|
||||
if num.present?
|
||||
where("id < ?", num.to_i).reorder("id desc")
|
||||
else
|
||||
where("true")
|
||||
end
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:before_id]
|
||||
q = q.before_id(params[:before_id].to_i)
|
||||
end
|
||||
|
||||
if params[:after_id]
|
||||
q = q.after_id(params[:after_id].to_i)
|
||||
end
|
||||
|
||||
if params[:tag_match]
|
||||
q = q.tag_match(params[:tag_match])
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
include FileMethods
|
||||
include ImageMethods
|
||||
include ApprovalMethods
|
||||
@@ -883,6 +952,7 @@ class Post < ActiveRecord::Base
|
||||
include VersionMethods
|
||||
include NoteMethods
|
||||
include ApiMethods
|
||||
extend SearchMethods
|
||||
|
||||
def reload(options = nil)
|
||||
super
|
||||
|
||||
@@ -8,8 +8,29 @@ class PostAppeal < ActiveRecord::Base
|
||||
validate :validate_creator_is_not_limited
|
||||
before_validation :initialize_creator, :on => :create
|
||||
validates_uniqueness_of :creator_id, :scope => :post_id, :message => "have already appealed this post"
|
||||
scope :for_user, lambda {|user_id| where(["creator_id = ?", user_id])}
|
||||
scope :recent, lambda {where(["created_at >= ?", 1.day.ago])}
|
||||
|
||||
module SearchMethods
|
||||
def for_user(user_id)
|
||||
where("creator_id = ?", user_id)
|
||||
end
|
||||
|
||||
def recent
|
||||
where("created_at >= ?", 1.day.ago)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:post_id]
|
||||
q = q.where("post_id = ?", params[:post_id].to_i)
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
|
||||
def validate_creator_is_not_limited
|
||||
if appeal_count_for_creator >= Danbooru.config.max_appeals_per_day
|
||||
|
||||
@@ -9,9 +9,33 @@ class PostFlag < ActiveRecord::Base
|
||||
before_validation :initialize_creator, :on => :create
|
||||
validates_uniqueness_of :creator_id, :scope => :post_id, :message => "have already flagged this post"
|
||||
before_save :update_post
|
||||
scope :resolved, where("is_resolved = ?", true)
|
||||
scope :unresolved, where("is_resolved = ?", false)
|
||||
scope :old, lambda {where("created_at <= ?", 3.days.ago)}
|
||||
|
||||
module SearchMethods
|
||||
def resolved
|
||||
where("is_resolved = ?", true)
|
||||
end
|
||||
|
||||
def unresolved
|
||||
where("is_resolved = ?", false)
|
||||
end
|
||||
|
||||
def old
|
||||
where("created_at <= ?", 3.days.ago)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:post_id]
|
||||
q = q.where("post_id = ?", params[:post_id].to_i)
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
|
||||
def update_post
|
||||
post.update_column(:is_flagged, true)
|
||||
|
||||
@@ -2,8 +2,37 @@ class PostVersion < ActiveRecord::Base
|
||||
belongs_to :post
|
||||
belongs_to :updater, :class_name => "User"
|
||||
before_validation :initialize_updater
|
||||
scope :for_user, lambda {|user_id| where("updater_id = ?", user_id)}
|
||||
scope :updater_name_matches, lambda {|name| where("updater_id = (select _.id from users _ where lower(_.name) = ?)", name.downcase)}
|
||||
|
||||
module SearchMethods
|
||||
def for_user(user_id)
|
||||
where("updater_id = ?", user_id)
|
||||
end
|
||||
|
||||
def updater_name(name)
|
||||
where("updater_id = (select _.id from users _ where lower(_.name) = ?)", name.downcase)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:updater_name]
|
||||
q = q.updater_name(params[:updater_name])
|
||||
end
|
||||
|
||||
if params[:updater_id]
|
||||
q = q.where("updater_id = ?", params[:updater_id].to_i)
|
||||
end
|
||||
|
||||
if params[:post_id]
|
||||
q = q.where("post_id = ?", params[:post_id].to_i)
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
|
||||
def self.create_from_post(post)
|
||||
if post.created_at == post.updated_at
|
||||
|
||||
@@ -2,8 +2,6 @@ class Tag < ActiveRecord::Base
|
||||
attr_accessible :category
|
||||
after_save :update_category_cache
|
||||
has_one :wiki_page, :foreign_key => "name", :primary_key => "title"
|
||||
scope :name_matches, lambda {|name| where("name LIKE ? ESCAPE E'\\\\'", name.downcase.to_escaped_for_sql_like)}
|
||||
scope :named, lambda {|name| where("name = ?", TagAlias.to_aliased([name]).join(""))}
|
||||
|
||||
class CategoryMapping
|
||||
Danbooru.config.reverse_tag_category_mapping.each do |value, category|
|
||||
@@ -365,6 +363,42 @@ class Tag < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
module SearchMethods
|
||||
def name_matches(name)
|
||||
where("name LIKE ? ESCAPE E'\\\\'", name.downcase.to_escaped_for_sql_like)
|
||||
end
|
||||
|
||||
def named(name)
|
||||
where("name = ?", TagAlias.to_aliased([name]).join(""))
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:name_matches]
|
||||
q = q.name_matches(params[:name_matches])
|
||||
end
|
||||
|
||||
if params[:category]
|
||||
q = q.where("category = ?", params[:category])
|
||||
end
|
||||
|
||||
case params[:sort]
|
||||
when "count"
|
||||
q = q.order("post_count")
|
||||
|
||||
when "date"
|
||||
q = q.order("created_at")
|
||||
|
||||
else
|
||||
q = q.order("name")
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
extend CountMethods
|
||||
extend ViewCountMethods
|
||||
include CategoryMethods
|
||||
@@ -373,4 +407,5 @@ class Tag < ActiveRecord::Base
|
||||
extend ParseMethods
|
||||
include RelationMethods
|
||||
extend SuggestionMethods
|
||||
extend SearchMethods
|
||||
end
|
||||
|
||||
@@ -7,8 +7,34 @@ class TagAlias < ActiveRecord::Base
|
||||
validates_uniqueness_of :antecedent_name
|
||||
validate :absence_of_transitive_relation
|
||||
belongs_to :creator, :class_name => "User"
|
||||
scope :name_matches, lambda {|name| where("(antecedent_name = ? or consequent_name = ?)", name.downcase, name.downcase)}
|
||||
|
||||
module SearchMethods
|
||||
def name_matches(name)
|
||||
where("(antecedent_name = ? or consequent_name = ?)", name.downcase, name.downcase)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:name_matches]
|
||||
q = q.name_matches(params[:name_matches])
|
||||
end
|
||||
|
||||
if params[:antecedent_name]
|
||||
q = q.where("antecedent_name = ?", params[:antecedent_name])
|
||||
end
|
||||
|
||||
if params[:id]
|
||||
q = q.where("id = ?", params[:id].to_i)
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
|
||||
def self.to_aliased(names)
|
||||
alias_hash = Cache.get_multi(names.flatten, "ta") do |name|
|
||||
ta = TagAlias.find_by_antecedent_name(name)
|
||||
|
||||
@@ -6,7 +6,6 @@ class TagImplication < ActiveRecord::Base
|
||||
validates_presence_of :creator_id
|
||||
validates_uniqueness_of :antecedent_name, :scope => :consequent_name
|
||||
validate :absence_of_circular_relation
|
||||
scope :name_matches, lambda {|name| where("(antecedent_name = ? or consequent_name = ?)", name.downcase, name.downcase)}
|
||||
|
||||
module DescendantMethods
|
||||
extend ActiveSupport::Concern
|
||||
@@ -68,8 +67,34 @@ class TagImplication < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
module SearchMethods
|
||||
def name_matches(name)
|
||||
where("(antecedent_name = ? or consequent_name = ?)", name.downcase, name.downcase)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:id]
|
||||
q = q.where("id = ?", params[:id].to_i)
|
||||
end
|
||||
|
||||
if params[:name_matches]
|
||||
q = q.name_matches(params[:name_matches])
|
||||
end
|
||||
|
||||
if params[:antecedent_name]
|
||||
q = q.where("antecedent_name = ?", params[:antecedent_name])
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
include DescendantMethods
|
||||
include ParentMethods
|
||||
extend SearchMethods
|
||||
|
||||
def initialize_creator
|
||||
self.creator_id = CurrentUser.user.id
|
||||
|
||||
@@ -42,6 +42,11 @@ class TagSubscription < ActiveRecord::Base
|
||||
user.is_moderator? || creator_id == user.id
|
||||
end
|
||||
|
||||
def self.search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
end
|
||||
|
||||
def self.visible_to(user)
|
||||
where("(is_public = TRUE OR creator_id = ? OR ?)", user.id, user.is_moderator?)
|
||||
end
|
||||
|
||||
@@ -12,8 +12,6 @@ class Upload < ActiveRecord::Base
|
||||
before_create :convert_cgi_file
|
||||
after_destroy :delete_temp_file
|
||||
validate :uploader_is_not_limited
|
||||
scope :uploaded_by, lambda {|user_id| where(["uploader_id = ?", user_id])}
|
||||
scope :pending, where(:status => "pending")
|
||||
|
||||
module ValidationMethods
|
||||
def uploader_is_not_limited
|
||||
@@ -149,7 +147,8 @@ class Upload < ActiveRecord::Base
|
||||
end
|
||||
|
||||
Danbooru.resize(source_path, resized_file_path_for(width), width, height, quality)
|
||||
if width == Danbooru.config.small_image_width
|
||||
|
||||
if width == Danbooru.config.small_image_width && Danbooru.config.ssd_path
|
||||
Danbooru.resize(source_path, ssd_file_path, width, height, quality)
|
||||
end
|
||||
end
|
||||
@@ -302,6 +301,31 @@ class Upload < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
module SearchMethods
|
||||
def uploaded_by(user_id)
|
||||
where("uploader_id = ?", user_id)
|
||||
end
|
||||
|
||||
def pending
|
||||
where(:status => "pending")
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:uploader_id]
|
||||
q = q.uploaded_by(params[:uploader_id].to_i)
|
||||
end
|
||||
|
||||
if params[:source]
|
||||
q = q.where("source = ?", params[:source])
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
include ConversionMethods
|
||||
include ValidationMethods
|
||||
include FileMethods
|
||||
@@ -313,6 +337,7 @@ class Upload < ActiveRecord::Base
|
||||
include CgiFileMethods
|
||||
include StatusMethods
|
||||
include UploaderMethods
|
||||
extend SearchMethods
|
||||
|
||||
def presenter
|
||||
@presenter ||= UploadPresenter.new(self)
|
||||
|
||||
@@ -40,10 +40,6 @@ class User < ActiveRecord::Base
|
||||
has_many :note_versions, :foreign_key => "updater_id"
|
||||
has_many :dmails, :foreign_key => "owner_id", :order => "dmails.id desc"
|
||||
belongs_to :inviter, :class_name => "User"
|
||||
scope :named, lambda {|name| where(["lower(name) = ?", name])}
|
||||
scope :admins, where("is_admin = TRUE")
|
||||
scope :with_email, lambda {|email| email.blank? ? where("FALSE") : where(["email = ?", email])}
|
||||
scope :find_for_password_reset, lambda {|name, email| email.blank? ? where("FALSE") : where(["name = ? AND email = ?", name, email])}
|
||||
|
||||
module BanMethods
|
||||
def validate_ip_addr_is_not_banned
|
||||
@@ -417,6 +413,51 @@ class User < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
module SearchMethods
|
||||
def named(name)
|
||||
where("lower(name) = ?", name)
|
||||
end
|
||||
|
||||
def name_matches(name)
|
||||
where("lower(name) like ? escape E'\\\\'", name.to_escaped_for_sql_like)
|
||||
end
|
||||
|
||||
def admins
|
||||
where("is_admin = TRUE")
|
||||
end
|
||||
|
||||
def with_email(email)
|
||||
if email.blank?
|
||||
where("FALSE")
|
||||
else
|
||||
where("email = ?", email)
|
||||
end
|
||||
end
|
||||
|
||||
def find_for_password_reset(name, email)
|
||||
if email.blank?
|
||||
where("FALSE")
|
||||
else
|
||||
where(["name = ? AND email = ?", name, email])
|
||||
end
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:name_matches]
|
||||
q = q.name_matches(params[:name_matches])
|
||||
end
|
||||
|
||||
if params[:min_level]
|
||||
q = q.where("level >= ?", params[:min_level].to_i)
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
include BanMethods
|
||||
include NameMethods
|
||||
include PasswordMethods
|
||||
@@ -429,6 +470,7 @@ class User < ActiveRecord::Base
|
||||
include LimitMethods
|
||||
include InvitationMethods
|
||||
include ApiMethods
|
||||
extend SearchMethods
|
||||
|
||||
def initialize_default_image_size
|
||||
self.default_image_size = "large"
|
||||
|
||||
@@ -6,11 +6,38 @@ class UserFeedback < ActiveRecord::Base
|
||||
attr_accessible :body, :user_id, :category, :user_name
|
||||
validates_presence_of :user, :creator, :body, :category
|
||||
validate :creator_is_privileged
|
||||
scope :positive, where("category = ?", "positive")
|
||||
scope :neutral, where("category = ?", "neutral")
|
||||
scope :negative, where("category = ?", "negative")
|
||||
scope :for_user, lambda {|user_id| where("user_id = ?", user_id)}
|
||||
|
||||
module SearchMethods
|
||||
def positive
|
||||
where("category = ?", "positive")
|
||||
end
|
||||
|
||||
def neutral
|
||||
where("category = ?", "neutral")
|
||||
end
|
||||
|
||||
def negative
|
||||
where("category = ?", "negative")
|
||||
end
|
||||
|
||||
def for_user(user_id)
|
||||
where("user_id = ?", user_id)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:user_id]
|
||||
q = q.for_user(params[:user_id].to_i)
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
|
||||
def initialize_creator
|
||||
self.creator_id = CurrentUser.id
|
||||
end
|
||||
|
||||
@@ -7,26 +7,37 @@ class WikiPage < ActiveRecord::Base
|
||||
validates_presence_of :title
|
||||
validate :validate_locker_is_janitor
|
||||
attr_accessible :title, :body, :is_locked
|
||||
scope :titled, lambda {|title| where(["title = ?", title.downcase.tr(" ", "_")])}
|
||||
scope :recent, order("updated_at DESC").limit(25)
|
||||
has_one :tag, :foreign_key => "name", :primary_key => "title"
|
||||
has_one :artist, :foreign_key => "name", :primary_key => "title"
|
||||
has_many :versions, :class_name => "WikiPageVersion", :dependent => :destroy, :order => "wiki_page_versions.id ASC"
|
||||
|
||||
def self.build_relation(options = {})
|
||||
relation = where()
|
||||
|
||||
if options[:title]
|
||||
relation = relation.where(["title LIKE ? ESCAPE E'\\\\'", options[:title].downcase.tr(" ", "_").to_escaped_for_sql_like])
|
||||
|
||||
module SearchMethods
|
||||
def titled(title)
|
||||
where("title = ?", title.downcase.tr(" ", "_"))
|
||||
end
|
||||
|
||||
if options[:creator_id]
|
||||
relation = relation.where(["creator_id = ?", options[:creator_id]])
|
||||
def recent
|
||||
order("updated_at DESC").limit(25)
|
||||
end
|
||||
|
||||
def search(params = {})
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:title]
|
||||
q = q.where("title LIKE ? ESCAPE E'\\\\'", params[:title].downcase.tr(" ", "_").to_escaped_for_sql_like)
|
||||
end
|
||||
|
||||
if params[:creator_id]
|
||||
q = q.where("creator_id = ?", params[:creator_id])
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
|
||||
relation
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
|
||||
def self.find_title_and_id(title)
|
||||
titled(title).select("title, id").first
|
||||
end
|
||||
|
||||
@@ -1,7 +1,29 @@
|
||||
class WikiPageVersion < ActiveRecord::Base
|
||||
belongs_to :wiki_page
|
||||
belongs_to :updater, :class_name => "User"
|
||||
scope :for_user, lambda {|user_id| where("updater_id = ?", user_id)}
|
||||
|
||||
module SearchMethods
|
||||
def for_user(user_id)
|
||||
where("updater_id = ?", user_id)
|
||||
end
|
||||
|
||||
def search(params)
|
||||
q = scoped
|
||||
return q if params.blank?
|
||||
|
||||
if params[:updater_id]
|
||||
q = q.for_user(params[:updater_id].to_i)
|
||||
end
|
||||
|
||||
if params[:wiki_page_id]
|
||||
q = q.where("wiki_page_id = ?", params[:wiki_page_id].to_i)
|
||||
end
|
||||
|
||||
q
|
||||
end
|
||||
end
|
||||
|
||||
extend SearchMethods
|
||||
|
||||
def updater_name
|
||||
User.id_to_name(updater_id)
|
||||
|
||||
Reference in New Issue
Block a user