add default scopes to every model

This commit is contained in:
albert
2013-01-07 17:12:43 -05:00
parent 257c0a081e
commit 040059c491
26 changed files with 40 additions and 19 deletions

View File

@@ -18,6 +18,7 @@ class Artist < ActiveRecord::Base
scope :other_names_match, lambda {|string| where(["other_names_index @@ to_tsquery('danbooru', ?)", Artist.normalize_name(string)])}
scope :name_equals, lambda {|string| where("name = ?", string)}
search_methods :url_match, :other_names_match
default_scope limit(1)
module UrlMethods
extend ActiveSupport::Concern

View File

@@ -1,6 +1,7 @@
class ArtistVersion < ActiveRecord::Base
belongs_to :updater
belongs_to :artist
default_scope limit(1)
def updater_name
User.id_to_name(updater_id).tr("_", " ")

View File

@@ -6,7 +6,8 @@ class Ban < ActiveRecord::Base
validate :user_is_inferior
validates_presence_of :user_id, :reason, :duration
before_validation :initialize_banner_id, :on => :create
default_scope limit(1)
def self.is_banned?(user)
exists?(["user_id = ? AND expires_at > ?", user.id, Time.now])
end

View File

@@ -8,7 +8,8 @@ class Comment < ActiveRecord::Base
after_save :update_last_commented_at
attr_accessible :body, :post_id
attr_accessor :do_not_bump_post
default_scope limit(1)
scope :recent, :order => "comments.id desc", :limit => 6
scope :body_matches, lambda {|query| where("body_index @@ plainto_tsquery(?)", query).order("comments.id DESC")}
scope :hidden, lambda {|user| where("score < ?", user.comment_threshold)}

View File

@@ -21,6 +21,7 @@ class Dmail < ActiveRecord::Base
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)}
search_method :to_name_matches, :from_name_matches
default_scope limit(1)
module AddressMethods
def to_name

View File

@@ -14,6 +14,7 @@ class ForumPost < ActiveRecord::Base
scope :for_user, lambda {|user_id| where("forum_posts.creator_id = ?", user_id)}
scope :active, where("is_deleted = false")
search_methods :body_matches
default_scope limit(1)
def self.new_reply(params)
if params[:topic_id]

View File

@@ -14,7 +14,8 @@ class ForumTopic < ActiveRecord::Base
scope :active, where("is_deleted = false")
search_methods :title_matches
accepts_nested_attributes_for :original_post
default_scope limit(1)
def editable_by?(user)
creator_id == user.id || user.is_moderator?
end

View File

@@ -3,6 +3,7 @@ class IpBan < ActiveRecord::Base
before_validation :initialize_creator, :on => :create
validates_presence_of :reason, :creator
validates_uniqueness_of :ip_addr
default_scope limit(1)
def self.is_banned?(ip_addr)
exists?(["ip_addr = ?", ip_addr])

View File

@@ -6,7 +6,8 @@ class JanitorTrial < ActiveRecord::Base
after_destroy :create_feedback
validates_presence_of :user
before_validation :initialize_creator
default_scope limit(1)
def initialize_creator
self.creator_id = CurrentUser.id
end

View File

@@ -1,6 +1,7 @@
class ModAction < ActiveRecord::Base
belongs_to :creator, :class_name => "User"
before_validation :initialize_creator, :on => :create
default_scope limit(1)
def initialize_creator
self.creator_id = CurrentUser.id

View File

@@ -16,6 +16,7 @@ class Note < ActiveRecord::Base
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)}
search_methods :body_matches, :post_tag_match
default_scope limit(1)
def presenter
@presenter ||= NotePresenter.new(self)

View File

@@ -2,6 +2,7 @@ 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)}
default_scope limit(1)
def initialize_updater
self.updater_id = CurrentUser.id

View File

@@ -15,7 +15,8 @@ class Pool < ActiveRecord::Base
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")
default_scope limit(1)
def self.name_to_id(name)
if name =~ /^\d+$/
name.to_i

View File

@@ -6,6 +6,7 @@ class PoolVersion < ActiveRecord::Base
belongs_to :updater, :class_name => "User"
before_validation :initialize_updater
scope :for_user, lambda {|user_id| where("updater_id = ?", user_id)}
default_scope limit(1)
def initialize_updater
self.updater_id = CurrentUser.id

View File

@@ -10,6 +10,7 @@ class PostAppeal < ActiveRecord::Base
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])}
default_scope limit(1)
def validate_creator_is_not_limited
if appeal_count_for_creator >= Danbooru.config.max_appeals_per_day

View File

@@ -12,7 +12,8 @@ class PostFlag < ActiveRecord::Base
scope :resolved, where("is_resolved = ?", true)
scope :unresolved, where("is_resolved = ?", false)
scope :old, lambda {where("created_at <= ?", 3.days.ago)}
default_scope limit(1)
def update_post
post.update_column(:is_flagged, true)
end

View File

@@ -5,7 +5,8 @@ class Tag < ActiveRecord::Base
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(""))}
search_methods :name_matches
default_scope limit(1)
class CategoryMapping
Danbooru.config.reverse_tag_category_mapping.each do |value, category|
define_method(category.downcase) do

View File

@@ -9,7 +9,8 @@ class TagAlias < ActiveRecord::Base
belongs_to :creator, :class_name => "User"
scope :name_matches, lambda {|name| where("(antecedent_name = ? or consequent_name = ?)", name.downcase, name.downcase)}
search_method :name_matches
default_scope limit(1)
def self.to_aliased(names)
alias_hash = Cache.get_multi(names.flatten, "ta") do |name|
ta = TagAlias.find_by_antecedent_name(name)

View File

@@ -8,7 +8,8 @@ class TagImplication < ActiveRecord::Base
validate :absence_of_circular_relation
scope :name_matches, lambda {|name| where("(antecedent_name = ? or consequent_name = ?)", name.downcase, name.downcase)}
search_method :name_matches
default_scope limit(1)
module DescendantMethods
extend ActiveSupport::Concern

View File

@@ -6,7 +6,8 @@ class TagSubscription < ActiveRecord::Base
before_save :limit_tag_count
attr_accessible :name, :tag_query, :post_ids, :is_visible_on_profile
validates_presence_of :name, :tag_query, :is_public, :creator_id
default_scope limit(1)
def normalize_name
self.name = name.gsub(/\W/, "_")
end

View File

@@ -14,7 +14,8 @@ class Upload < ActiveRecord::Base
validate :uploader_is_not_limited
scope :uploaded_by, lambda {|user_id| where(["uploader_id = ?", user_id])}
scope :pending, where(:status => "pending")
default_scope limit(1)
module ValidationMethods
def uploader_is_not_limited
if !uploader.can_upload?

View File

@@ -45,7 +45,8 @@ class User < ActiveRecord::Base
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])}
search_method :named
default_scope limit(1)
module BanMethods
def validate_ip_addr_is_not_banned
if IpBan.is_banned?(CurrentUser.ip_addr)

View File

@@ -10,7 +10,8 @@ class UserFeedback < ActiveRecord::Base
scope :neutral, where("category = ?", "neutral")
scope :negative, where("category = ?", "negative")
scope :for_user, lambda {|user_id| where("user_id = ?", user_id)}
default_scope limit(1)
def initialize_creator
self.creator_id = CurrentUser.id
end

View File

@@ -12,7 +12,8 @@ class WikiPage < ActiveRecord::Base
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"
default_scope limit(1)
def self.build_relation(options = {})
relation = where()

View File

@@ -2,7 +2,8 @@ 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)}
default_scope limit(1)
def updater_name
User.id_to_name(updater_id)
end

View File

@@ -5,10 +5,6 @@ module Danbooru
module ActiveRecordExtension
extend ActiveSupport::Concern
included do
default_scope limit(1)
end
module ClassMethods
def paginate(page, options = {})
@paginator_options = options