Code Monkey home page Code Monkey logo

paranoid42's Introduction

Paranoid42

Retain soft-deleting functionality without losing AdequateRecord's speed improvements. A fork of paranoid2.

Rails 4.2 introduced adequate record, but in order to take advantage of the improvements, default_scope can't be used (Release Notes). Paranoid42 removes the default scope, and adds a new not_deleted method to your paranoid classes (Benchmarks).

So this:

> Special.where(name: "test")

SELECT "specials".* FROM "specials" WHERE (deleted_at IS NULL) AND "specials"."name" = $1  [["name", "test"]]

Turns into this:

> Special.not_deleted.where(name: "test")

SELECT "specials".* FROM "specials" WHERE (deleted_at IS NULL) AND "specials"."name" = $1  [["name", "test"]]

If you would rather use a shorter method, just create a method with your preferred name, and reference not_deleted:

def active
  not_deleted
end

Counter Caches have also been tied into Paranoid42. Soft-deleting a record will decrement the association counter cache, and recovering a record will incremenet the association counter cache.

Installation

Add this line to your application's Gemfile:

gem 'paranoid42'

And then execute:

$ bundle

Usage

Add deleted_at: datetime to your model. Generate and run migrations.

rails g migration AddDeletedAtToClients deleted_at:datetime
class AddDeletedAtToClients < ActiveRecord::Migration
  def change
    add_column :clients, :deleted_at, :datetime
  end
end
class Client < ActiveRecord::Base
  paranoid
end

c = Client.find(params[:id])

# will set destroyed_at time
c.destroy

# will recover object and all it's associations
c.recover

# will recover only this object without it's associations
c.recover(associations: false)

# will destroy object for real
c.destroy(force: true)

# also useful scopes are available
Client.only_deleted

With paperclip

class Listing < ActiveRecord::Base
  has_attached_file :image,
    # ...
    preserve_files: true
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

paranoid42's People

Contributors

effektz avatar

Stargazers

 avatar

Watchers

Justin Weidmann avatar  avatar James Cloos 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.