Module: Rails::Trash::InstanceMethods

Defined in:
lib/rails-trash.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



42
43
44
45
46
# File 'lib/rails-trash.rb', line 42

def self.included(base)
  base.class_eval do
    default_scope where(:deleted_at => nil) if arel_table[:deleted_at]
  end
end

Instance Method Details

#destroy_with_trashObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/rails-trash.rb', line 48

def destroy_with_trash
  return destroy_without_trash if @trash_is_disabled
  deleted_at = Time.now.utc
  self.update_attribute(:deleted_at, deleted_at)
  self.class.reflect_on_all_associations(:has_many).each do |reflection|
    if reflection.options[:dependent].eql?(:destroy)
      self.send(reflection.name).update_all(:deleted_at => deleted_at)
    end
  end
end

#disable_trashObject



63
64
65
66
67
68
69
70
71
# File 'lib/rails-trash.rb', line 63

def disable_trash
  save_val = @trash_is_disabled
  begin
    @trash_is_disabled = true
    yield if block_given?
  ensure
    @trash_is_disabled = save_val
  end
end

#restoreObject



59
60
61
# File 'lib/rails-trash.rb', line 59

def restore
  self.update_attribute(:deleted_at, nil)
end

#trashed?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/rails-trash.rb', line 73

def trashed?
  deleted_at.present?
end