Websites/app/models/e621/pool.rb

58 lines
1.3 KiB
Ruby

module E621
class Pool < ApplicationRecord
self.table_name = "e621.pools"
after_commit(on: %i[create update]) do
PoolIndex.update_document(self)
end
after_commit(on: :destroy) do
PoolIndex.delete_document(id)
end
def posts
E621::Post.where(id: post_ids)
end
def post_tags
posts.map(&:tags).flatten.uniq
end
def thumbnail_url
post = posts.first
return nil if post.nil? || post.is_deleted?
"https://static1.e621.net/data/#{post.md5[0..1]}/#{post.md5[2..3]}/#{post.md5}.#{post.file_ext}"
end
def self.search(params, page, limit)
E621PoolQueryBuilder.new(params).search(page, limit) => { results:, count: }
pools = where(id: results).sort_by { |pool| results.index(pool.id) }
{ pools: pools, total: count }
end
module SyncMethods
def sync
E621ExportDownloadJob.perform_later("pools")
end
def sync!
E621ExportDownloadJob.perform_now("pools")
end
def sync_tags
find_each do |pool|
UpdateE621PoolTagsJob.perform_later(pool.id)
end
end
def sync_tags!
find_each do |pool|
UpdateE621PoolTagsJob.perform_now(pool.id)
end
end
end
extend SyncMethods
end
end