activerecord-prunable 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +7 -7
- data/README.md +10 -0
- data/activerecord-prunable.gemspec +1 -1
- data/lib/active_record/prunable.rb +40 -7
- data/lib/activerecord-prunable.rb +8 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53737b5692d22d48a79f1b754a7541683e5e1226
|
4
|
+
data.tar.gz: 96d239d3b0809674c2af1095ed5674cfd7889df1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1c9ad1d3c656a02e318910140a35765fb3b638340085f85cd3dcd0fd4005ea9327591d17d71d0cbdab4c8b95e6a2fe54825d651e373d7014bbd2b5866cad7bf
|
7
|
+
data.tar.gz: 1e49941cf2e63081ef6c2dff32d4207bf22666a084cf3918baac7fd4b983f0957d8beeda8b53cf028017a1336faa3758d87c7d0f2b23256ccf84d10e450b0f17
|
data/Gemfile.lock
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
activerecord-prunable (0.
|
4
|
+
activerecord-prunable (0.2.0)
|
5
5
|
activerecord
|
6
6
|
activesupport
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activemodel (4.2.
|
12
|
-
activesupport (= 4.2.
|
11
|
+
activemodel (4.2.7)
|
12
|
+
activesupport (= 4.2.7)
|
13
13
|
builder (~> 3.1)
|
14
|
-
activerecord (4.2.
|
15
|
-
activemodel (= 4.2.
|
16
|
-
activesupport (= 4.2.
|
14
|
+
activerecord (4.2.7)
|
15
|
+
activemodel (= 4.2.7)
|
16
|
+
activesupport (= 4.2.7)
|
17
17
|
arel (~> 6.0)
|
18
|
-
activesupport (4.2.
|
18
|
+
activesupport (4.2.7)
|
19
19
|
i18n (~> 0.7)
|
20
20
|
json (~> 1.7, >= 1.7.7)
|
21
21
|
minitest (~> 5.1)
|
data/README.md
CHANGED
@@ -24,6 +24,10 @@ gem "activerecord-prunable"
|
|
24
24
|
|
25
25
|
scope :prunable, -> { where("created_at > ?", 1.month.ago) }
|
26
26
|
|
27
|
+
# You can also set type of removing records (:destroy or :delete).
|
28
|
+
# By default it's :destroy
|
29
|
+
prune_method :delete
|
30
|
+
|
27
31
|
end
|
28
32
|
```
|
29
33
|
|
@@ -51,6 +55,12 @@ Pruning multiple models:
|
|
51
55
|
Prunable.prune!(SomeModel, AnotherModel)
|
52
56
|
```
|
53
57
|
|
58
|
+
Set default method of pruning (:destroy or :delete):
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
Prunable.prune!(prune_method: :delete)
|
62
|
+
```
|
63
|
+
|
54
64
|
Getting an array of all models that include `ActiveRecord::Prunable`:
|
55
65
|
|
56
66
|
```ruby
|
@@ -18,28 +18,61 @@ module ActiveRecord
|
|
18
18
|
end
|
19
19
|
|
20
20
|
module ClassMethods
|
21
|
+
def prune_method(method)
|
22
|
+
unless [:destroy, :delete].include?(method)
|
23
|
+
logger.info "Incorrect prune method #{method} will be ignored"
|
24
|
+
return false
|
25
|
+
end
|
26
|
+
|
27
|
+
logger.info "Prune method #{method} was successfully setted"
|
28
|
+
class_variable_set(:@@prune_method, method)
|
29
|
+
end
|
30
|
+
|
21
31
|
def prune!
|
22
32
|
logger.info "Pruning old records of #{self}"
|
33
|
+
return false unless check_scope
|
34
|
+
|
35
|
+
destroyed = prune_by_method
|
23
36
|
|
37
|
+
if destroyed > 0
|
38
|
+
logger.info "#{destroyed} records have been pruned."
|
39
|
+
else
|
40
|
+
logger.info "Nothing to prune."
|
41
|
+
end
|
42
|
+
|
43
|
+
destroyed
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def check_scope
|
24
49
|
unless respond_to?(:prunable)
|
25
50
|
logger.info "This model has no :prunable scope, nothing to prune."
|
26
|
-
return
|
51
|
+
return false
|
27
52
|
end
|
28
53
|
|
29
54
|
unless prunable.is_a?(::ActiveRecord::Relation)
|
30
55
|
logger.info ":prunable is not a relation, nothing to prune."
|
31
|
-
return
|
56
|
+
return false
|
32
57
|
end
|
33
58
|
|
34
|
-
|
59
|
+
true
|
60
|
+
end
|
35
61
|
|
36
|
-
|
37
|
-
|
62
|
+
def prune_by_method
|
63
|
+
unless class_variable_defined?(:@@prune_method)
|
64
|
+
prune_method = :destroy
|
38
65
|
else
|
39
|
-
|
66
|
+
prune_method = class_variable_get(:@@prune_method)
|
40
67
|
end
|
41
68
|
|
42
|
-
|
69
|
+
logger.info "Prune method is #{prune_method}"
|
70
|
+
|
71
|
+
if prune_method == :delete
|
72
|
+
prunable.delete_all
|
73
|
+
else
|
74
|
+
prunable.destroy_all.size
|
75
|
+
end
|
43
76
|
end
|
44
77
|
end
|
45
78
|
end
|
@@ -9,9 +9,15 @@ module Prunable
|
|
9
9
|
ActiveRecord::Prunable.includes
|
10
10
|
end
|
11
11
|
|
12
|
-
def prune!(*models)
|
12
|
+
def prune!(*models, prune_method: nil)
|
13
13
|
models = self.models if models.empty?
|
14
|
-
models.each
|
14
|
+
models.each do |model|
|
15
|
+
if prune_method && !model.class_variable_defined?(:@@prune_method)
|
16
|
+
model.prune_method(prune_method)
|
17
|
+
end
|
18
|
+
|
19
|
+
model.prune!
|
20
|
+
end
|
15
21
|
end
|
16
22
|
end
|
17
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-prunable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- dr2m
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-08-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|