Sha256: 47df523fd2e6001031c6966d7a5d492834fa688dc070a0f0f8c3780229d1054b

Contents?: true

Size: 954 Bytes

Versions: 9

Compression:

Stored size: 954 Bytes

Contents

module Deleteable

  def self.included(receiver)
    receiver.extend ClassMethods
    receiver.class_eval do
      default_scope where("deleted_at IS NULL")
    end
  end

  def is_deleted?
    !deleted_at.nil?
  end

  def delete
    update_attribute(:deleted_at,Time.now)
  end

  def undelete
    update_attribute(:deleted_at,nil)
  end

  module ClassMethods
    def deleted
      unscoped.where("deleted_at IS NOT NULL")
    end
  end

end

module DeleteableActions
  def delete
    destroy
  end

  def destroy
    thing = self.class.name.gsub(/s?Controller$/,'').constantize.find(params[:id])
    thing.delete
    flash[:info] = "#{thing.class.name.gsub(/^.*::/,'').humanize} successfully deleted."
    redirect_to(action: :index)
  end

  # :nocov: - not currently supported
  def undelete
    thing = self.class.name.gsub(/s?Controller$/,'').constantize.find(params[:id])
    thing.undelete
    redirect_to(action: :index)
  end
  # :nocov:
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mail_manager-3.2.25.1 lib/deleteable.rb
mail_manager-3.2.22.0 lib/deleteable.rb
mail_manager-3.2.7 lib/deleteable.rb
mail_manager-3.2.6 lib/deleteable.rb
mail_manager-3.2.5 lib/deleteable.rb
mail_manager-3.2.4 lib/deleteable.rb
mail_manager-3.2.2 lib/deleteable.rb
mail_manager-3.2.1 lib/deleteable.rb
mail_manager-3.2.0 lib/deleteable.rb