activerecord-prunable 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 45b9f545fee642c67289799871a2d5da4054d84c
4
- data.tar.gz: 5f1ee266dd3ede0271215b9fb0461d8c03d4ebbc
3
+ metadata.gz: 53737b5692d22d48a79f1b754a7541683e5e1226
4
+ data.tar.gz: 96d239d3b0809674c2af1095ed5674cfd7889df1
5
5
  SHA512:
6
- metadata.gz: eb2e391664238c813f99c1741c43a060c5d9970f72b770079acceeba8bba911fe9ce2a4695cc6a82663a5e93307782c52ee618ea3354be9481b231bc6af7dfdc
7
- data.tar.gz: b6d61ba7d11befb1d65bbda2e61be368ea638f3cd6c3ccc85d3302dd502567edd8a8b1764aec5c2d61ca1081f18276488487ea48fdd249977e76d930aaa5c729
6
+ metadata.gz: b1c9ad1d3c656a02e318910140a35765fb3b638340085f85cd3dcd0fd4005ea9327591d17d71d0cbdab4c8b95e6a2fe54825d651e373d7014bbd2b5866cad7bf
7
+ data.tar.gz: 1e49941cf2e63081ef6c2dff32d4207bf22666a084cf3918baac7fd4b983f0957d8beeda8b53cf028017a1336faa3758d87c7d0f2b23256ccf84d10e450b0f17
@@ -1,21 +1,21 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activerecord-prunable (0.1.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.6)
12
- activesupport (= 4.2.6)
11
+ activemodel (4.2.7)
12
+ activesupport (= 4.2.7)
13
13
  builder (~> 3.1)
14
- activerecord (4.2.6)
15
- activemodel (= 4.2.6)
16
- activesupport (= 4.2.6)
14
+ activerecord (4.2.7)
15
+ activemodel (= 4.2.7)
16
+ activesupport (= 4.2.7)
17
17
  arel (~> 6.0)
18
- activesupport (4.2.6)
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "activerecord-prunable"
3
- spec.version = "0.1.1"
3
+ spec.version = "0.2.0"
4
4
  spec.authors = ["dr2m"]
5
5
  spec.email = ["maletin@maletin.work"]
6
6
 
@@ -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
- destroyed = prunable.destroy_all
59
+ true
60
+ end
35
61
 
36
- if destroyed.any?
37
- logger.info "#{destroyed.size} records have been pruned."
62
+ def prune_by_method
63
+ unless class_variable_defined?(:@@prune_method)
64
+ prune_method = :destroy
38
65
  else
39
- logger.info "Nothing to prune."
66
+ prune_method = class_variable_get(:@@prune_method)
40
67
  end
41
68
 
42
- destroyed
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(&:prune!)
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.1.1
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-07-20 00:00:00.000000000 Z
11
+ date: 2016-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec