Files
danbooru/app/models/post_vote.rb
evazion ee4516f5fe searchable: refactor searchable_includes.
Pass searchable associations directly to search_attributes instead of
defining them separately in searchable_includes.
2020-12-16 23:57:07 -06:00

56 lines
1.4 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: [1, -1]
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.visible(user)
user.is_admin? ? all : where(user: user)
end
def self.search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :score, :user, :post)
q.apply_default_order(params)
end
def initialize_attributes
self.user_id ||= CurrentUser.id
if vote == "up"
self.score = 1
elsif vote == "down"
self.score = -1
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 self.available_includes
[:user, :post]
end
end