Files
grecia/app/lib/acts_as_paranoid_aliases.rb
Javi Martín 38ad65605e Use excluding instead of where.not(id:
This method was added in Rails 7.0 and makes the code slihgtly more
readable.

The downside is that it generates two queries instead of one, so it
might generate some confusion when debugging SQL queries. Its impact on
performance is probably negligible.
2024-07-22 18:35:35 +02:00

70 lines
1.2 KiB
Ruby

module ActsAsParanoidAliases
def self.included(base)
base.extend(ClassMethods)
class_eval do
def hide
return false if hidden?
update_attribute(:hidden_at, Time.current)
after_hide
end
def hidden?
deleted?
end
def after_hide
end
def confirmed_hide?
confirmed_hide_at.present?
end
def confirm_hide
update_attribute(:confirmed_hide_at, Time.current)
end
def restore(opts = {})
return false unless hidden?
super
update_attribute(:confirmed_hide_at, nil) if self.class.column_names.include? "confirmed_hide_at"
after_restore
end
def after_restore
end
end
end
module ClassMethods
def with_confirmed_hide
where.not(confirmed_hide_at: nil)
end
def without_confirmed_hide
excluding(with_confirmed_hide)
end
def with_hidden
with_deleted
end
def only_hidden
only_deleted
end
def hide_all(ids)
return if ids.blank?
where(id: ids).each(&:hide)
end
def restore_all(ids)
return if ids.blank?
only_hidden.where(id: ids).find_each(&:restore)
end
end
end