Files
danbooru/app/models/artist_commentary_version.rb
evazion a7dc05ce63 Enable frozen string literals.
Make all string literals immutable by default.
2021-12-14 21:33:27 -06:00

58 lines
1.6 KiB
Ruby

# frozen_string_literal: true
class ArtistCommentaryVersion < ApplicationRecord
belongs_to :post
belongs_to_updater
def self.text_matches(query)
query = "*#{query}*" unless query =~ /\*/
where_ilike(:original_title, query)
.or(where_ilike(:original_description, query))
.or(where_ilike(:translated_title, query))
.or(where_ilike(:translated_description, query))
end
def self.search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :original_title, :original_description, :translated_title, :translated_description, :post, :updater)
if params[:text_matches].present?
q = q.text_matches(params[:text_matches])
end
q.apply_default_order(params)
end
def previous
@previous ||= ArtistCommentaryVersion.where("post_id = ? and updated_at < ?", post_id, updated_at).order("updated_at desc").limit(1).to_a
@previous.first
end
def subsequent
@subsequent ||= ArtistCommentaryVersion.where("post_id = ? and updated_at > ?", post_id, updated_at).order("updated_at asc").limit(1).to_a
@subsequent.first
end
def current
@current ||= ArtistCommentaryVersion.where(post_id: post_id).order("updated_at desc").limit(1).to_a
@current.first
end
def self.status_fields
{
original_title: "OrigTitle",
original_description: "OrigDesc",
translated_title: "TransTitle",
translated_description: "TransDesc",
}
end
def unchanged_empty?(field)
self[field].strip.empty? && (previous.nil? || previous[field].strip.empty?)
end
def self.available_includes
[:post, :updater]
end
end