Files
nairobi/lib/acts_as_paranoid_aliases.rb
Javi Martín d0d681a44b Add and apply EmptyLineAfterGuardClause rule
We were inconsistent on this one. I consider it particularly useful when
a method starts with a `return` statement.

In other cases, we probably shouldn't have a guard rule in the middle of
a method in any case, but that's a different refactoring.
2019-10-24 17:56:03 +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(opts)
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
where(confirmed_hide_at: nil)
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).each(&:restore)
end
end
end