acts_as_paranoid 0.7.2 → 0.7.3

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
  SHA256:
3
- metadata.gz: 73cbdef60cb81f9506a3763c13b0380f83169b8c68f0b914305d828b90202d25
4
- data.tar.gz: 2023f4db3125ff3db3dd9dc7b0fb0142e92d6f8f54bc433557d5dd0dc40fb853
3
+ metadata.gz: 2a0dd97ab43e1d7a8a72327a1abf5df84ae9d3ff36c38caed38a808aacef6b01
4
+ data.tar.gz: a628f74884aff858620127b163b1998b5d790a0cfcbff4ad56e9851f962a3caf
5
5
  SHA512:
6
- metadata.gz: 1c7b2ff7d8655621915c11630012ff946a196b46fd0a175facad75d90aa4f6ecae1ae6df1e627f9cf778faa46d26fb559b275e62101a4ba402007d545014f43d
7
- data.tar.gz: ec47e5232127609e3e83b9bf22b142a821e955b55a77e082f21ceaeb5e62c390253fee6815cfc3a4a795257eb68c15f29eb5adbad8e511703718e4278d84e52c
6
+ metadata.gz: 1488e86f1437d179e8aef1bc8b2e6da851a29d44ef9a93d7d2a60685bae36da50d69ec74bb19f6c4751257ddce5af556994c1d38beaef037b5753c063fdf409a
7
+ data.tar.gz: 630cec48256d5baf5e859f138fcc74b0a3718f6283ee08d47b36fa96cca9feba16bd6a2e1e4a3eeb3ecc210238d5ac8eb4ea31374ca8b0cfa07b57bdb9392368
data/CHANGELOG.md CHANGED
@@ -2,6 +2,27 @@
2
2
 
3
3
  Notable changes to this project will be documented in this file.
4
4
 
5
+ ## 0.7.3
6
+
7
+ ## Improvements
8
+
9
+ * Fix deletion time scopes ([#212] by [Matijs van Zuijlen][mvz])
10
+ * Reload `has_one` associations after dependent recovery ([#214],
11
+ by [Matijs van Zuijlen][mvz])
12
+ * Make dependent recovery work when parent is non-optional ([#227],
13
+ by [Matijs van Zuijlen][mvz])
14
+ * Avoid querying nil `belongs_to` associations when recovering ([#219],
15
+ by [Matijs van Zuijlen][mvz])
16
+ * On relations, deprecate `destroy!` in favour of `destroy_fully!` ([#222],
17
+ by [Matijs van Zuijlen][mvz])
18
+ * Deprecate the undocumented `:recovery_value` setting. Calculate the correct
19
+ value instead. ([#220], by [Matijs van Zuijlen][mvz])
20
+
21
+ ## Developer experience
22
+
23
+ * Log ActiveRecord activity to a visible log during tests ([#218],
24
+ by [Matijs van Zuijlen][mvz])
25
+
5
26
  ## 0.7.2
6
27
 
7
28
  * Do not set boolean column to NULL on recovery if nulls are not allowed
@@ -100,6 +121,13 @@ Notable changes to this project will be documented in this file.
100
121
 
101
122
  <!-- issues & pull requests -->
102
123
 
124
+ [#227]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/227
125
+ [#222]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/222
126
+ [#220]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/220
127
+ [#219]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/219
128
+ [#218]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/218
129
+ [#214]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/214
130
+ [#212]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/212
103
131
  [#209]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/209
104
132
  [#208]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/208
105
133
  [#207]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/207
@@ -27,15 +27,10 @@ module ActsAsParanoid
27
27
  column_type: "time",
28
28
  recover_dependent_associations: true,
29
29
  dependent_recovery_window: 2.minutes,
30
- recovery_value: nil,
31
30
  double_tap_destroys_fully: true
32
31
  }
33
32
  if options[:column_type] == "string"
34
33
  paranoid_configuration.merge!(deleted_value: "deleted")
35
- elsif options[:column_type] == "boolean" && !options[:allow_nulls]
36
- paranoid_configuration.merge!(recovery_value: false)
37
- elsif options[:column_type] == "boolean"
38
- paranoid_configuration.merge!(allow_nulls: true)
39
34
  end
40
35
 
41
36
  paranoid_configuration.merge!(options) # user options
@@ -88,6 +88,19 @@ module ActsAsParanoid
88
88
  end
89
89
  end
90
90
 
91
+ def recovery_value
92
+ if paranoid_configuration.key? :recovery_value
93
+ ActiveSupport::Deprecation.warn \
94
+ "The recovery_value setting is deprecated and will be removed in" \
95
+ " ActsAsParanoid 0.8.0"
96
+ paranoid_configuration[:recovery_value]
97
+ elsif boolean_type_not_nullable?
98
+ false
99
+ else
100
+ nil
101
+ end
102
+ end
103
+
91
104
  protected
92
105
 
93
106
  def define_deleted_time_scopes
@@ -96,10 +109,12 @@ module ActsAsParanoid
96
109
  }
97
110
 
98
111
  scope :deleted_after_time, lambda { |time|
99
- where("#{table_name}.#{paranoid_column} > ?", time)
112
+ only_deleted
113
+ .where("#{table_name}.#{paranoid_column} > ?", time)
100
114
  }
101
115
  scope :deleted_before_time, lambda { |time|
102
- where("#{table_name}.#{paranoid_column} < ?", time)
116
+ only_deleted
117
+ .where("#{table_name}.#{paranoid_column} < ?", time)
103
118
  }
104
119
  end
105
120
 
@@ -185,16 +200,16 @@ module ActsAsParanoid
185
200
 
186
201
  self.class.transaction do
187
202
  run_callbacks :recover do
188
- if options[:recursive]
189
- recover_dependent_associations(options[:recovery_window], options)
190
- end
191
203
  increment_counters_on_associations
192
- self.paranoid_value = self.class.paranoid_configuration[:recovery_value]
193
- if options[:raise_error]
194
- save!
195
- else
196
- save
197
- end
204
+ deleted_value = paranoid_value
205
+ self.paranoid_value = self.class.recovery_value
206
+ result = if options[:raise_error]
207
+ save!
208
+ else
209
+ save
210
+ end
211
+ recover_dependent_associations(deleted_value, options) if options[:recursive]
212
+ result
198
213
  end
199
214
  end
200
215
  end
@@ -205,30 +220,19 @@ module ActsAsParanoid
205
220
  recover(options)
206
221
  end
207
222
 
208
- def recover_dependent_associations(window, options)
223
+ def recover_dependent_associations(deleted_value, options)
209
224
  self.class.dependent_associations.each do |reflection|
210
- next unless (klass = get_reflection_class(reflection)).paranoid?
211
-
212
- scope = klass.only_deleted.merge(get_association_scope(reflection: reflection))
213
-
214
- # We can only recover by window if both parent and dependant have a
215
- # paranoid column type of :time.
216
- if self.class.paranoid_column_type == :time && klass.paranoid_column_type == :time
217
- scope = scope.deleted_inside_time_window(paranoid_value, window)
218
- end
219
-
220
- scope.each do |object|
221
- object.recover(options)
222
- end
225
+ recover_dependent_association(reflection, deleted_value, options)
223
226
  end
224
227
  end
225
228
 
226
229
  def destroy_dependent_associations!
227
230
  self.class.dependent_associations.each do |reflection|
228
- next unless (klass = get_reflection_class(reflection)).paranoid?
231
+ assoc = association(reflection.name)
232
+ next unless (klass = assoc.klass).paranoid?
229
233
 
230
234
  klass
231
- .only_deleted.merge(get_association_scope(reflection: reflection))
235
+ .only_deleted.merge(get_association_scope(assoc))
232
236
  .each(&:destroy!)
233
237
  end
234
238
  end
@@ -255,16 +259,33 @@ module ActsAsParanoid
255
259
 
256
260
  private
257
261
 
258
- def get_association_scope(reflection:)
259
- ActiveRecord::Associations::AssociationScope.scope(association(reflection.name))
260
- end
262
+ def recover_dependent_association(reflection, deleted_value, options)
263
+ assoc = association(reflection.name)
264
+ return unless (klass = assoc.klass).paranoid?
261
265
 
262
- def get_reflection_class(reflection)
263
- if reflection.macro == :belongs_to && reflection.options.include?(:polymorphic)
264
- send(reflection.foreign_type).constantize
265
- else
266
- reflection.klass
266
+ if reflection.belongs_to? && attributes[reflection.association_foreign_key].nil?
267
+ return
268
+ end
269
+
270
+ scope = klass.only_deleted.merge(get_association_scope(assoc))
271
+
272
+ # We can only recover by window if both parent and dependant have a
273
+ # paranoid column type of :time.
274
+ if self.class.paranoid_column_type == :time && klass.paranoid_column_type == :time
275
+ scope = scope.deleted_inside_time_window(deleted_value, options[:recovery_window])
267
276
  end
277
+
278
+ recovered = false
279
+ scope.each do |object|
280
+ object.recover(options)
281
+ recovered = true
282
+ end
283
+
284
+ assoc.reload if recovered && reflection.has_one? && assoc.loaded?
285
+ end
286
+
287
+ def get_association_scope(dependent_association)
288
+ ActiveRecord::Associations::AssociationScope.scope(dependent_association)
268
289
  end
269
290
 
270
291
  def paranoid_value=(value)
@@ -30,6 +30,13 @@ module ActsAsParanoid
30
30
  end
31
31
 
32
32
  def destroy!(id_or_array)
33
+ destroy_fully! id_or_array
34
+ end
35
+
36
+ deprecate :destroy!,
37
+ deprecator: ActiveSupport::Deprecation.new("0.8.0", "ActsAsParanoid")
38
+
39
+ def destroy_fully!(id_or_array)
33
40
  where(primary_key => id_or_array).orig_delete_all
34
41
  end
35
42
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActsAsParanoid
4
- VERSION = "0.7.2"
4
+ VERSION = "0.7.3"
5
5
  end
@@ -346,13 +346,9 @@ class AssociationsTest < ParanoidBaseTest
346
346
  assert_not_nil not_paranoid.paranoid_time
347
347
  end
348
348
 
349
- def test_mass_assignment_of_paranoid_column_enabled
350
- if Gem.loaded_specs["activerecord"].version >= Gem::Version.new("5.2.0")
351
- skip "Creation as deleted is not supported with Rails >= 5.2"
349
+ def test_mass_assignment_of_paranoid_column_disabled
350
+ assert_raises ActiveRecord::RecordNotSaved do
351
+ ParanoidTime.create! name: "Foo", deleted_at: Time.now
352
352
  end
353
- now = Time.now
354
- record = ParanoidTime.create! name: "Foo", deleted_at: now
355
- assert_equal "Foo", record.name
356
- assert_equal now, record.deleted_at
357
353
  end
358
354
  end
data/test/test_core.rb CHANGED
@@ -112,6 +112,34 @@ class ParanoidTest < ParanoidBaseTest
112
112
  end
113
113
  end
114
114
 
115
+ def test_recover_has_one_association
116
+ parent = ParanoidBoolean.create(name: "parent")
117
+ child = parent.create_paranoid_has_one_dependant(name: "child")
118
+
119
+ parent.destroy
120
+ assert parent.paranoid_has_one_dependant.destroyed?
121
+
122
+ parent.recover
123
+ refute parent.paranoid_has_one_dependant.destroyed?
124
+
125
+ child.reload
126
+ refute child.destroyed?
127
+ end
128
+
129
+ def test_recover_has_many_association
130
+ parent = ParanoidTime.create(name: "parent")
131
+ child = parent.paranoid_has_many_dependants.create(name: "child")
132
+
133
+ parent.destroy
134
+ assert child.destroyed?
135
+
136
+ parent.recover
137
+ assert_equal 1, parent.paranoid_has_many_dependants.count
138
+
139
+ child.reload
140
+ refute child.destroyed?
141
+ end
142
+
115
143
  # Rails does not allow saving deleted records
116
144
  def test_no_save_after_destroy
117
145
  paranoid = ParanoidString.first
@@ -124,6 +152,13 @@ class ParanoidTest < ParanoidBaseTest
124
152
  end
125
153
  end
126
154
 
155
+ def test_scope_chaining
156
+ assert_equal 3, ParanoidBoolean.unscoped.with_deleted.count
157
+ assert_equal 0, ParanoidBoolean.unscoped.only_deleted.count
158
+ assert_equal 0, ParanoidBoolean.with_deleted.only_deleted.count
159
+ assert_equal 3, ParanoidBoolean.with_deleted.with_deleted.count
160
+ end
161
+
127
162
  def setup_recursive_tests
128
163
  @paranoid_time_object = ParanoidTime.first
129
164
 
@@ -676,4 +711,26 @@ class ParanoidTest < ParanoidBaseTest
676
711
  assert_equal "explicit_table.deleted_at", ParanoidWithExplicitTableNameAfterMacro
677
712
  .paranoid_column_reference
678
713
  end
714
+
715
+ def test_deleted_after_time
716
+ ParanoidTime.first.destroy
717
+ assert_equal 0, ParanoidTime.deleted_after_time(1.hour.from_now).count
718
+ assert_equal 1, ParanoidTime.deleted_after_time(1.hour.ago).count
719
+ end
720
+
721
+ def test_deleted_before_time
722
+ ParanoidTime.first.destroy
723
+ assert_equal 1, ParanoidTime.deleted_before_time(1.hour.from_now).count
724
+ assert_equal 0, ParanoidTime.deleted_before_time(1.hour.ago).count
725
+ end
726
+
727
+ def test_deleted_inside_time_window
728
+ ParanoidTime.first.destroy
729
+ assert_equal 1, ParanoidTime.deleted_inside_time_window(1.minute.ago, 2.minutes).count
730
+ assert_equal 1,
731
+ ParanoidTime.deleted_inside_time_window(1.minute.from_now, 2.minutes).count
732
+ assert_equal 0, ParanoidTime.deleted_inside_time_window(3.minutes.ago, 1.minute).count
733
+ assert_equal 0,
734
+ ParanoidTime.deleted_inside_time_window(3.minutes.from_now, 1.minute).count
735
+ end
679
736
  end
@@ -2,9 +2,16 @@
2
2
 
3
3
  require "test_helper"
4
4
 
5
- class MultipleDefaultScopesTest < ParanoidBaseTest
5
+ class MultipleDefaultScopesTest < ActiveSupport::TestCase
6
6
  def setup
7
- setup_db
7
+ ActiveRecord::Schema.define(version: 1) do
8
+ create_table :paranoid_polygons do |t|
9
+ t.integer :sides
10
+ t.datetime :deleted_at
11
+
12
+ timestamps t
13
+ end
14
+ end
8
15
 
9
16
  ParanoidPolygon.create! sides: 3
10
17
  ParanoidPolygon.create! sides: 3
@@ -15,6 +22,10 @@ class MultipleDefaultScopesTest < ParanoidBaseTest
15
22
  assert_equal 4, ParanoidPolygon.unscoped.count
16
23
  end
17
24
 
25
+ def teardown
26
+ teardown_db
27
+ end
28
+
18
29
  def test_fake_removal_with_multiple_default_scope
19
30
  ParanoidPolygon.first.destroy
20
31
  assert_equal 2, ParanoidPolygon.count
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class DependentRecoveryTest < ActiveSupport::TestCase
6
+ class ParanoidForest < ActiveRecord::Base
7
+ acts_as_paranoid
8
+ has_many :paranoid_trees, dependent: :destroy
9
+ end
10
+
11
+ class ParanoidTree < ActiveRecord::Base
12
+ acts_as_paranoid
13
+ belongs_to :paranoid_forest, optional: false
14
+ end
15
+
16
+ def setup
17
+ ActiveRecord::Schema.define(version: 1) do
18
+ create_table :paranoid_forests do |t|
19
+ t.string :name
20
+ t.boolean :rainforest
21
+ t.datetime :deleted_at
22
+
23
+ timestamps t
24
+ end
25
+
26
+ create_table :paranoid_trees do |t|
27
+ t.integer :paranoid_forest_id
28
+ t.string :name
29
+ t.datetime :deleted_at
30
+
31
+ timestamps t
32
+ end
33
+ end
34
+ end
35
+
36
+ def teardown
37
+ teardown_db
38
+ end
39
+
40
+ def test_recover_dependent_records_with_required_belongs_to
41
+ forest = ParanoidForest.create! name: "forest"
42
+
43
+ tree = ParanoidTree.new name: "tree"
44
+ refute tree.valid?
45
+ tree.paranoid_forest = forest
46
+ assert tree.valid?
47
+ tree.save!
48
+
49
+ forest.destroy
50
+ forest.recover
51
+
52
+ assert_equal 1, ParanoidTree.count
53
+ end
54
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class DeprecatedBehaviorTest < ActiveSupport::TestCase
6
+ class StringlyParanoid < ActiveRecord::Base
7
+ acts_as_paranoid column_type: "string", column: "foo", recovery_value: "alive"
8
+ end
9
+
10
+ def setup
11
+ ActiveRecord::Schema.define(version: 1) do
12
+ create_table :stringly_paranoids do |t|
13
+ t.string :foo
14
+
15
+ timestamps t
16
+ end
17
+ end
18
+ end
19
+
20
+ def teardown
21
+ teardown_db
22
+ end
23
+
24
+ def test_recovery_value
25
+ record = StringlyParanoid.create!
26
+ record.destroy
27
+ record.recover
28
+ assert_equal "alive", record.paranoid_value
29
+ end
30
+ end
data/test/test_helper.rb CHANGED
@@ -32,8 +32,12 @@ I18n.enforce_available_locales = true
32
32
  ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
33
33
  ActiveRecord::Schema.verbose = false
34
34
 
35
+ log_dir = File.expand_path("../log/", __dir__)
36
+ FileUtils.mkdir_p log_dir
37
+ file_path = File.join(log_dir, "test.log")
38
+ ActiveRecord::Base.logger = Logger.new(file_path)
39
+
35
40
  # rubocop:disable Metrics/AbcSize
36
- # rubocop:disable Metrics/MethodLength
37
41
  def setup_db
38
42
  ActiveRecord::Schema.define(version: 1) do # rubocop:disable Metrics/BlockLength
39
43
  create_table :paranoid_times do |t|
@@ -169,22 +173,6 @@ def setup_db
169
173
  timestamps t
170
174
  end
171
175
 
172
- create_table :paranoid_forests do |t|
173
- t.string :name
174
- t.boolean :rainforest
175
- t.datetime :deleted_at
176
-
177
- timestamps t
178
- end
179
-
180
- create_table :paranoid_trees do |t|
181
- t.integer :paranoid_forest_id
182
- t.string :name
183
- t.datetime :deleted_at
184
-
185
- timestamps t
186
- end
187
-
188
176
  create_table :paranoid_polygons do |t|
189
177
  t.integer :sides
190
178
  t.datetime :deleted_at
@@ -241,11 +229,18 @@ def setup_db
241
229
 
242
230
  timestamps t
243
231
  end
232
+
233
+ create_table :paranoid_with_serialized_columns do |t|
234
+ t.string :name
235
+ t.datetime :deleted_at
236
+ t.string :colors
237
+
238
+ timestamps t
239
+ end
244
240
  end
245
241
  end
246
- # rubocop:enable Metrics/AbcSize
247
- # rubocop:enable Metrics/MethodLength
248
242
 
243
+ # rubocop:enable Metrics/AbcSize
249
244
  def timestamps(table)
250
245
  table.column :created_at, :timestamp, null: false
251
246
  table.column :updated_at, :timestamp, null: false
@@ -290,6 +285,7 @@ class ParanoidString < ActiveRecord::Base
290
285
  end
291
286
 
292
287
  class NotParanoid < ActiveRecord::Base
288
+ has_many :paranoid_times
293
289
  end
294
290
 
295
291
  class ParanoidNoDoubleTapDestroysFully < ActiveRecord::Base
@@ -524,22 +520,6 @@ class ParanoidBaseTest < ActiveSupport::TestCase
524
520
  end
525
521
  end
526
522
 
527
- class ParanoidForest < ActiveRecord::Base
528
- acts_as_paranoid
529
-
530
- ActiveRecord::Base.logger = Logger.new(StringIO.new)
531
-
532
- scope :rainforest, -> { where(rainforest: true) }
533
-
534
- has_many :paranoid_trees, dependent: :destroy
535
- end
536
-
537
- class ParanoidTree < ActiveRecord::Base
538
- acts_as_paranoid
539
- belongs_to :paranoid_forest
540
- validates_presence_of :name
541
- end
542
-
543
523
  class ParanoidPolygon < ActiveRecord::Base
544
524
  acts_as_paranoid
545
525
  default_scope { where("sides = ?", 3) }
@@ -563,3 +543,12 @@ class ParanoidWithExplicitTableNameAfterMacro < ActiveRecord::Base
563
543
  acts_as_paranoid
564
544
  self.table_name = "explicit_table"
565
545
  end
546
+
547
+ class ParanoidWithSerializedColumn < ActiveRecord::Base
548
+ acts_as_paranoid
549
+ validates_as_paranoid
550
+
551
+ serialize :colors, Array
552
+
553
+ validates_uniqueness_of_without_deleted :colors
554
+ end
@@ -2,9 +2,63 @@
2
2
 
3
3
  require "test_helper"
4
4
 
5
- class RelationsTest < ParanoidBaseTest
5
+ class RelationsTest < ActiveSupport::TestCase
6
+ class ParanoidForest < ActiveRecord::Base
7
+ acts_as_paranoid
8
+
9
+ scope :rainforest, -> { where(rainforest: true) }
10
+
11
+ has_many :paranoid_trees, dependent: :destroy
12
+ end
13
+
14
+ class ParanoidTree < ActiveRecord::Base
15
+ acts_as_paranoid
16
+ belongs_to :paranoid_forest
17
+ validates_presence_of :name
18
+ end
19
+
20
+ class NotParanoidBowl < ActiveRecord::Base
21
+ has_many :paranoid_chocolates, dependent: :destroy
22
+ end
23
+
24
+ class ParanoidChocolate < ActiveRecord::Base
25
+ acts_as_paranoid
26
+ belongs_to :not_paranoid_bowl
27
+ validates_presence_of :name
28
+ end
29
+
6
30
  def setup
7
- setup_db
31
+ ActiveRecord::Schema.define(version: 1) do
32
+ create_table :paranoid_forests do |t|
33
+ t.string :name
34
+ t.boolean :rainforest
35
+ t.datetime :deleted_at
36
+
37
+ timestamps t
38
+ end
39
+
40
+ create_table :paranoid_trees do |t|
41
+ t.integer :paranoid_forest_id
42
+ t.string :name
43
+ t.datetime :deleted_at
44
+
45
+ timestamps t
46
+ end
47
+
48
+ create_table :not_paranoid_bowls do |t|
49
+ t.string :name
50
+
51
+ timestamps t
52
+ end
53
+
54
+ create_table :paranoid_chocolates do |t|
55
+ t.integer :not_paranoid_bowl_id
56
+ t.string :name
57
+ t.datetime :deleted_at
58
+
59
+ timestamps t
60
+ end
61
+ end
8
62
 
9
63
  @paranoid_forest_1 = ParanoidForest.create! name: "ParanoidForest #1"
10
64
  @paranoid_forest_2 = ParanoidForest.create! name: "ParanoidForest #2", rainforest: true
@@ -21,6 +75,10 @@ class RelationsTest < ParanoidBaseTest
21
75
  assert_equal 4, ParanoidTree.count
22
76
  end
23
77
 
78
+ def teardown
79
+ teardown_db
80
+ end
81
+
24
82
  def test_filtering_with_scopes
25
83
  assert_equal 2, ParanoidForest.rainforest.with_deleted.count
26
84
  assert_equal 2, ParanoidForest.with_deleted.rainforest.count
@@ -83,13 +141,29 @@ class RelationsTest < ParanoidBaseTest
83
141
  assert_equal 1, ParanoidForest.rainforest.only_deleted.count
84
142
 
85
143
  # destroy_all: through a relation
86
- @paranoid_forest_2.paranoid_trees.order(:id).destroy_all
144
+ @paranoid_forest_2.paranoid_trees.destroy_all
87
145
  assert_equal 0, @paranoid_forest_2.paranoid_trees.count
88
146
  assert_equal 2, @paranoid_forest_2.paranoid_trees.with_deleted.count
89
147
  end
90
148
 
91
- def test_real_removal_through_relation_with_destroy_bang
92
- # Relation.destroy!: aliased to delete
149
+ def test_fake_removal_through_has_many_relation_of_non_paranoid_model
150
+ not_paranoid = NotParanoidBowl.create! name: "NotParanoid #1"
151
+ not_paranoid.paranoid_chocolates.create! name: "ParanoidChocolate #1"
152
+ not_paranoid.paranoid_chocolates.create! name: "ParanoidChocolate #2"
153
+
154
+ not_paranoid.paranoid_chocolates.destroy_all
155
+ assert_equal 0, not_paranoid.paranoid_chocolates.count
156
+ assert_equal 2, not_paranoid.paranoid_chocolates.with_deleted.count
157
+ end
158
+
159
+ def test_real_removal_through_relation_with_destroy_fully
160
+ ParanoidForest.rainforest.destroy_fully!(@paranoid_forest_3)
161
+ assert_equal 1, ParanoidForest.rainforest.count
162
+ assert_equal 1, ParanoidForest.rainforest.with_deleted.count
163
+ assert_equal 0, ParanoidForest.rainforest.only_deleted.count
164
+ end
165
+
166
+ def test_real_removal_through_relation_with_deprecated_destroy!
93
167
  ParanoidForest.rainforest.destroy!(@paranoid_forest_3)
94
168
  assert_equal 1, ParanoidForest.rainforest.count
95
169
  assert_equal 1, ParanoidForest.rainforest.with_deleted.count
@@ -99,7 +173,7 @@ class RelationsTest < ParanoidBaseTest
99
173
  def test_two_step_real_removal_through_relation_with_destroy
100
174
  # destroy: two-step through a relation
101
175
  paranoid_tree = @paranoid_forest_1.paranoid_trees.first
102
- @paranoid_forest_1.paranoid_trees.order(:id).destroy(paranoid_tree.id)
176
+ @paranoid_forest_1.paranoid_trees.destroy(paranoid_tree.id)
103
177
  @paranoid_forest_1.paranoid_trees.only_deleted.destroy(paranoid_tree.id)
104
178
  assert_equal 1, @paranoid_forest_1.paranoid_trees.count
105
179
  assert_equal 1, @paranoid_forest_1.paranoid_trees.with_deleted.count
@@ -108,7 +182,7 @@ class RelationsTest < ParanoidBaseTest
108
182
 
109
183
  def test_two_step_real_removal_through_relation_with_destroy_all
110
184
  # destroy_all: two-step through a relation
111
- @paranoid_forest_1.paranoid_trees.order(:id).destroy_all
185
+ @paranoid_forest_1.paranoid_trees.destroy_all
112
186
  @paranoid_forest_1.paranoid_trees.only_deleted.destroy_all
113
187
  assert_equal 0, @paranoid_forest_1.paranoid_trees.count
114
188
  assert_equal 0, @paranoid_forest_1.paranoid_trees.with_deleted.count
@@ -117,7 +191,7 @@ class RelationsTest < ParanoidBaseTest
117
191
 
118
192
  def test_real_removal_through_relation_with_delete_all_bang
119
193
  # delete_all!: through a relation
120
- @paranoid_forest_2.paranoid_trees.order(:id).delete_all!
194
+ @paranoid_forest_2.paranoid_trees.delete_all!
121
195
  assert_equal 0, @paranoid_forest_2.paranoid_trees.count
122
196
  assert_equal 0, @paranoid_forest_2.paranoid_trees.with_deleted.count
123
197
  assert_equal 0, @paranoid_forest_2.paranoid_trees.only_deleted.count
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class TableNamespaceTest < ActiveSupport::TestCase
6
+ module Paranoid
7
+ class Blob < ActiveRecord::Base
8
+ acts_as_paranoid
9
+
10
+ validates_presence_of :name
11
+
12
+ def self.table_name_prefix
13
+ "paranoid_"
14
+ end
15
+ end
16
+ end
17
+
18
+ def setup
19
+ ActiveRecord::Schema.define(version: 1) do
20
+ create_table :paranoid_blobs do |t|
21
+ t.string :name
22
+ t.datetime :deleted_at
23
+
24
+ timestamps t
25
+ end
26
+ end
27
+ end
28
+
29
+ def teardown
30
+ teardown_db
31
+ end
32
+
33
+ def test_correct_table_name
34
+ assert_equal "paranoid_blobs", Paranoid::Blob.table_name
35
+
36
+ b = Paranoid::Blob.new(name: "hello!")
37
+ b.save!
38
+ assert_equal b, Paranoid::Blob.first
39
+ end
40
+ end
@@ -23,6 +23,24 @@ class ValidatesUniquenessTest < ParanoidBaseTest
23
23
  end
24
24
  end
25
25
 
26
+ def test_validate_serialized_attribute_without_deleted
27
+ ParanoidWithSerializedColumn.create!(name: "ParanoidWithSerializedColumn #1",
28
+ colors: %w[Cyan Maroon])
29
+ record = ParanoidWithSerializedColumn.new(name: "ParanoidWithSerializedColumn #2")
30
+ record.colors = %w[Cyan Maroon]
31
+ refute record.valid?
32
+
33
+ record.colors = %w[Beige Turquoise]
34
+ assert record.valid?
35
+ end
36
+
37
+ def test_updated_serialized_attribute_validated_without_deleted
38
+ record = ParanoidWithSerializedColumn.create!(name: "ParanoidWithSerializedColumn #1",
39
+ colors: %w[Cyan Maroon])
40
+ record.update!(colors: %w[Beige Turquoise])
41
+ assert record.valid?
42
+ end
43
+
26
44
  def test_models_with_scoped_validations_can_be_multiply_deleted
27
45
  model_a = ParanoidWithScopedValidation.create(name: "Model A", category: "Category A")
28
46
  model_b = ParanoidWithScopedValidation.create(name: "Model B", category: "Category B")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_paranoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zachary Scott
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-04-16 00:00:00.000000000 Z
13
+ date: 2021-05-02 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -196,9 +196,12 @@ files:
196
196
  - test/test_associations.rb
197
197
  - test/test_core.rb
198
198
  - test/test_default_scopes.rb
199
+ - test/test_dependent_recovery.rb
200
+ - test/test_deprecated_behavior.rb
199
201
  - test/test_helper.rb
200
202
  - test/test_inheritance.rb
201
203
  - test/test_relations.rb
204
+ - test/test_table_namespace.rb
202
205
  - test/test_validations.rb
203
206
  homepage: https://github.com/ActsAsParanoid/acts_as_paranoid
204
207
  licenses:
@@ -228,7 +231,10 @@ test_files:
228
231
  - test/test_associations.rb
229
232
  - test/test_core.rb
230
233
  - test/test_default_scopes.rb
234
+ - test/test_dependent_recovery.rb
235
+ - test/test_deprecated_behavior.rb
231
236
  - test/test_helper.rb
232
237
  - test/test_inheritance.rb
233
238
  - test/test_relations.rb
239
+ - test/test_table_namespace.rb
234
240
  - test/test_validations.rb