Files
danbooru/app/models/post_vote.rb
evazion 0ad42d23c9 models: refactor search visibility methods.
Refactor how model visibility works in index actions:

* Call `visible` in the controller instead of in model `search`
  methods. This decouples model visibility from model searching.

* Explicitly pass CurrentUser when calling `visible`. This reduces
  hidden dependencies on the current user inside models.

* Standardize on calling the method `visible`. In some places it was
  called `permitted` instead.

* Add a `visible` base method to ApplicationModel.
2020-02-19 17:08:59 -06:00

77 lines
1.9 KiB
Ruby

class PostVote < ApplicationRecord
class Error < StandardError; end
belongs_to :post
belongs_to :user
attr_accessor :vote
after_initialize :initialize_attributes, if: :new_record?
validates_presence_of :score
validates_inclusion_of :score, :in => [SuperVoter::MAGNITUDE, 1, -1, -SuperVoter::MAGNITUDE]
after_create :update_post_on_create
after_destroy :update_post_on_destroy
scope :positive, -> { where("post_votes.score > 0") }
scope :negative, -> { where("post_votes.score < 0") }
def self.positive_user_ids
positive.group(:user_id).having("count(*) > 100").pluck(:user_id)
end
def self.negative_post_ids(user_id)
negative.where(user_id: user_id).pluck(:post_id)
end
def self.positive_post_ids(user_id)
positive.where(user_id: user_id).pluck(:post_id)
end
def self.visible(user)
user.is_admin? ? all : where(user: user)
end
def self.search(params)
q = super
q = q.search_attributes(params, :post, :user, :score)
q.apply_default_order(params)
end
def initialize_attributes
self.user_id ||= CurrentUser.user.id
if vote == "up"
self.score = magnitude
elsif vote == "down"
self.score = -magnitude
end
end
def update_post_on_create
if score > 0
Post.where(:id => post_id).update_all("score = score + #{score}, up_score = up_score + #{score}")
else
Post.where(:id => post_id).update_all("score = score + #{score}, down_score = down_score + #{score}")
end
end
def update_post_on_destroy
if score > 0
Post.where(:id => post_id).update_all("score = score - #{score}, up_score = up_score - #{score}")
else
Post.where(:id => post_id).update_all("score = score - #{score}, down_score = down_score - #{score}")
end
end
def magnitude
if user.is_super_voter?
SuperVoter::MAGNITUDE
else
1
end
end
def self.available_includes
[:user, :post]
end
end