From 040059c491562863e1b4a9096e4a93e20bdefeff Mon Sep 17 00:00:00 2001 From: albert Date: Mon, 7 Jan 2013 17:12:43 -0500 Subject: [PATCH] add default scopes to every model --- app/models/artist.rb | 1 + app/models/artist_version.rb | 1 + app/models/ban.rb | 3 ++- app/models/comment.rb | 3 ++- app/models/dmail.rb | 1 + app/models/forum_post.rb | 1 + app/models/forum_topic.rb | 3 ++- app/models/ip_ban.rb | 1 + app/models/janitor_trial.rb | 3 ++- app/models/mod_action.rb | 1 + app/models/note.rb | 1 + app/models/note_version.rb | 1 + app/models/pool.rb | 3 ++- app/models/pool_version.rb | 1 + app/models/post_appeal.rb | 1 + app/models/post_flag.rb | 3 ++- app/models/tag.rb | 3 ++- app/models/tag_alias.rb | 3 ++- app/models/tag_implication.rb | 3 ++- app/models/tag_subscription.rb | 3 ++- app/models/upload.rb | 3 ++- app/models/user.rb | 3 ++- app/models/user_feedback.rb | 3 ++- app/models/wiki_page.rb | 3 ++- app/models/wiki_page_version.rb | 3 ++- lib/danbooru/paginator/active_record_extension.rb | 4 ---- 26 files changed, 40 insertions(+), 19 deletions(-) diff --git a/app/models/artist.rb b/app/models/artist.rb index 573c9a511..bdae643ee 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -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 diff --git a/app/models/artist_version.rb b/app/models/artist_version.rb index d4c71129a..93d95916e 100644 --- a/app/models/artist_version.rb +++ b/app/models/artist_version.rb @@ -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("_", " ") diff --git a/app/models/ban.rb b/app/models/ban.rb index e9da57f77..52118793c 100644 --- a/app/models/ban.rb +++ b/app/models/ban.rb @@ -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 diff --git a/app/models/comment.rb b/app/models/comment.rb index 61420f178..191469df1 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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)} diff --git a/app/models/dmail.rb b/app/models/dmail.rb index 3dce7fd75..f8978be18 100644 --- a/app/models/dmail.rb +++ b/app/models/dmail.rb @@ -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 diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index afc318856..79313b000 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -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] diff --git a/app/models/forum_topic.rb b/app/models/forum_topic.rb index 533cbf3f7..e843186c7 100644 --- a/app/models/forum_topic.rb +++ b/app/models/forum_topic.rb @@ -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 diff --git a/app/models/ip_ban.rb b/app/models/ip_ban.rb index 6548e8aa2..24ef7f742 100644 --- a/app/models/ip_ban.rb +++ b/app/models/ip_ban.rb @@ -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]) diff --git a/app/models/janitor_trial.rb b/app/models/janitor_trial.rb index a7e0e42a3..24e32e44c 100644 --- a/app/models/janitor_trial.rb +++ b/app/models/janitor_trial.rb @@ -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 diff --git a/app/models/mod_action.rb b/app/models/mod_action.rb index 529aac5ff..d06f1f2ba 100644 --- a/app/models/mod_action.rb +++ b/app/models/mod_action.rb @@ -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 diff --git a/app/models/note.rb b/app/models/note.rb index bd7e797fb..73383e52b 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -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) diff --git a/app/models/note_version.rb b/app/models/note_version.rb index 96076ac5e..2f4f7425e 100644 --- a/app/models/note_version.rb +++ b/app/models/note_version.rb @@ -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 diff --git a/app/models/pool.rb b/app/models/pool.rb index 22a5266cb..4e1db5b63 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -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 diff --git a/app/models/pool_version.rb b/app/models/pool_version.rb index c85cf236e..9d86a0b42 100644 --- a/app/models/pool_version.rb +++ b/app/models/pool_version.rb @@ -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 diff --git a/app/models/post_appeal.rb b/app/models/post_appeal.rb index 6c838e036..8b3c31f99 100644 --- a/app/models/post_appeal.rb +++ b/app/models/post_appeal.rb @@ -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 diff --git a/app/models/post_flag.rb b/app/models/post_flag.rb index 38bc6b2b0..b57f079c0 100644 --- a/app/models/post_flag.rb +++ b/app/models/post_flag.rb @@ -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 diff --git a/app/models/tag.rb b/app/models/tag.rb index 9f89a61f3..317d9bb21 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -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 diff --git a/app/models/tag_alias.rb b/app/models/tag_alias.rb index 8895a742f..a3e73543a 100644 --- a/app/models/tag_alias.rb +++ b/app/models/tag_alias.rb @@ -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) diff --git a/app/models/tag_implication.rb b/app/models/tag_implication.rb index 3f0d7ae3c..28fbe65af 100644 --- a/app/models/tag_implication.rb +++ b/app/models/tag_implication.rb @@ -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 diff --git a/app/models/tag_subscription.rb b/app/models/tag_subscription.rb index cff516b3f..4d14bc9ab 100644 --- a/app/models/tag_subscription.rb +++ b/app/models/tag_subscription.rb @@ -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 diff --git a/app/models/upload.rb b/app/models/upload.rb index 0f30cd71a..f4e63c6c1 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -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? diff --git a/app/models/user.rb b/app/models/user.rb index 7242ec4aa..303cc35c4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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) diff --git a/app/models/user_feedback.rb b/app/models/user_feedback.rb index 465409afa..ff31cd6cb 100644 --- a/app/models/user_feedback.rb +++ b/app/models/user_feedback.rb @@ -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 diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index f05635f03..f7ba826a2 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -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() diff --git a/app/models/wiki_page_version.rb b/app/models/wiki_page_version.rb index 75e5f01fa..96e17be36 100644 --- a/app/models/wiki_page_version.rb +++ b/app/models/wiki_page_version.rb @@ -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 diff --git a/lib/danbooru/paginator/active_record_extension.rb b/lib/danbooru/paginator/active_record_extension.rb index 03fed12af..ca5da06f6 100644 --- a/lib/danbooru/paginator/active_record_extension.rb +++ b/lib/danbooru/paginator/active_record_extension.rb @@ -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