Sha256: 1dd62681dfcb4dd1e82d1244a25583bba16a62fa61a2c089566527d8200aca60
Contents?: true
Size: 1.33 KB
Versions: 1
Compression:
Stored size: 1.33 KB
Contents
require "destroyed_at/version" module DestroyedAt def self.included(klass) klass.instance_eval do default_scope { where(destroyed_at: nil) } after_initialize :_set_destruction_state define_model_callbacks :restore end end # Set an object's destroyed_at time. def destroy run_callbacks(:destroy) do destroy_associations self.destroyed_at = current_time_from_proper_timezone self.save(callbacks: false) @destroyed = true end end # Set an object's destroyed_at time to nil. def restore state = nil run_callbacks(:restore) do self.destroyed_at = nil if state = self.save(callbacks: false, validate: false) @destroyed = false _restore_associations end end state end private def _set_destruction_state @destroyed = destroyed_at.present? if has_attribute?(:destroyed_at) # Don't stop the other callbacks from running true end def _restore_associations reflections.select { |key, value| value.options[:dependent] == :destroy }.keys.each do |key| assoc = association(key) if assoc.options[:through] && assoc.options[:dependent] == :destroy assoc = association(assoc.options[:through]) end assoc.association_scope.each { |r| r.restore if r.respond_to? :restore } end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
destroyed_at-0.1.5 | lib/destroyed_at.rb |