kakurenbo-puti 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4a224bebe0029c5a6c736176831dd549df38df0a
4
- data.tar.gz: 6bac0bc291bd5318c582a18f435b63850bcc598d
3
+ metadata.gz: 5a7a55e62643987f839573cbee301f3114176a2a
4
+ data.tar.gz: 440591cbabb762607202703a66c82b6d8c912b02
5
5
  SHA512:
6
- metadata.gz: a8d590bfc647fbe0a63d2d743e354bed23480f89c4326def4f9c5663ccd56e9e973a8043841971360426d9b996fbb0ecbd832d78578fe9b0f03f58c4a2cceb0a
7
- data.tar.gz: 32cac6cab3b8035e9983c53685809ea30c5eaec0ab2a26240c913cb76d21d8f4c50304f86d44acebe86ac6b2cb6bcd9f4f7dad1c10771fd25901617287c19473
6
+ metadata.gz: ff003f0b53797305c1f7b78df14ca3ce00fef049ddb1fa1030b7927230ce2699546c59122f6e080291d1bd5ef7e68bdf68701c372bc11873db7d8cb385c338cb
7
+ data.tar.gz: 5481b3aa1fa467ffd822703f62c3d49a232949194bd69462ff14a552528c083bd775ac2ff455cb81430b5f0c19c0c93fc37f299b69e9e48e466c0d61586c2d1a
data/.gitignore CHANGED
@@ -20,3 +20,6 @@ mkmf.log
20
20
 
21
21
  # IDE files.
22
22
  .idea/
23
+
24
+ # bundler directory.
25
+ vendor/bundle/
data/README.md CHANGED
@@ -77,6 +77,7 @@ model.restore!
77
77
 
78
78
  ### Definition of the dependency
79
79
  Use dependent_associations option of `soft-deletable`.
80
+ This option is useable only in `belongs_to`.
80
81
 
81
82
  ```ruby
82
83
 
@@ -12,6 +12,7 @@ module KakurenboPuti
12
12
  Initializers.create_scopes self, dependent_associations
13
13
 
14
14
  include InstanceMethods
15
+ extend ClassMethods
15
16
  end
16
17
 
17
18
  module Initializers
@@ -40,14 +41,17 @@ module KakurenboPuti
40
41
  def self.create_scopes(target_class, dependent_associations)
41
42
  target_class.class_eval do
42
43
  scope :only_soft_destroyed, -> { where.not(id: without_soft_destroyed.select(:id)) }
44
+
43
45
  scope :without_soft_destroyed, (lambda do
44
46
  dependent_associations.inject(where(soft_delete_column => nil)) do |relation, name|
45
47
  association = relation.klass.reflect_on_all_associations.find{|a| a.name == name }
48
+ raise 'dependent association is usable only in `belongs_to`.' unless association.belongs_to?
49
+
50
+ parent_relation = association.klass.all
46
51
  if association.klass.method_defined?(:soft_delete_column)
47
- relation.joins(name).merge(association.klass.without_soft_destroyed).references(name)
48
- else
49
- relation.joins(name).references(name)
52
+ parent_relation = parent_relation.without_soft_destroyed
50
53
  end
54
+ relation.where(association.foreign_key => parent_relation.select(:id))
51
55
  end
52
56
  end)
53
57
  end
@@ -89,5 +93,15 @@ module KakurenboPuti
89
93
  self.class.only_soft_destroyed.where(id: id).exists?
90
94
  end
91
95
  end
96
+
97
+ module ClassMethods
98
+ def soft_destroy_all(conditions = nil)
99
+ if conditions
100
+ where(conditions).soft_destroy_all
101
+ else
102
+ all.each {|object| object.soft_destroy }
103
+ end
104
+ end
105
+ end
92
106
  end
93
107
  end
@@ -1,3 +1,3 @@
1
1
  module KakurenboPuti
2
- VERSION = '0.0.3'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -19,6 +19,7 @@ describe KakurenboPuti::ActiveRecordBase do
19
19
  SoftDeleteModel.tap do |klass|
20
20
  klass.class_eval do
21
21
  soft_deletable options_cache
22
+ has_many :soft_delete_children
22
23
 
23
24
  before_soft_destroy :cb_mock
24
25
  after_soft_destroy :cb_mock
@@ -147,6 +148,16 @@ describe KakurenboPuti::ActiveRecordBase do
147
148
  child_class.without_soft_destroyed
148
149
  end
149
150
 
151
+ context 'When dependent association use in `has_many`' do
152
+ let :model_class_options do
153
+ {dependent_associations: [:soft_delete_children]}
154
+ end
155
+
156
+ it 'raise error' do
157
+ expect { subject }.to raise_error
158
+ end
159
+ end
160
+
150
161
  context 'When soft-deleted' do
151
162
  it 'Return a relation without soft-deleted model.' do
152
163
  expect {
@@ -327,4 +338,33 @@ describe KakurenboPuti::ActiveRecordBase do
327
338
  end
328
339
  end
329
340
  end
341
+
342
+ describe '.soft_destroy_all' do
343
+ subject do
344
+ model_class.soft_destroy_all
345
+ end
346
+ let!(:model_instance) { model_class.create! }
347
+
348
+ it 'SoftDelete model' do
349
+ expect {
350
+ subject
351
+ }.to change {
352
+ model_class.without_soft_destroyed.count
353
+ }.to(0)
354
+ end
355
+
356
+ context 'with conditions' do
357
+ subject do
358
+ model_class.soft_destroy_all(id: model_instance.id)
359
+ end
360
+
361
+ it 'SoftDelete model' do
362
+ expect {
363
+ subject
364
+ }.to change {
365
+ model_class.without_soft_destroyed.count
366
+ }.to(0)
367
+ end
368
+ end
369
+ end
330
370
  end
@@ -0,0 +1,123 @@
1
+ require 'spec_helper'
2
+
3
+ describe KakurenboPuti::ActiveRecordBase do
4
+ define_active_record_model :Parent do |t|
5
+ t.datetime :soft_destroyed_at
6
+ end
7
+
8
+ define_active_record_model :OneChild do |t|
9
+ t.integer :parent_id
10
+ t.datetime :soft_destroyed_at
11
+ end
12
+
13
+ define_active_record_model :TwoChild do |t|
14
+ t.integer :one_child_id
15
+ t.datetime :soft_destroyed_at
16
+ end
17
+
18
+ define_active_record_model :ThreeChild do |t|
19
+ t.integer :two_child_id
20
+ t.datetime :soft_destroyed_at
21
+ end
22
+
23
+ define_active_record_model :FourChild do |t|
24
+ t.integer :three_child_id
25
+ t.datetime :soft_destroyed_at
26
+ end
27
+
28
+ define_active_record_model :FiveChild do |t|
29
+ t.integer :four_child_id
30
+ t.datetime :soft_destroyed_at
31
+ end
32
+
33
+
34
+ let :parent_class do
35
+ Parent.tap do |klass|
36
+ klass.class_eval { soft_deletable }
37
+ end
38
+ end
39
+
40
+ let :one_child_class do
41
+ OneChild.tap do |klass|
42
+ klass.class_eval do
43
+ soft_deletable dependent_associations: [:parent]
44
+ belongs_to :parent
45
+ end
46
+ end
47
+ end
48
+
49
+ let :two_child_class do
50
+ TwoChild.tap do |klass|
51
+ klass.class_eval do
52
+ soft_deletable dependent_associations: [:one_child]
53
+ belongs_to :one_child
54
+ end
55
+ end
56
+ end
57
+
58
+ let :three_child_class do
59
+ ThreeChild.tap do |klass|
60
+ klass.class_eval do
61
+ soft_deletable dependent_associations: [:two_child]
62
+ belongs_to :two_child
63
+ end
64
+ end
65
+ end
66
+
67
+ let :four_child_class do
68
+ FourChild.tap do |klass|
69
+ klass.class_eval do
70
+ soft_deletable dependent_associations: [:three_child]
71
+ belongs_to :three_child
72
+ end
73
+ end
74
+ end
75
+
76
+ let :five_child_class do
77
+ FiveChild.tap do |klass|
78
+ klass.class_eval do
79
+ soft_deletable dependent_associations: [:four_child]
80
+ belongs_to :four_child
81
+ end
82
+ end
83
+ end
84
+
85
+ let :parent do
86
+ parent_class.create!
87
+ end
88
+
89
+ let :one_child do
90
+ one_child_class.create!(parent: parent)
91
+ end
92
+
93
+ let :two_child do
94
+ two_child_class.create!(one_child: one_child)
95
+ end
96
+
97
+ let :three_child do
98
+ three_child_class.create!(two_child: two_child)
99
+ end
100
+
101
+ let :four_child do
102
+ four_child_class.create(three_child: three_child)
103
+ end
104
+
105
+ let :five_child do
106
+ five_child_class.create(four_child: four_child)
107
+ end
108
+
109
+
110
+ context 'When delete root' do
111
+ subject do
112
+ parent.soft_destroy
113
+ end
114
+
115
+ it 'SoftDestroy dependent tail.' do
116
+ expect {
117
+ subject
118
+ }.to change {
119
+ five_child.soft_destroyed?
120
+ }.from(false).to(true)
121
+ end
122
+ end
123
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kakurenbo-puti
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - alfa-jpn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-07 00:00:00.000000000 Z
11
+ date: 2015-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -132,6 +132,7 @@ files:
132
132
  - lib/kakurenbo_puti/active_record_base.rb
133
133
  - lib/kakurenbo_puti/version.rb
134
134
  - spec/kakurenbo_puti/active_record_base_spec.rb
135
+ - spec/kakurenbo_puti/situations/deep_nest_spec.rb
135
136
  - spec/spec_helper.rb
136
137
  - spec/supports/active_record_model.rb
137
138
  - spec/supports/active_record_model_spec.rb
@@ -161,6 +162,7 @@ specification_version: 4
161
162
  summary: Lightweight soft-delete gem.
162
163
  test_files:
163
164
  - spec/kakurenbo_puti/active_record_base_spec.rb
165
+ - spec/kakurenbo_puti/situations/deep_nest_spec.rb
164
166
  - spec/spec_helper.rb
165
167
  - spec/supports/active_record_model.rb
166
168
  - spec/supports/active_record_model_spec.rb