models: remove belongs_to_creator macro.
The belongs_to_creator macro was used to initialize the creator_id field to the CurrentUser. This made tests complicated because it meant you had to create and set the current user every time you wanted to create an object, when lead to the current user being set over and over again. It also meant you had to constantly be aware of what the CurrentUser was in many different contexts, which was often confusing. Setting creators explicitly simplifies everything greatly.
This commit is contained in:
@@ -373,18 +373,6 @@ class ApplicationRecord < ActiveRecord::Base
|
||||
|
||||
concerning :UserMethods do
|
||||
class_methods do
|
||||
def belongs_to_creator(options = {})
|
||||
class_eval do
|
||||
belongs_to :creator, options.merge(class_name: "User")
|
||||
before_validation(on: :create) do |rec|
|
||||
if rec.creator_id.nil?
|
||||
rec.creator_id = CurrentUser.id
|
||||
rec.creator_ip_addr = CurrentUser.ip_addr if rec.respond_to?(:creator_ip_addr=)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def belongs_to_updater(options = {})
|
||||
class_eval do
|
||||
belongs_to :updater, options.merge(class_name: "User")
|
||||
|
||||
@@ -13,7 +13,7 @@ class Artist < ApplicationRecord
|
||||
after_save :clear_url_string_changed
|
||||
validate :validate_tag_category
|
||||
validates :name, tag_name: true, uniqueness: true
|
||||
belongs_to_creator
|
||||
belongs_to :creator, class_name: "User"
|
||||
has_many :members, :class_name => "Artist", :foreign_key => "group_name", :primary_key => "name"
|
||||
has_many :urls, :dependent => :destroy, :class_name => "ArtistUrl", :autosave => true
|
||||
has_many :versions, -> {order("artist_versions.id ASC")}, :class_name => "ArtistVersion"
|
||||
@@ -418,15 +418,15 @@ class Artist < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def ban!
|
||||
def ban!(banner: CurrentUser.user)
|
||||
Post.transaction do
|
||||
CurrentUser.without_safe_mode do
|
||||
Post.tag_match(name).each(&:ban!)
|
||||
|
||||
# potential race condition but unlikely
|
||||
unless TagImplication.where(:antecedent_name => name, :consequent_name => "banned_artist").exists?
|
||||
tag_implication = TagImplication.create!(:antecedent_name => name, :consequent_name => "banned_artist", :skip_secondary_validations => true)
|
||||
tag_implication.approve!(approver: CurrentUser.user)
|
||||
tag_implication = TagImplication.create!(antecedent_name: name, consequent_name: "banned_artist", skip_secondary_validations: true, creator: banner)
|
||||
tag_implication.approve!(approver: banner)
|
||||
end
|
||||
|
||||
update_column(:is_banned, true)
|
||||
|
||||
@@ -15,7 +15,7 @@ class BulkUpdateRequest < ApplicationRecord
|
||||
validate :validate_script, :on => :create
|
||||
before_validation :initialize_attributes, :on => :create
|
||||
before_validation :normalize_text
|
||||
after_create :create_forum_topic
|
||||
before_create :create_forum_topic
|
||||
after_save :update_notice
|
||||
|
||||
scope :pending_first, -> { order(Arel.sql("(case status when 'pending' then 0 when 'approved' then 1 else 2 end)")) }
|
||||
@@ -94,12 +94,9 @@ class BulkUpdateRequest < ApplicationRecord
|
||||
end
|
||||
|
||||
def create_forum_topic
|
||||
if forum_topic_id
|
||||
forum_post = forum_topic.posts.create(body: reason_with_link)
|
||||
update(forum_post_id: forum_post.id)
|
||||
else
|
||||
forum_topic = ForumTopic.create(title: title, category_id: 1, original_post_attributes: {body: reason_with_link})
|
||||
update(forum_topic_id: forum_topic.id, forum_post_id: forum_topic.posts.first.id)
|
||||
CurrentUser.as(user) do
|
||||
self.forum_topic = ForumTopic.create(title: title, category_id: 1, creator: user) unless forum_topic.present?
|
||||
self.forum_post = forum_topic.posts.create(body: reason_with_link, creator: user) unless forum_post.present?
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ class Comment < ApplicationRecord
|
||||
validate :validate_comment_is_not_spam, on: :create
|
||||
validates_presence_of :body, :message => "has no content"
|
||||
belongs_to :post
|
||||
belongs_to_creator
|
||||
belongs_to :creator, class_name: "User"
|
||||
belongs_to_updater
|
||||
has_many :moderation_reports, as: :model
|
||||
has_many :votes, :class_name => "CommentVote", :dependent => :destroy
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
class FavoriteGroup < ApplicationRecord
|
||||
validates_uniqueness_of :name, :case_sensitive => false, :scope => :creator_id
|
||||
validates_format_of :name, :with => /\A[^,]+\Z/, :message => "cannot have commas"
|
||||
belongs_to_creator
|
||||
belongs_to :creator, class_name: "User"
|
||||
before_validation :normalize_name
|
||||
before_validation :strip_name
|
||||
validate :creator_can_create_favorite_groups, :on => :create
|
||||
@@ -45,9 +45,9 @@ class FavoriteGroup < ApplicationRecord
|
||||
extend SearchMethods
|
||||
|
||||
def creator_can_create_favorite_groups
|
||||
if creator.favorite_group_count >= creator.favorite_group_limit
|
||||
if creator.favorite_groups.count >= creator.favorite_group_limit
|
||||
error = "You can only keep up to #{creator.favorite_group_limit} favorite groups."
|
||||
if !CurrentUser.user.is_platinum?
|
||||
if !creator.is_platinum?
|
||||
error += " Upgrade your account to create more."
|
||||
end
|
||||
self.errors.add(:base, error)
|
||||
|
||||
@@ -2,7 +2,7 @@ class ForumPost < ApplicationRecord
|
||||
include Mentionable
|
||||
|
||||
attr_readonly :topic_id
|
||||
belongs_to_creator
|
||||
belongs_to :creator, class_name: "User"
|
||||
belongs_to_updater
|
||||
belongs_to :topic, :class_name => "ForumTopic"
|
||||
has_many :dtext_links, as: :model, dependent: :destroy
|
||||
@@ -113,7 +113,7 @@ class ForumPost < ApplicationRecord
|
||||
end
|
||||
|
||||
def validate_topic_is_unlocked
|
||||
return if CurrentUser.is_moderator?
|
||||
return if creator.is_moderator?
|
||||
return if topic.nil?
|
||||
|
||||
if topic.is_locked?
|
||||
@@ -139,7 +139,7 @@ class ForumPost < ApplicationRecord
|
||||
def update_topic_updated_at_on_create
|
||||
if topic
|
||||
# need to do this to bypass the topic's original post from getting touched
|
||||
ForumTopic.where(:id => topic.id).update_all(["updater_id = ?, response_count = response_count + 1, updated_at = ?", CurrentUser.id, Time.now])
|
||||
ForumTopic.where(:id => topic.id).update_all(["updater_id = ?, response_count = response_count + 1, updated_at = ?", creator.id, Time.now])
|
||||
topic.response_count += 1
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class ForumPostVote < ApplicationRecord
|
||||
belongs_to_creator
|
||||
belongs_to :creator, class_name: "User"
|
||||
belongs_to :forum_post
|
||||
validates :creator_id, uniqueness: {scope: :forum_post_id}
|
||||
validates :score, inclusion: {in: [-1, 0, 1]}
|
||||
|
||||
@@ -11,7 +11,7 @@ class ForumTopic < ApplicationRecord
|
||||
Admin: User::Levels::ADMIN
|
||||
}
|
||||
|
||||
belongs_to_creator
|
||||
belongs_to :creator, class_name: "User"
|
||||
belongs_to_updater
|
||||
has_many :posts, -> {order("forum_posts.id asc")}, :class_name => "ForumPost", :foreign_key => "topic_id", :dependent => :destroy
|
||||
has_many :moderation_reports, through: :posts
|
||||
@@ -180,7 +180,7 @@ class ForumTopic < ApplicationRecord
|
||||
end
|
||||
|
||||
def update_orignal_post
|
||||
original_post&.update_columns(:updater_id => CurrentUser.id, :updated_at => Time.now)
|
||||
original_post&.update_columns(:updater_id => updater.id, :updated_at => Time.now)
|
||||
end
|
||||
|
||||
def viewable_moderation_reports
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
class IpBan < ApplicationRecord
|
||||
belongs_to_creator
|
||||
belongs_to :creator, class_name: "User"
|
||||
validate :validate_ip_addr
|
||||
validates_presence_of :reason
|
||||
validates_uniqueness_of :ip_addr
|
||||
after_create { ModAction.log("#{CurrentUser.name} created ip ban for #{ip_addr}", :ip_ban_create) }
|
||||
after_destroy { ModAction.log("#{CurrentUser.name} deleted ip ban for #{ip_addr}", :ip_ban_delete) }
|
||||
after_create { ModAction.log("#{creator.name} created ip ban for #{ip_addr}", :ip_ban_create) }
|
||||
after_destroy { ModAction.log("#{creator.name} deleted ip ban for #{ip_addr}", :ip_ban_delete) }
|
||||
|
||||
def self.is_banned?(ip_addr)
|
||||
where("ip_addr >>= ?", ip_addr).exists?
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class NewsUpdate < ApplicationRecord
|
||||
belongs_to_creator
|
||||
belongs_to :creator, class_name: "User"
|
||||
belongs_to_updater
|
||||
scope :recent, -> {where("created_at >= ?", 2.weeks.ago).order("created_at desc").limit(5)}
|
||||
end
|
||||
|
||||
@@ -3,7 +3,7 @@ class Note < ApplicationRecord
|
||||
|
||||
attr_accessor :html_id
|
||||
belongs_to :post
|
||||
belongs_to_creator
|
||||
belongs_to :creator, class_name: "User"
|
||||
has_many :versions, -> {order("note_versions.id ASC")}, :class_name => "NoteVersion", :dependent => :destroy
|
||||
validates_presence_of :x, :y, :width, :height, :body
|
||||
validate :note_within_image
|
||||
@@ -129,8 +129,9 @@ class Note < ApplicationRecord
|
||||
save!
|
||||
end
|
||||
|
||||
def copy_to(new_post)
|
||||
def copy_to(new_post, creator: CurrentUser.user)
|
||||
new_note = dup
|
||||
new_note.creator = creator
|
||||
new_note.post_id = new_post.id
|
||||
new_note.version = 0
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ class Pool < ApplicationRecord
|
||||
POOL_ORDER_LIMIT = 1000
|
||||
|
||||
array_attribute :post_ids, parse: /\d+/, cast: :to_i
|
||||
belongs_to_creator
|
||||
belongs_to :creator, class_name: "User"
|
||||
|
||||
validates_uniqueness_of :name, case_sensitive: false, if: :name_changed?
|
||||
validate :validate_name, if: :name_changed?
|
||||
|
||||
@@ -268,8 +268,8 @@ class Post < ApplicationRecord
|
||||
!is_status_locked? && (is_pending? || is_flagged? || is_deleted?) && uploader != user && !approved_by?(user)
|
||||
end
|
||||
|
||||
def flag!(reason, options = {})
|
||||
flag = flags.create(:reason => reason, :is_resolved => false, :is_deletion => options[:is_deletion])
|
||||
def flag!(reason, is_deletion: false)
|
||||
flag = flags.create(reason: reason, is_resolved: false, is_deletion: is_deletion, creator: CurrentUser.user)
|
||||
|
||||
if flag.errors.any?
|
||||
raise PostFlag::Error.new(flag.errors.full_messages.join("; "))
|
||||
@@ -746,7 +746,7 @@ class Post < ApplicationRecord
|
||||
when /^newpool:(.+)$/i
|
||||
pool = Pool.find_by_name($1)
|
||||
if pool.nil?
|
||||
pool = Pool.create(:name => $1, :description => "This pool was automatically generated")
|
||||
pool = Pool.create(name: $1, creator: CurrentUser.user, description: "This pool was automatically generated")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,7 +8,6 @@ class PostAppeal < ApplicationRecord
|
||||
validates_presence_of :reason
|
||||
validate :validate_post_is_inactive
|
||||
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"
|
||||
|
||||
api_attributes including: [:is_resolved]
|
||||
@@ -60,10 +59,6 @@ class PostAppeal < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def initialize_creator
|
||||
self.creator_id = CurrentUser.id
|
||||
end
|
||||
|
||||
def appeal_count_for_creator
|
||||
creator.post_appeals.recent.count
|
||||
end
|
||||
|
||||
@@ -9,7 +9,7 @@ class PostFlag < ApplicationRecord
|
||||
|
||||
COOLDOWN_PERIOD = 3.days
|
||||
|
||||
belongs_to_creator :class_name => "User"
|
||||
belongs_to :creator, class_name: "User"
|
||||
belongs_to :post
|
||||
validates_presence_of :reason
|
||||
validate :validate_creator_is_not_limited, on: :create
|
||||
@@ -124,7 +124,7 @@ class PostFlag < ApplicationRecord
|
||||
def validate_creator_is_not_limited
|
||||
return if is_deletion
|
||||
|
||||
if CurrentUser.can_approve_posts?
|
||||
if creator.can_approve_posts?
|
||||
# do nothing
|
||||
elsif creator.created_at > 1.week.ago
|
||||
errors[:creator] << "cannot flag within the first week of sign up"
|
||||
|
||||
@@ -7,7 +7,7 @@ class TagRelationship < ApplicationRecord
|
||||
|
||||
attr_accessor :skip_secondary_validations
|
||||
|
||||
belongs_to_creator
|
||||
belongs_to :creator, class_name: "User"
|
||||
belongs_to :approver, class_name: "User", optional: true
|
||||
belongs_to :forum_post, optional: true
|
||||
belongs_to :forum_topic, optional: true
|
||||
@@ -24,7 +24,6 @@ class TagRelationship < ApplicationRecord
|
||||
scope :pending, -> {where(status: "pending")}
|
||||
scope :retired, -> {where(status: "retired")}
|
||||
|
||||
before_validation :initialize_creator, :on => :create
|
||||
before_validation :normalize_names
|
||||
validates_format_of :status, :with => /\A(active|deleted|pending|processing|queued|retired|error: .*)\Z/
|
||||
validates_presence_of :antecedent_name, :consequent_name
|
||||
@@ -33,10 +32,6 @@ class TagRelationship < ApplicationRecord
|
||||
validate :antecedent_and_consequent_are_different
|
||||
after_save :update_notice
|
||||
|
||||
def initialize_creator
|
||||
self.creator_id = CurrentUser.user.id
|
||||
end
|
||||
|
||||
def normalize_names
|
||||
self.antecedent_name = antecedent_name.mb_chars.downcase.tr(" ", "_")
|
||||
self.consequent_name = consequent_name.mb_chars.downcase.tr(" ", "_")
|
||||
|
||||
@@ -86,6 +86,7 @@ class User < ApplicationRecord
|
||||
before_update :encrypt_password_on_update
|
||||
before_create :promote_to_admin_if_first_user
|
||||
before_create :customize_new_user
|
||||
has_many :artists, foreign_key: :creator_id
|
||||
has_many :artist_versions, foreign_key: :updater_id
|
||||
has_many :artist_commentary_versions, foreign_key: :updater_id
|
||||
has_many :comments, foreign_key: :creator_id
|
||||
@@ -94,6 +95,7 @@ class User < ApplicationRecord
|
||||
has_many :feedback, :class_name => "UserFeedback", :dependent => :destroy
|
||||
has_many :forum_post_votes, dependent: :destroy, foreign_key: :creator_id
|
||||
has_many :moderation_reports, as: :model
|
||||
has_many :pools, foreign_key: :creator_id
|
||||
has_many :posts, :foreign_key => "uploader_id"
|
||||
has_many :post_appeals, foreign_key: :creator_id
|
||||
has_many :post_approvals, :dependent => :destroy
|
||||
@@ -108,6 +110,7 @@ class User < ApplicationRecord
|
||||
has_one :dmail_filter
|
||||
has_one :super_voter
|
||||
has_one :token_bucket
|
||||
has_many :notes, foreign_key: :creator_id
|
||||
has_many :note_versions, :foreign_key => "updater_id"
|
||||
has_many :dmails, -> {order("dmails.id desc")}, :foreign_key => "owner_id"
|
||||
has_many :saved_searches
|
||||
@@ -115,6 +118,9 @@ class User < ApplicationRecord
|
||||
has_many :user_name_change_requests, -> {visible.order("user_name_change_requests.created_at desc")}
|
||||
has_many :favorite_groups, -> {order(name: :asc)}, foreign_key: :creator_id
|
||||
has_many :favorites, ->(rec) {where("user_id % 100 = #{rec.id % 100} and user_id = #{rec.id}").order("id desc")}
|
||||
has_many :ip_bans, foreign_key: :creator_id
|
||||
has_many :tag_aliases, foreign_key: :creator_id
|
||||
has_many :tag_implications, foreign_key: :creator_id
|
||||
belongs_to :inviter, class_name: "User", optional: true
|
||||
accepts_nested_attributes_for :dmail_filter
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
class UserFeedback < ApplicationRecord
|
||||
self.table_name = "user_feedback"
|
||||
belongs_to :user
|
||||
belongs_to_creator
|
||||
belongs_to :creator, class_name: "User"
|
||||
attr_accessor :disable_dmail_notification
|
||||
validates_presence_of :body, :category
|
||||
validates_inclusion_of :category, :in => %w(positive negative neutral)
|
||||
|
||||
Reference in New Issue
Block a user