Files
danbooru/app/models/note_version.rb
BrokenEagle d829ab3a00 Move all order logic to models
- Have a default order for each model
-- The overall default is ID DESC
- Allow for custom orderings
-- When comma-separated IDs are used
2018-01-29 11:42:53 -08:00

38 lines
1.0 KiB
Ruby

class NoteVersion < ApplicationRecord
before_validation :initialize_updater
belongs_to :updater, :class_name => "User", :counter_cache => "note_update_count"
scope :for_user, lambda {|user_id| where("updater_id = ?", user_id)}
attr_accessible :note_id, :x, :y, :width, :height, :body, :updater_id, :updater_ip_addr, :is_active, :post_id, :html_id, :version
def self.search(params)
q = super
if params[:updater_id]
q = q.where(updater_id: params[:updater_id].split(",").map(&:to_i))
end
if params[:post_id]
q = q.where(post_id: params[:post_id].split(",").map(&:to_i))
end
if params[:note_id]
q = q.where(note_id: params[:note_id].split(",").map(&:to_i))
end
q.apply_default_order(params)
end
def initialize_updater
self.updater_id = CurrentUser.id
self.updater_ip_addr = CurrentUser.ip_addr
end
def previous
NoteVersion.where("note_id = ? and updated_at < ?", note_id, updated_at).order("updated_at desc").first
end
def updater_name
User.id_to_name(updater_id)
end
end