GithubHelp home page GithubHelp logo

alexpetrov / activerecord-prunable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from snthpr1d3/activerecord-prunable

0.0 2.0 0.0 18 KB

Ruby gem for convenient removal of obsolete ActiveRecord models.

License: MIT License

Ruby 99.04% Shell 0.96%

activerecord-prunable's Introduction

Activerecord-prunable Gem Version Build Status Code Climate

Convenient removal of obsolete records for ActiveRecord models.

Installation

Add this line to your application's Gemfile:

gem 'activerecord-prunable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord-prunable

Usage

1. Include the Prunable module in the ActiveRecord model which needs to be pruned.

2. Define the :prunable scope which returns records to prune.

class Notification < ApplicationRecord
  include ActiveRecord::Prunable

  scope :prunable, -> { where("created_at > ?", 1.month.ago) }

  # You can also set type of removing records (:destroy or :delete).
  # By default it's :destroy
  prune_method :delete
end

or use one of the prune_after, prune_created_after, prune_updated_after methods

class Notification < ApplicationRecord
  include ActiveRecord::Prunable

   prune_after 7.days
end

prune_after is an alias for prune_created_after prune_created_after(TTL) defines where('created_at < ?', current_time - TTL) prunable scope prune_updated_after(TTL) defines where('updated_at < ?', current_time - TTL) prunable scope

3. Add a Prunable.prune! call to a periodic task.

Example:

# clockwork configuration file

every(1.day, 'models.prune', at: '04:20', thread: true) { Prunable.prune! }

You can also inject current time Prunable.prune!(current_time: Time.current)

Advanced Usage

Pruning a single model:

SomeModel.prune!

Pruning multiple models: Note that the Prunable.prune! calls Rails.application.eager_load!. It can decrease free memory size.

Prunable.prune!(SomeModel, AnotherModel)

Set default method of pruning (:destroy or :delete):

Prunable.prune!(prune_method: :delete)

Call :prunable scope with params:

Prunable.prune!(params: [:foo, :bar])

Getting an array of all the models which include ActiveRecord::Prunable:

Prunable.models

Pruning all models which include ActiveRecord::Prunable:

Prunable.prune!

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.