Files
danbooru/app/models/note_version.rb
BrokenEagle e23ee170f5 Add alternate comparison types to versions
- The types are:
-- Previous: The default and the previously used type
-- Subsequent: Compares against the next version
-- Current: Compares against the current version
- Allow switching between comparison types in index and diff views
-- Have links vary depending upon current comparison type
2020-03-17 18:31:20 +00:00

78 lines
1.7 KiB
Ruby

class NoteVersion < ApplicationRecord
belongs_to :post
belongs_to :note
belongs_to_updater :counter_cache => "note_update_count"
def self.search(params)
q = super
q = q.search_attributes(params, :updater, :is_active, :post, :note_id, :x, :y, :width, :height, :body, :version)
q = q.text_attribute_matches(:body, params[:body_matches])
q.apply_default_order(params)
end
def previous
@previous ||= begin
NoteVersion.where("note_id = ? and version < ?", note_id, version).order("updated_at desc").limit(1).to_a
end
@previous.first
end
def subsequent
@subsequent ||= begin
NoteVersion.where("note_id = ? and version > ?", note_id, version).order("updated_at asc").limit(1).to_a
end
@subsequent.first
end
def current
@current ||= begin
NoteVersion.where("note_id = ?", note_id).order("updated_at desc").limit(1).to_a
end
@current.first
end
def self.status_fields
{
body: "Body",
was_moved: "Moved",
was_resized: "Resized",
was_deleted: "Deleted",
was_undeleted: "Undeleted",
}
end
def was_moved(type)
other = self.send(type)
x != other.x || y != other.y
end
def was_resized(type)
other = self.send(type)
width != other.width || height != other.height
end
def was_deleted(type)
other = self.send(type)
if type == "previous"
!is_active && other.is_active
else
is_active && !other.is_active
end
end
def was_undeleted(type)
other = self.send(type)
if type == "previous"
is_active && !other.is_active
else
!is_active && other.is_active
end
end
def self.available_includes
[:updater, :note, :post]
end
end