activerecord-prunable 0.2.0 → 0.2.1

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: 53737b5692d22d48a79f1b754a7541683e5e1226
4
- data.tar.gz: 96d239d3b0809674c2af1095ed5674cfd7889df1
3
+ metadata.gz: a969a628d477ceb71a467f4464b14b53632718aa
4
+ data.tar.gz: e6f7fde7c349973bdb80ca2f580dfb4f8871fd4f
5
5
  SHA512:
6
- metadata.gz: b1c9ad1d3c656a02e318910140a35765fb3b638340085f85cd3dcd0fd4005ea9327591d17d71d0cbdab4c8b95e6a2fe54825d651e373d7014bbd2b5866cad7bf
7
- data.tar.gz: 1e49941cf2e63081ef6c2dff32d4207bf22666a084cf3918baac7fd4b983f0957d8beeda8b53cf028017a1336faa3758d87c7d0f2b23256ccf84d10e450b0f17
6
+ metadata.gz: 0044529be2922d5c1765889d14a7e85d2af8bd5c54abd3d3f8ab74b53e0e3dea86a8b1d686949b5a2dc9a357e3a9961227e99a929339892d6b2a481dc0a76c52
7
+ data.tar.gz: 0751d43fe59f9c76f0f95a7f6b92aea445025f1b300121f6d3e3c103453db55d7481682da51bdc7c3e2e50fa0c699d44afb8289a194dfa6420bf21c30a6b461c
@@ -1,21 +1,21 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activerecord-prunable (0.2.0)
4
+ activerecord-prunable (0.2.1)
5
5
  activerecord
6
6
  activesupport
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activemodel (4.2.7)
12
- activesupport (= 4.2.7)
11
+ activemodel (4.2.7.1)
12
+ activesupport (= 4.2.7.1)
13
13
  builder (~> 3.1)
14
- activerecord (4.2.7)
15
- activemodel (= 4.2.7)
16
- activesupport (= 4.2.7)
14
+ activerecord (4.2.7.1)
15
+ activemodel (= 4.2.7.1)
16
+ activesupport (= 4.2.7.1)
17
17
  arel (~> 6.0)
18
- activesupport (4.2.7)
18
+ activesupport (4.2.7.1)
19
19
  i18n (~> 0.7)
20
20
  json (~> 1.7, >= 1.7.7)
21
21
  minitest (~> 5.1)
@@ -23,6 +23,7 @@ GEM
23
23
  tzinfo (~> 1.1)
24
24
  arel (6.0.3)
25
25
  builder (3.2.2)
26
+ byebug (9.0.5)
26
27
  diff-lcs (1.2.5)
27
28
  i18n (0.7.0)
28
29
  json (1.8.3)
@@ -49,6 +50,7 @@ PLATFORMS
49
50
 
50
51
  DEPENDENCIES
51
52
  activerecord-prunable!
53
+ byebug
52
54
  rspec (~> 3.0)
53
55
 
54
56
  BUNDLED WITH
data/README.md CHANGED
@@ -61,6 +61,12 @@ Set default method of pruning (:destroy or :delete):
61
61
  Prunable.prune!(prune_method: :delete)
62
62
  ```
63
63
 
64
+ Call `:prunable` scope with params:
65
+
66
+ ```ruby
67
+ Prunable.prune!(params: [:foo, :bar])
68
+ ```
69
+
64
70
  Getting an array of all models that include `ActiveRecord::Prunable`:
65
71
 
66
72
  ```ruby
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "activerecord-prunable"
3
- spec.version = "0.2.0"
3
+ spec.version = "0.2.1"
4
4
  spec.authors = ["dr2m"]
5
5
  spec.email = ["maletin@maletin.work"]
6
6
 
@@ -13,4 +13,6 @@ Gem::Specification.new do |spec|
13
13
 
14
14
  spec.add_dependency "activerecord"
15
15
  spec.add_dependency "activesupport"
16
+
17
+ spec.add_development_dependency "byebug"
16
18
  end
@@ -19,20 +19,18 @@ module ActiveRecord
19
19
 
20
20
  module ClassMethods
21
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
22
+ return false unless check_prune_method(method)
26
23
 
27
- logger.info "Prune method #{method} was successfully setted"
24
+ logger.info "Prune method #{method} was successfully setted for #{self}"
28
25
  class_variable_set(:@@prune_method, method)
29
26
  end
30
27
 
31
- def prune!
28
+ def prune!(*params, prune_method: nil)
32
29
  logger.info "Pruning old records of #{self}"
33
- return false unless check_scope
30
+ return false unless check_scope(*params)
34
31
 
35
- destroyed = prune_by_method
32
+ scope = prunable(*params)
33
+ destroyed = prune_by_method(scope, prune_method)
36
34
 
37
35
  if destroyed > 0
38
36
  logger.info "#{destroyed} records have been pruned."
@@ -45,13 +43,13 @@ module ActiveRecord
45
43
 
46
44
  private
47
45
 
48
- def check_scope
46
+ def check_scope(*params)
49
47
  unless respond_to?(:prunable)
50
48
  logger.info "This model has no :prunable scope, nothing to prune."
51
49
  return false
52
50
  end
53
51
 
54
- unless prunable.is_a?(::ActiveRecord::Relation)
52
+ unless prunable(*params).is_a?(::ActiveRecord::Relation)
55
53
  logger.info ":prunable is not a relation, nothing to prune."
56
54
  return false
57
55
  end
@@ -59,19 +57,29 @@ module ActiveRecord
59
57
  true
60
58
  end
61
59
 
62
- def prune_by_method
60
+ def check_prune_method(method)
61
+ unless [:destroy, :delete].include?(method)
62
+ logger.info "Incorrect prune method #{method} will be ignored for #{self}"
63
+ return false
64
+ end
65
+ true
66
+ end
67
+
68
+ def prune_by_method(scope, prune_method)
63
69
  unless class_variable_defined?(:@@prune_method)
64
- prune_method = :destroy
70
+ prune_method = prune_method || :destroy
65
71
  else
66
72
  prune_method = class_variable_get(:@@prune_method)
67
73
  end
68
74
 
75
+ return false unless check_prune_method(prune_method)
76
+
69
77
  logger.info "Prune method is #{prune_method}"
70
78
 
71
79
  if prune_method == :delete
72
- prunable.delete_all
80
+ scope.delete_all
73
81
  else
74
- prunable.destroy_all.size
82
+ scope.destroy_all.size
75
83
  end
76
84
  end
77
85
  end
@@ -9,14 +9,10 @@ module Prunable
9
9
  ActiveRecord::Prunable.includes
10
10
  end
11
11
 
12
- def prune!(*models, prune_method: nil)
12
+ def prune!(*models, prune_method: nil, params: [])
13
13
  models = self.models if models.empty?
14
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!
15
+ model.prune!(*params, prune_method: prune_method)
20
16
  end
21
17
  end
22
18
  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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - dr2m
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-15 00:00:00.000000000 Z
11
+ date: 2016-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description:
56
70
  email:
57
71
  - maletin@maletin.work