Code Monkey home page Code Monkey logo

mark_only's Introduction

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 4.1.x, 4.2.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'
  # only needed if you use scopes:
  self.active_scope_name = 'active'
  self.deleted_scope_name = '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

To also add scopes to filter by the marked column value, use the scopes option:

class Client < ActiveRecord::Base
  mark_only :some_column_to_mark, scopes: true

  ...
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.

mark_only's People

Contributors

garysweaver avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.