Giter Site home page Giter Site logo

caron10 / activerecord-prunable Goto Github PK

View Code? Open in Web Editor NEW

This project forked from snthpr1d3/activerecord-prunable

0.0 1.0 0.0 28 KB

Convenient removal of obsolete records for ActiveRecord models.

License: MIT License

Ruby 99.18% Shell 0.82%

activerecord-prunable's Introduction

Activerecord-prunable Gem Version Build Status Code Climate

Convenient removal of obsolete 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 models 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
  
  # Additional method to set removing in batches.
  # You're also able to specify batch size with number
  batch_removal
  # You can use alias :remove_in_batches
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

  # You're also able to define batch removal:
  # Prune_after 7.days, remove_in_batches: true or
  # Prune_after 7.days, batch_removal: 100
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)

Batch removal:

Prunable.prune!(in_batches: true)

You're also able to specify the batch size: Prunable.prune!(batch_size: 100)

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!

You are also able to call prune method instead of prune! if you don't want to receive exceptions.

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.