2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-08-06 12:58:24 -04:00
|
|
|
class PoolVersion < ApplicationRecord
|
2019-06-04 08:56:20 -04:00
|
|
|
user_status_counter :pool_edit_count, foreign_key: :updater_id
|
2017-01-19 04:51:53 -05:00
|
|
|
belongs_to :updater, :class_name => "User"
|
2019-05-10 21:19:23 -04:00
|
|
|
before_validation :fill_version, on: :create
|
|
|
|
before_validation :fill_changes, on: :create
|
2017-01-19 04:51:53 -05:00
|
|
|
|
2016-12-14 21:09:45 -05:00
|
|
|
module SearchMethods
|
2018-02-08 17:53:02 -05:00
|
|
|
def default_order
|
|
|
|
order(updated_at: :desc)
|
|
|
|
end
|
|
|
|
|
2016-12-14 21:09:45 -05:00
|
|
|
def for_user(user_id)
|
|
|
|
where("updater_id = ?", user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def search(params)
|
2017-12-17 17:58:34 -05:00
|
|
|
q = super
|
2016-12-14 21:09:45 -05:00
|
|
|
|
2023-08-03 16:01:53 -04:00
|
|
|
q = q.where_user(:updater_id, :updater, params)
|
2016-12-14 21:09:45 -05:00
|
|
|
|
|
|
|
if params[:pool_id].present?
|
2017-05-14 13:38:39 -04:00
|
|
|
q = q.where(pool_id: params[:pool_id].split(",").map(&:to_i))
|
2016-12-14 21:09:45 -05:00
|
|
|
end
|
|
|
|
|
2021-10-30 13:46:55 -04:00
|
|
|
if params[:ip_addr].present?
|
|
|
|
q = q.where("updater_ip_addr <<= ?", params[:ip_addr])
|
|
|
|
end
|
|
|
|
|
2023-07-07 08:32:57 -04:00
|
|
|
q.apply_basic_order(params)
|
2016-12-14 21:09:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
extend SearchMethods
|
|
|
|
|
2019-05-10 21:19:23 -04:00
|
|
|
def self.queue(pool, updater, updater_ip_addr)
|
|
|
|
self.create({
|
|
|
|
pool_id: pool.id,
|
|
|
|
post_ids: pool.post_ids,
|
|
|
|
updater_id: updater.id,
|
|
|
|
updater_ip_addr: updater_ip_addr,
|
|
|
|
description: pool.description,
|
|
|
|
name: pool.name,
|
|
|
|
is_active: pool.is_active?,
|
|
|
|
category: pool.category
|
|
|
|
})
|
2016-12-14 21:09:45 -05:00
|
|
|
end
|
|
|
|
|
2019-05-10 21:19:23 -04:00
|
|
|
def self.calculate_version(pool_id)
|
|
|
|
1 + where("pool_id = ?", pool_id).maximum(:version).to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def fill_version
|
2022-08-06 12:58:24 -04:00
|
|
|
self.version = PoolVersion.calculate_version(self.pool_id)
|
2019-05-10 21:19:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def fill_changes
|
2024-02-25 08:14:30 -05:00
|
|
|
if previous
|
|
|
|
self.added_post_ids = post_ids - previous.post_ids
|
|
|
|
self.removed_post_ids = previous.post_ids - post_ids
|
2017-02-14 19:03:19 -05:00
|
|
|
else
|
2024-02-25 08:14:30 -05:00
|
|
|
self.added_post_ids = post_ids
|
|
|
|
self.removed_post_ids = []
|
2017-02-14 19:03:19 -05:00
|
|
|
end
|
|
|
|
|
2024-02-25 08:14:30 -05:00
|
|
|
self.description_changed = previous.nil? ? true : description != previous.description
|
|
|
|
self.name_changed = previous.nil? ? true : name != previous.name
|
2017-02-14 19:03:19 -05:00
|
|
|
end
|
|
|
|
|
2017-01-03 17:40:53 -05:00
|
|
|
def previous
|
2024-02-25 08:14:30 -05:00
|
|
|
@previous ||= PoolVersion.where("pool_id = ? and version < ?", pool_id, version).order("version desc").first
|
2017-01-03 17:40:53 -05:00
|
|
|
end
|
|
|
|
|
2016-12-14 21:09:45 -05:00
|
|
|
def pool
|
|
|
|
Pool.find(pool_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def updater
|
|
|
|
User.find(updater_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def updater_name
|
|
|
|
User.id_to_name(updater_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def pretty_name
|
2020-03-13 23:28:25 -04:00
|
|
|
name&.tr("_", " ") || '(Unknown Name)'
|
2016-12-14 21:09:45 -05:00
|
|
|
end
|
|
|
|
end
|