activerecord-prunable 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 45b9f545fee642c67289799871a2d5da4054d84c
4
+ data.tar.gz: 5f1ee266dd3ede0271215b9fb0461d8c03d4ebbc
5
+ SHA512:
6
+ metadata.gz: eb2e391664238c813f99c1741c43a060c5d9970f72b770079acceeba8bba911fe9ce2a4695cc6a82663a5e93307782c52ee618ea3354be9481b231bc6af7dfdc
7
+ data.tar.gz: b6d61ba7d11befb1d65bbda2e61be368ea638f3cd6c3ccc85d3302dd502567edd8a8b1764aec5c2d61ca1081f18276488487ea48fdd249977e76d930aaa5c729
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pruner.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activerecord-prunable (0.1.0)
5
+ activerecord
6
+ activesupport
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (4.2.6)
12
+ activesupport (= 4.2.6)
13
+ builder (~> 3.1)
14
+ activerecord (4.2.6)
15
+ activemodel (= 4.2.6)
16
+ activesupport (= 4.2.6)
17
+ arel (~> 6.0)
18
+ activesupport (4.2.6)
19
+ i18n (~> 0.7)
20
+ json (~> 1.7, >= 1.7.7)
21
+ minitest (~> 5.1)
22
+ thread_safe (~> 0.3, >= 0.3.4)
23
+ tzinfo (~> 1.1)
24
+ arel (6.0.3)
25
+ builder (3.2.2)
26
+ diff-lcs (1.2.5)
27
+ i18n (0.7.0)
28
+ json (1.8.3)
29
+ minitest (5.9.0)
30
+ rspec (3.4.0)
31
+ rspec-core (~> 3.4.0)
32
+ rspec-expectations (~> 3.4.0)
33
+ rspec-mocks (~> 3.4.0)
34
+ rspec-core (3.4.4)
35
+ rspec-support (~> 3.4.0)
36
+ rspec-expectations (3.4.0)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.4.0)
39
+ rspec-mocks (3.4.1)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.4.0)
42
+ rspec-support (3.4.1)
43
+ thread_safe (0.3.5)
44
+ tzinfo (1.2.2)
45
+ thread_safe (~> 0.1)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ activerecord-prunable!
52
+ rspec (~> 3.0)
53
+
54
+ BUNDLED WITH
55
+ 1.12.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 n.maletin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Description
2
+
3
+ Convenient removal of obsolete ActiveRecord models.
4
+
5
+ # Installation
6
+
7
+ ```ruby
8
+ # Gemfile
9
+
10
+ gem "activerecord-prunable"
11
+ ```
12
+
13
+ # Usage
14
+
15
+ 1. Include the `Prunable` module in the ActiveRecord model that needs to be pruned.
16
+ 2. Define the `:prunable` scope that returns models to prune.
17
+
18
+ Example:
19
+
20
+ ```ruby
21
+ class Notification < ApplicationRecord
22
+
23
+ include ActiveRecord::Prunable
24
+
25
+ scope :prunable, -> { where("created_at > ?", 1.month.ago) }
26
+
27
+ end
28
+ ```
29
+
30
+ 3. Add a `Prunable.prune!` call to a periodic task.
31
+
32
+ Example:
33
+
34
+ ```ruby
35
+ # clockwork configuration file
36
+
37
+ every(1.day, 'models.prune', at: '04:20', thread: true) { Prunable.prune! }
38
+ ```
39
+
40
+ # Advanced Usage
41
+
42
+ Pruning a single model:
43
+
44
+ ```ruby
45
+ SomeModel.prune!
46
+ ```
47
+
48
+ Pruning multiple models:
49
+
50
+ ```ruby
51
+ Prunable.prune!(SomeModel, AnotherModel)
52
+ ```
53
+
54
+ Getting an array of all models that include `ActiveRecord::Prunable`:
55
+
56
+ ```ruby
57
+ Prunable.models
58
+ ```
59
+
60
+ Pruning all models that include `ActiveRecord::Prunable`:
61
+
62
+ ```ruby
63
+ Prunable.prune!
64
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "activerecord-prunable"
3
+ spec.version = "0.1.1"
4
+ spec.authors = ["dr2m"]
5
+ spec.email = ["maletin@maletin.work"]
6
+
7
+ spec.summary = "Convenient removal of obsolete ActiveRecord models."
8
+ spec.license = "MIT"
9
+
10
+ spec.files = Dir.glob('**/*').reject{|f| f.match(%r{^(spec)/}) }
11
+
12
+ spec.add_development_dependency "rspec", "~> 3.0"
13
+
14
+ spec.add_dependency "activerecord"
15
+ spec.add_dependency "activesupport"
16
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "prunable"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,46 @@
1
+ require 'active_support'
2
+ require 'active_record'
3
+
4
+ module ActiveRecord
5
+ module Prunable
6
+ extend ActiveSupport::Concern
7
+
8
+ @@models = []
9
+
10
+ class << self
11
+ def included(model)
12
+ @@models << model
13
+ end
14
+
15
+ def includes
16
+ @@models
17
+ end
18
+ end
19
+
20
+ module ClassMethods
21
+ def prune!
22
+ logger.info "Pruning old records of #{self}"
23
+
24
+ unless respond_to?(:prunable)
25
+ logger.info "This model has no :prunable scope, nothing to prune."
26
+ return
27
+ end
28
+
29
+ unless prunable.is_a?(::ActiveRecord::Relation)
30
+ logger.info ":prunable is not a relation, nothing to prune."
31
+ return
32
+ end
33
+
34
+ destroyed = prunable.destroy_all
35
+
36
+ if destroyed.any?
37
+ logger.info "#{destroyed.size} records have been pruned."
38
+ else
39
+ logger.info "Nothing to prune."
40
+ end
41
+
42
+ destroyed
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,17 @@
1
+ require 'active_record/prunable'
2
+
3
+ module Prunable
4
+ @@eager_load = nil
5
+
6
+ class << self
7
+ def models
8
+ @@eager_load ||= Rails.application.eager_load!
9
+ ActiveRecord::Prunable.includes
10
+ end
11
+
12
+ def prune!(*models)
13
+ models = self.models if models.empty?
14
+ models.each(&:prune!)
15
+ end
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-prunable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - dr2m
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - maletin@maletin.work
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Gemfile
63
+ - Gemfile.lock
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - activerecord-prunable.gemspec
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/active_record/prunable.rb
71
+ - lib/activerecord-prunable.rb
72
+ homepage:
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.5.1
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Convenient removal of obsolete ActiveRecord models.
96
+ test_files: []
97
+ has_rdoc: