app/models/effective/log.rb in effective_logging-1.6.0 vs app/models/effective/log.rb in effective_logging-1.7.0
- old
+ new
@@ -25,19 +25,39 @@
# status :string, :validates => [:presence, :inclusion => {:in => EffectiveLogging.statuses }]
# timestamps
# end
validates :message, presence: true
- validates :status, presence: true, inclusion: { in: (EffectiveLogging.statuses + [EffectiveLogging.log_changes_status]) }
+ validates :status, presence: true, inclusion: { in: (EffectiveLogging.statuses + [EffectiveLogging.log_changes_status, EffectiveLogging.trashable_status]) }
default_scope -> { order(updated_at: :desc) }
+ scope :logged_changes, -> { where(status: EffectiveLogging.log_changes_status)}
+ scope :changes, -> { where(status: EffectiveLogging.log_changes_status)}
+ scope :trash, -> { where(status: EffectiveLogging.trashable_status)}
+
+ def to_s
+ case status
+ when EffectiveLogging.trashable_status
+ [associated_type, associated_id].join(' ').presence || 'New Trash item'
+ else
+ "Log #{id}"
+ end
+ end
+
def log(message, status = EffectiveLogging.statuses.first, options = {})
EffectiveLogger.log(message, status, (options || {}).merge({:parent => self}))
end
def details
self[:details] || {}
+ end
+
+ # So this is a Trash item
+ # When we delete ourselves, we restore this trash item first
+ def restore_trashable!
+ raise 'no attributes to restore from' unless details.kind_of?(Hash) && details[:attributes].present?
+ associated_type.constantize.new(details[:attributes]).save!
end
# def next_log
# @next_log ||= Log.unscoped.order(:id).where(:parent_id => self.parent_id).where('id > ?', self.id).first
# end