Skip to content
A fork of Paranoia (by Ryan Bigg and others) that updates a specified column with an pre-configured value on destroy, and does no scoping. Supports destroy hooks.
Ruby
Find file

README.md

Build Status Gem Version

Mark Only

Want to only mark a column with a value when the record is deleted or destroyed, but not unscope it? Then you're in the right place. If you want the records to be out of scope, look at acts_as_paranoid, paranoid, etc.

mark_only on the model class sets a specified column with a specified value on destroy/delete and disables the ability to delete/destroy on instance, model class, and via relation, using the default ActiveRecord version of those, and supports destroy callbacks if raise not enabled. destroy! in Rails 4 is supported.

Once a record is marked, deleted?/destroyed? methods on a retrieved model instance return false.

Tested with ActiveRecord 3.1.x, 3.2.x, and 4.0.x via travis and appraisal.

Code originally based on Paranoia (by Ryan Bigg and others), but heavily modified.

Installation & Usage

Put this in your Gemfile:

gem 'mark_only'

Then run:

bundle install

Updating is as simple as bundle update mark_only.

Run your migrations for the desired models

class AddDeletedAtToClient < ActiveRecord::Migration
  def self.up
    add_column :clients, :some_column_to_mark, :string, :default => 'active'
  end

  def self.down
    remove_column :clients, :some_column_to_mark
  end
end

Use the active_value as :default.

Usage

In your environment.rb:

MarkOnly.configure do
  # if true, debug log failed attempts to delete/destroy
  self.debug = false
  # the value that should indicate that a record is deleted
  self.deleted_value = 'deleted'
end

...

In your model:

To disallow attempts to delete/destroy and instead update the specified column:

class Client < ActiveRecord::Base
  mark_only :some_column_to_mark

  ...
end

Disabling Globally

If you need to globally disable temporarily so that everything marked as mark_only will destroy/delete instead of work normally, which might be useful in a data cleanup script, use:

MarkOnly.enabled = false

Note: that is an application-wide setting affecting all models that use mark_only, and may allow destruction of data you did not intend to destroy. Please be careful!

Upgrading

  • v0.0.1 -> v1.0.x: restore! no longer supported; the workaround is to use SQL to change a record's mark column to some value other than MarkOnly.deleted_value. Similarly, if you need to really delete or destroy a row in the database corresponding to the record, use SQL.

License

This gem is released under the MIT license.

Something went wrong with that request. Please try again.