Archive your Rails models rather than delete them. This provides the archiving functionality app so you can do the following:
user.archived? #=> false
user.archive! #=> true
user.archived? #=> true
user.unarchive! #=> true
user.archived? #=> false
<% if user.archived? %>
<%= link_to :Archive, archive_user_path(user) %>
<% else %>
<%= link_to :Unarchive, archive_user_path(user) %>
<% end %>
Add this line to your application's Gemfile:
gem 'archivable'
And then execute:
$ bundle
Or install it yourself as:
$ gem install archivable
First, you need to add the archived
column to your model (which we we call User
for this example):
$ rails g migration add_archived_to_users archived:boolean
$ rake db:migrate
NOTE: remember to edit the migration and set :archived
column to default to false
in order to simplify querying for non-archived models.
add_column :users, :archived, :boolean, default: false
In your routes file (config/routes.rb
):
My::Application.routes.draw do
resources :users do
get archive, on: :member
get archived, on: :collection
end
end
Next, you need to include the model concern to gain access to some handy methods.
class User < ActiveRecord::Base
include Archivable::Model
# ...
end
Lastly, you need to include the controller concern to handle the controller actions.
class UsersController < ApplicationController
include Archivable::Controller
def index
@users = User.where(archived: false)
end
# ...
end
Now, instead of a delete link, you can do the following:
<%= link_to user.archived? ? :Unarchive : :Archive, archive_user_path(user) %>
<%= link_to 'See Archived Users', archived_users_path %>
- Fork it ( http://github.com/johnotander/archivable/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request