paranoid42 1.0.0 → 1.1.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: e570b02db22769fdd1f075786855bb8a0f69b86c
4
- data.tar.gz: 50b87789f4e9598ab3ef6684746cd54692230fd8
3
+ metadata.gz: 69e4b2c87e3fb10a533307a92a108d7401095fce
4
+ data.tar.gz: ca31ba429e73de3d1744e2f9a69f927fa8074e9c
5
5
  SHA512:
6
- metadata.gz: 9bb7953888ba744e8836b14fefa60061fe5df737ba007d5612cd9c8b3e9583cf932224cabf0b689b42ae706c01e6ce17840543717a68c75149b988e81eb5230e
7
- data.tar.gz: 332914f9bf6b643c64cccecc22d79b992ba7f53c5aff9ac13bd17f89d935136bc762db0da835e4d7ad3a52437709932b162c58eefbd39064d8748280c4659860
6
+ metadata.gz: 07eab9d0d50aec82382eea3e41c109bb4aacc9598c1be13ba698f573d0046da1c9064c7233ba8ea0eb7d734504b0111cad2f84ec186a356bf152eefb9f4f0ceb
7
+ data.tar.gz: 259251463b5b1dde4c046c297def4628632907d11727e275960327ebbbc83551637ee46f59b08cbf1b9fc66c2cfe0cb6973ca1cf83adce71f91be0e632109802
data/README.md CHANGED
@@ -1,10 +1,8 @@
1
1
  # Paranoid42
2
2
 
3
- [paranoid2](https://github.com/anjlab/paranoid2) ideas (and code) adapted for rails 4.2
3
+ Retain soft-deleting functionality without losing AdequateRecord's speed improvements. A fork of [paranoid2](https://github.com/anjlab/paranoid2).
4
4
 
5
- Rails 4.2 introduced adequate record, but in order to take advantage of the improvements, default_scope can't be used. Paranoid42 removes the default scope from Paranoid2. This means you have to be explicit about telling your app not to return deleted records.
6
-
7
- Paranoid42 removes the default scope and adds a new `not_deleted` method to your paranoid classes.
5
+ Rails 4.2 introduced adequate record, but in order to take advantage of the improvements, default_scope can't be used ([Release Notes](http://edgeguides.rubyonrails.org/4_2_release_notes.html)). Paranoid42 removes the default scope, and adds a new `not_deleted` method to your paranoid classes ([Benchmarks](https://gist.github.com/effektz/f18e1be522a328a981b9)).
8
6
 
9
7
  So this:
10
8
 
@@ -30,11 +28,7 @@ def active
30
28
  end
31
29
  ```
32
30
 
33
- [Benchmarks](https://gist.github.com/effektz/f18e1be522a328a981b9)
34
-
35
- ## Paranoid2
36
-
37
- Rails 4 defines `ActiveRecord::Base#destroy!` so `Paranoid42` gem use `force: true` arg to force destroy.
31
+ Counter Caches have also been tied into Paranoid42. Soft-deleting a record will decrement the association counter cache, and recovering a record will incremenet the association counter cache.
38
32
 
39
33
  ## Installation
40
34
 
@@ -73,11 +67,11 @@ c = Client.find(params[:id])
73
67
  # will set destroyed_at time
74
68
  c.destroy
75
69
 
76
- # will restore object and all it's associations
77
- c.restore
70
+ # will recover object and all it's associations
71
+ c.recover
78
72
 
79
- # will restore only this object without it's associations
80
- c.restore(associations: false)
73
+ # will recover only this object without it's associations
74
+ c.recover(associations: false)
81
75
 
82
76
  # will destroy object for real
83
77
  c.destroy(force: true)
@@ -10,16 +10,34 @@ module Paranoid42
10
10
  with_paranoid(opts) { super() }
11
11
  end
12
12
 
13
+ def update_counter_caches(opts = {})
14
+ return unless opts[:direction].present? && [:up, :down].include?(opts[:direction])
15
+ each_counter_cached_associations do |association|
16
+ foreign_key = association.reflection.foreign_key.to_sym
17
+ unless destroyed_by_association && destroyed_by_association.foreign_key.to_sym == foreign_key
18
+ if send(association.reflection.name)
19
+ if opts[:direction] == :up
20
+ association.increment_counters
21
+ elsif opts[:direction] == :down
22
+ association.decrement_counters
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
13
29
  def delete(opts = {})
14
30
  with_paranoid(opts) do
15
- touch(:deleted_at) if !deleted? && persisted?
31
+ if !deleted? && persisted?
32
+ touch(:deleted_at)
33
+ update_counter_caches({direction: :down})
34
+ end
16
35
  self.class.unscoped { super() } if paranoid_force
17
36
  end
18
37
  end
19
38
 
20
- def restore(opts={})
39
+ def recover(opts={})
21
40
  return if !destroyed?
22
-
23
41
  attrs = timestamp_attributes_for_update_in_model
24
42
  current_time = current_time_from_proper_timezone
25
43
  changes = {}
@@ -27,26 +45,23 @@ module Paranoid42
27
45
  changes[column.to_s] = write_attribute(column.to_s, current_time)
28
46
  end
29
47
  changes['deleted_at'] = write_attribute('deleted_at', nil)
30
-
31
48
  changes[self.class.locking_column] = increment_lock if locking_enabled?
32
-
33
49
  @changed_attributes.except!(*changes.keys)
34
50
  primary_key = self.class.primary_key
35
51
  self.class.unscoped.where({ primary_key => self[primary_key] }).update_all(changes)
36
-
37
52
  if opts.fetch(:associations) { true }
38
- restore_associations
53
+ recover_associations
39
54
  end
55
+ update_counter_caches({direction: :up})
40
56
  end
41
57
 
42
- def restore_associations
58
+ def recover_associations
43
59
  self.class.reflect_on_all_associations.each do |a|
44
60
  next unless a.klass.paranoid?
45
-
46
61
  if a.collection?
47
- send(a.name).restore_all
62
+ send(a.name).recover_all
48
63
  else
49
- a.klass.unscoped { send(a.name).try(:restore) }
64
+ a.klass.unscoped { send(a.name).try(:recover) }
50
65
  end
51
66
  end
52
67
  end
@@ -80,8 +95,8 @@ module Paranoid42
80
95
  end
81
96
  end
82
97
 
83
- def restore_all
84
- only_deleted.each &:restore
98
+ def recover_all
99
+ only_deleted.each &:recover
85
100
  end
86
101
  end
87
102
  end
@@ -1,3 +1,3 @@
1
1
  module Paranoid42
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
data/paranoid42.gemspec CHANGED
@@ -6,10 +6,10 @@ require 'paranoid42/version'
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = "paranoid42"
8
8
  gem.version = Paranoid42::VERSION
9
- gem.authors = ["effektz", "yury"]
10
- gem.email = ["alexweidmann@gmail.com", "yury.korolev@gmail.com"]
11
- gem.description = %q{paranoid models for rails 4.2}
12
- gem.summary = %q{paranoid models for rails 4.2}
9
+ gem.authors = ["effektz", "nitsujw"]
10
+ gem.email = ["alexweidmann@gmail.com", "nitsujweidmann@gmail.com"]
11
+ gem.description = %q{Paranoid soft deleting for Rails 4.2}
12
+ gem.summary = %q{Paranoid soft deleting for Rails 4.2}
13
13
  gem.homepage = "https://github.com/effektz/paranoid42"
14
14
  gem.license = "MIT"
15
15
 
@@ -70,12 +70,12 @@ describe Paranoid42 do
70
70
  model.only_deleted.wont_include b
71
71
  end
72
72
 
73
- it 'restores' do
73
+ it 'recovers' do
74
74
  a = model.create
75
75
  a.destroy
76
76
  a.must_be :destroyed?
77
77
  b = model.only_deleted.find(a.id)
78
- b.restore
78
+ b.recover
79
79
  b.reload
80
80
  b.wont_be :destroyed?
81
81
  end
@@ -137,7 +137,7 @@ describe Paranoid42 do
137
137
  employee.employers.not_deleted.count.must_equal 1
138
138
  end
139
139
 
140
- it 'restores has_many associations' do
140
+ it 'recovers has_many associations' do
141
141
  parent = ParentModel.create
142
142
  a = model.create(parent_model: parent)
143
143
  parent.destroy
@@ -145,20 +145,20 @@ describe Paranoid42 do
145
145
  parent.must_be :destroyed?
146
146
  a.must_be :destroyed?
147
147
  parent = ParentModel.unscoped.find(parent.id)
148
- parent.restore
148
+ parent.recover
149
149
  a.reload
150
150
  parent.wont_be :destroyed?
151
151
  a.wont_be :destroyed?
152
152
  end
153
153
 
154
- it 'restores belongs_to associations' do
154
+ it 'recovers belongs_to associations' do
155
155
  parent = ParentModel.create
156
156
  a = model.create(parent_model: parent)
157
157
  parent.destroy
158
158
  a.reload
159
159
  parent.must_be :destroyed?
160
160
  a.must_be :destroyed?
161
- a.restore
161
+ a.recover
162
162
  a.wont_be :destroyed?
163
163
  a.parent_model.wont_be :destroyed?
164
164
  end
@@ -190,7 +190,7 @@ describe Paranoid42 do
190
190
  end
191
191
 
192
192
  it 'validates uniqueness with scope' do
193
- a = model.create!(name: 'yury', phone: '9106701550')
193
+ a = model.create!(name: 'yury', phone: '3034207100')
194
194
  b = model.create(name: 'effektz', phone: '3034207100')
195
195
  b.wont_be :valid?
196
196
  a.destroy
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paranoid42
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - effektz
8
- - yury
8
+ - nitsujw
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-01-02 00:00:00.000000000 Z
12
+ date: 2015-01-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -53,10 +53,10 @@ dependencies:
53
53
  - - ">="
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
- description: paranoid models for rails 4.2
56
+ description: Paranoid soft deleting for Rails 4.2
57
57
  email:
58
58
  - alexweidmann@gmail.com
59
- - yury.korolev@gmail.com
59
+ - nitsujweidmann@gmail.com
60
60
  executables: []
61
61
  extensions: []
62
62
  extra_rdoc_files: []
@@ -97,7 +97,7 @@ rubyforge_project:
97
97
  rubygems_version: 2.4.5
98
98
  signing_key:
99
99
  specification_version: 4
100
- summary: paranoid models for rails 4.2
100
+ summary: Paranoid soft deleting for Rails 4.2
101
101
  test_files:
102
102
  - spec/paranoid42_spec.rb
103
103
  - spec/spec_helper.rb