GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Rails plugin to hide record instead of delete
Homepage: http://www.kivanio.com.br
Clone URL: git://github.com/kivanio/acts_as_deleted.git
name age message
file MIT-LICENSE Mon Aug 18 07:38:25 -0700 2008 initial commit [kivanio]
file README Fri Aug 22 14:00:09 -0700 2008 1 - fix some bugs [kivanio]
file Rakefile Mon Aug 18 07:38:25 -0700 2008 initial commit [kivanio]
file init.rb Mon Aug 18 19:23:46 -0700 2008 remove white spaces [kivanio]
directory lib/ Tue Sep 02 14:26:53 -0700 2008 fix bugs [kivanio]
directory test/ Mon Aug 18 07:38:25 -0700 2008 initial commit [kivanio]
README
ActsAsDeleted
=============

Rails plugin to hide records instead of delete.

Install plugin:
script/plugin install git://github.com/kivanio/acts_as_deleted.git

Example
=======

In your migration:

class CreateCars < ActiveRecord::Migration
  def self.up
    create_table :cars do |t|
      t.string :name

      # Use "deletestamps" to create columns "deleted" and "deleted_at"
      # With argument "true", will be create a column "deleted_id" to use with restful-authentication plugin
      t.deletestamps(true)
      
      t.timestamps
    end
  end

  def self.down
    drop_table :cars
  end
end



In your model:

class Car < ActiveRecord::Base
  # default names scopes: "with_deleted" and "without_deleted"
  acts_as_deleted
  
  # You can set names of scopes
  acts_as_deleted(:deleteed,:no_deleteed)

end

After that in your Controller:

class CarController < ApplicationController
  
  # Use scope "without_deleted" to find normal records
  def normal_records
    @cars = Car.without_deleted.find :all
  end
  
  # Use scope "with_deleted" to find deleted records
  def deleted_records
    @cars = Car.with_deleted.find :all
  end
  
  # Use custom scope "deleteed" to find normal records
  def normal_records
    @cars = Car.deleteed.find :all
  end
  
  # Use custom scope "no_deleteed" to find deleted records
  def deleted_records
    @cars = Car.no_deleteed.find :all
  end
  
  #Use just normal find of rails to get all records
  def all
    @cars = car.find :all
  end

  # In destroy action use "delete" method to hide a record
  def destroy
   @car = Car.find(params[:id])
    if @car.delete
      flash[:notice] = 'Bye Bye Car.'
    end
    redirect_to(cars_url)
  end
  
  # When you use plugin restful-authentication, you can use "delete_with_user" to save user
  def destroy_with_user
     @car = Car.find(params[:id])
      if @car.delete_with_user(current_user.id)
        flash[:notice] = 'Bye Bye Car.'
      end
      redirect_to(cars_url)
    end

end


TIPS:

To use validates_uniqueness_of with this plugin, you should use option :scope of validates_uniqueness_of.

validates_uniqueness_of :name, :scope => :deleted

Will be validated "name AND deleted" instead of only "name"


Very simple!

Like?
Recommend: http://www.workingwithrails.com/recommendation/new/person/5679-kivanio-pereira-barbosa

Copyright (c) 2008 [Kivanio Barbosa] http://www.kivanio.com.br , released under the MIT license