paranoia 2.0.4 → 2.0.5

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: 699077f31f4cc413787d2132a6a36fc18051392d
4
- data.tar.gz: 3ad7c463ced3e000c097c76c3b77d3378f4100e7
3
+ metadata.gz: 1bd7736219b3968e111105e25de4553f328a744c
4
+ data.tar.gz: 3df48b961122c079b3565a2b3cc1f48b7b940fea
5
5
  SHA512:
6
- metadata.gz: d9b72b34e364d624fc9b0152f6be5d57903f87b0aed34ac6a010cb7bc0b117df0954db926595f67873cb43e3e6b5542ac4a07a6626e0d59de4af9d4c2874e132
7
- data.tar.gz: 423bdba7a858510b877973e0746d0fbe896f96d43b6eae06793da88e9566755e53b9740bcaa1a82b883a12ac1876927f47a60cb58626200f02ec2678a89af6af
6
+ metadata.gz: 1df4bb3d86bcf7dcb932a11139acc9d68b8edadea446f64acdf370e9cde9b455684bf261f839949a826fcba3a77490252c6b15d027ad1cc4dc7853ca6ad702a7
7
+ data.tar.gz: 61751265887e6a270b906433b9b3b03688344d683492abb2cbf4d91fa03f10f2b3db9c02b9e506b66175366e0581702671d2c54311b960dcdcba93238d2e9856
@@ -8,3 +8,4 @@ rvm:
8
8
  env:
9
9
  - RAILS='~> 4.0.8'
10
10
  - RAILS='~> 4.1.4'
11
+ - RAILS='~> 4.2.0'
@@ -1,5 +1,14 @@
1
1
  # paranoia Changelog
2
2
 
3
+ ## 2.0.5 (2015-01-22)
4
+
5
+ ### Bug fixes
6
+
7
+ * Fix restoring polymorphic has_one relationships [#189](https://github.com/radar/paranoia/pull/189) [#174](https://github.com/radar/paranoia/issues/174) [Patrick Koperwas](https://github.com/PatKoperwas)
8
+ * Fix errors when restoring a model with a has_one against a non-paranoid model. [#168](https://github.com/radar/paranoia/pull/168) [Shreyas Agarwal](https://github.com/shreyas123)
9
+ * Fix rspec 2 compatibility [#197](https://github.com/radar/paranoia/pull/197) [Emil Sågfors](https://github.com/lime)
10
+ * Fix some deprecation warnings on rails 4.2 [Sergey Alekseev](https://github.com/sergey-alekseev)
11
+
3
12
  ## 2.0.4 (2014-12-02)
4
13
 
5
14
  ### Features
data/Gemfile CHANGED
@@ -9,7 +9,7 @@ platforms :rbx do
9
9
  gem 'rubinius-developer_tools'
10
10
  end
11
11
 
12
- rails = ENV['RAILS'] || '~> 4.1.4'
12
+ rails = ENV['RAILS'] || '~> 4.2.0'
13
13
 
14
14
  gem 'rails', rails
15
15
 
data/README.md CHANGED
@@ -4,7 +4,7 @@ Paranoia is a re-implementation of [acts\_as\_paranoid](http://github.com/techno
4
4
 
5
5
  You would use either plugin / gem if you wished that when you called `destroy` on an Active Record object that it didn't actually destroy it, but just *hide* the record. Paranoia does this by setting a `deleted_at` field to the current time when you `destroy` a record, and hides it by scoping all queries on your model to only include records which do not have a `deleted_at` field.
6
6
 
7
- If you wish to actually destroy an object you may call `really_destroy!`. **WARNING**: This will also *really destroy* all `dependent: destroy` records, so please aim this method away from face when using.**
7
+ If you wish to actually destroy an object you may call `really_destroy!`. **WARNING**: This will also *really destroy* all `dependent: destroy` records, so please aim this method away from face when using.
8
8
 
9
9
  If a record has `has_many` associations defined AND those associations have `dependent: :destroy` set on them, then they will also be soft-deleted if `acts_as_paranoid` is set, otherwise the normal destroy will be called.
10
10
 
@@ -140,6 +140,8 @@ If you want to check if a record is soft-deleted:
140
140
 
141
141
  ``` ruby
142
142
  client.destroyed?
143
+ # or
144
+ client.deleted?
143
145
  ```
144
146
 
145
147
  If you want to restore a record:
@@ -142,9 +142,20 @@ module Paranoia
142
142
  end
143
143
 
144
144
  if association_data.nil? && association.macro.to_s == "has_one"
145
- association_class_name = association.options[:class_name].present? ? association.options[:class_name] : association.name.to_s.camelize
146
- association_foreign_key = association.options[:foreign_key].present? ? association.options[:foreign_key] : "#{self.class.name.to_s.underscore}_id"
147
- Object.const_get(association_class_name).only_deleted.where(association_foreign_key => self.id).first.try(:restore, recursive: true)
145
+ association_class_name = association.class_name
146
+ association_foreign_key = association.foreign_key
147
+
148
+ if association.type
149
+ association_polymorphic_type = association.type
150
+ association_find_conditions = { association_polymorphic_type => self.class.name.to_s, association_foreign_key => self.id }
151
+ else
152
+ association_find_conditions = { association_foreign_key => self.id }
153
+ end
154
+
155
+ association_class = Object.const_get(association_class_name)
156
+ if association_class.paranoid?
157
+ association_class.only_deleted.where(association_find_conditions).first.try!(:restore, recursive: true)
158
+ end
148
159
  end
149
160
  end
150
161
 
@@ -4,10 +4,20 @@ require 'rspec/expectations'
4
4
  RSpec::Matchers.define :act_as_paranoid do
5
5
  match { |subject| subject.class.ancestors.include?(Paranoia) }
6
6
 
7
- failure_message { "expected #{subject.class} to use `acts_as_paranoid`" }
8
- failure_message_when_negated { "expected #{subject.class} not to use `acts_as_paranoid`" }
7
+ failure_message_proc = lambda do
8
+ "expected #{subject.class} to use `acts_as_paranoid`"
9
+ end
9
10
 
10
- # RSpec 2 compatibility:
11
- alias_method :failure_message_for_should, :failure_message
12
- alias_method :failure_message_for_should_not, :failure_message_when_negated
11
+ failure_message_when_negated_proc = lambda do
12
+ "expected #{subject.class} not to use `acts_as_paranoid`"
13
+ end
14
+
15
+ if respond_to?(:failure_message_when_negated)
16
+ failure_message(&failure_message_proc)
17
+ failure_message_when_negated(&failure_message_when_negated_proc)
18
+ else
19
+ # RSpec 2 compatibility:
20
+ failure_message_for_should(&failure_message_proc)
21
+ failure_message_for_should_not(&failure_message_when_negated_proc)
22
+ end
13
23
  end
@@ -1,3 +1,3 @@
1
1
  module Paranoia
2
- VERSION = "2.0.4"
2
+ VERSION = "2.0.5"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require 'active_record'
2
+ ActiveRecord::Base.raise_in_transactional_callbacks = true if ActiveRecord::VERSION::STRING >= '4.2'
2
3
 
3
4
  test_framework = if ActiveRecord::VERSION::STRING >= "4.1"
4
5
  require 'minitest/autorun'
@@ -20,6 +21,7 @@ def setup!
20
21
  ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_model_with_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER)'
21
22
  ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_model_with_anthor_class_name_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER)'
22
23
  ActiveRecord::Base.connection.execute 'CREATE TABLE paranoid_model_with_foreign_key_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, deleted_at DATETIME, has_one_foreign_key_id INTEGER)'
24
+ ActiveRecord::Base.connection.execute 'CREATE TABLE not_paranoid_model_with_belongs (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER, paranoid_model_with_has_one_id INTEGER)'
23
25
  ActiveRecord::Base.connection.execute 'CREATE TABLE featureful_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME, name VARCHAR(32))'
24
26
  ActiveRecord::Base.connection.execute 'CREATE TABLE plain_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
25
27
  ActiveRecord::Base.connection.execute 'CREATE TABLE callback_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
@@ -32,6 +34,7 @@ def setup!
32
34
  ActiveRecord::Base.connection.execute 'CREATE TABLE custom_column_models (id INTEGER NOT NULL PRIMARY KEY, destroyed_at DATETIME)'
33
35
  ActiveRecord::Base.connection.execute 'CREATE TABLE custom_sentinel_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME NOT NULL)'
34
36
  ActiveRecord::Base.connection.execute 'CREATE TABLE non_paranoid_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER)'
37
+ ActiveRecord::Base.connection.execute 'CREATE TABLE polymorphic_models (id INTEGER NOT NULL PRIMARY KEY, parent_id INTEGER, parent_type STRING, deleted_at DATETIME)'
35
38
  end
36
39
 
37
40
  class WithDifferentConnection < ActiveRecord::Base
@@ -454,9 +457,12 @@ class ParanoiaTest < test_framework
454
457
  belongsTo = ParanoidModelWithBelong.create
455
458
  anthorClassName = ParanoidModelWithAnthorClassNameBelong.create
456
459
  foreignKey = ParanoidModelWithForeignKeyBelong.create
460
+ notParanoidModel = NotParanoidModelWithBelong.create
461
+
457
462
  hasOne.paranoid_model_with_belong = belongsTo
458
463
  hasOne.class_name_belong = anthorClassName
459
464
  hasOne.paranoid_model_with_foreign_key_belong = foreignKey
465
+ hasOne.not_paranoid_model_with_belong = notParanoidModel
460
466
  hasOne.save!
461
467
 
462
468
  hasOne.destroy
@@ -469,6 +475,7 @@ class ParanoiaTest < test_framework
469
475
 
470
476
  assert_equal true, hasOne.reload.deleted_at.nil?
471
477
  assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}"
478
+ assert_equal true, notParanoidModel.destroyed?
472
479
  assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record"
473
480
  assert ParanoidModelWithAnthorClassNameBelong.with_deleted.reload.count != 0, "There should be an other record"
474
481
  assert ParanoidModelWithForeignKeyBelong.with_deleted.reload.count != 0, "There should be a foreign_key record"
@@ -480,9 +487,12 @@ class ParanoiaTest < test_framework
480
487
  belongsTo = ParanoidModelWithBelong.create
481
488
  anthorClassName = ParanoidModelWithAnthorClassNameBelong.create
482
489
  foreignKey = ParanoidModelWithForeignKeyBelong.create
490
+ notParanoidModel = NotParanoidModelWithBelong.create
491
+
483
492
  hasOne.paranoid_model_with_belong = belongsTo
484
493
  hasOne.class_name_belong = anthorClassName
485
494
  hasOne.paranoid_model_with_foreign_key_belong = foreignKey
495
+ hasOne.not_paranoid_model_with_belong = notParanoidModel
486
496
  hasOne.save!
487
497
 
488
498
  hasOne.destroy
@@ -496,6 +506,7 @@ class ParanoiaTest < test_framework
496
506
 
497
507
  assert_equal true, hasOne.reload.deleted_at.nil?
498
508
  assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}"
509
+ assert_equal true, notParanoidModel.destroyed?
499
510
  assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record"
500
511
  assert ParanoidModelWithAnthorClassNameBelong.with_deleted.reload.count != 0, "There should be an other record"
501
512
  assert ParanoidModelWithForeignKeyBelong.with_deleted.reload.count != 0, "There should be a foreign_key record"
@@ -507,9 +518,12 @@ class ParanoiaTest < test_framework
507
518
  belongsTo = ParanoidModelWithBelong.create
508
519
  anthorClassName = ParanoidModelWithAnthorClassNameBelong.create
509
520
  foreignKey = ParanoidModelWithForeignKeyBelong.create
521
+ notParanoidModel = NotParanoidModelWithBelong.create
522
+
510
523
  hasOne.paranoid_model_with_belong = belongsTo
511
524
  hasOne.class_name_belong = anthorClassName
512
525
  hasOne.paranoid_model_with_foreign_key_belong = foreignKey
526
+ hasOne.not_paranoid_model_with_belong = notParanoidModel
513
527
  hasOne.save!
514
528
 
515
529
  hasOne.destroy
@@ -522,6 +536,7 @@ class ParanoiaTest < test_framework
522
536
 
523
537
  assert_equal true, hasOne.reload.deleted_at.nil?
524
538
  assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}"
539
+ assert_equal true, notParanoidModel.destroyed?
525
540
  assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record"
526
541
  assert ParanoidModelWithAnthorClassNameBelong.with_deleted.reload.count != 0, "There should be an other record"
527
542
  assert ParanoidModelWithForeignKeyBelong.with_deleted.reload.count != 0, "There should be a foreign_key record"
@@ -535,7 +550,7 @@ class ParanoiaTest < test_framework
535
550
 
536
551
  # Does it raise NoMethodException on restore of nil
537
552
  hasOne.restore(:recursive => true)
538
-
553
+
539
554
  assert hasOne.reload.deleted_at.nil?
540
555
  end
541
556
 
@@ -549,7 +564,7 @@ class ParanoiaTest < test_framework
549
564
 
550
565
  hasOnes.each(&:destroy)
551
566
 
552
- ParanoidModelWithHasOne.restore(hasOnes[1], :recursive => true)
567
+ ParanoidModelWithHasOne.restore(hasOnes[1].id, :recursive => true)
553
568
  hasOnes.each(&:reload)
554
569
  belongsTos.each(&:reload)
555
570
 
@@ -564,14 +579,14 @@ class ParanoiaTest < test_framework
564
579
  def test_has_one_really_destroy_with_nil
565
580
  model = ParanoidModelWithHasOne.create
566
581
  model.really_destroy!
567
-
582
+
568
583
  refute ParanoidModelWithBelong.unscoped.exists?(model.id)
569
584
  end
570
-
585
+
571
586
  def test_has_one_really_destroy_with_record
572
587
  model = ParanoidModelWithHasOne.create { |record| record.build_paranoid_model_with_belong }
573
588
  model.really_destroy!
574
-
589
+
575
590
  refute ParanoidModelWithBelong.unscoped.exists?(model.id)
576
591
  end
577
592
 
@@ -592,12 +607,14 @@ class ParanoiaTest < test_framework
592
607
  end
593
608
 
594
609
  def test_i_am_the_destroyer
595
- output = capture(:stdout) { ParanoidModel.I_AM_THE_DESTROYER! }
596
- assert_equal %Q{
610
+ expected = %Q{
597
611
  Sharon: "There should be a method called I_AM_THE_DESTROYER!"
598
612
  Ryan: "What should this method do?"
599
613
  Sharon: "It should fix all the spelling errors on the page!"
600
- }, output
614
+ }
615
+ assert_output expected do
616
+ ParanoidModel.I_AM_THE_DESTROYER!
617
+ end
601
618
  end
602
619
 
603
620
  def test_destroy_fails_if_callback_raises_exception
@@ -654,6 +671,34 @@ class ParanoiaTest < test_framework
654
671
  setup!
655
672
  end
656
673
 
674
+ def test_restore_recursive_on_polymorphic_has_one_association
675
+ parent = ParentModel.create
676
+ polymorphic = PolymorphicModel.create(parent: parent)
677
+
678
+ parent.destroy
679
+
680
+ assert_equal 0, polymorphic.class.count
681
+
682
+ parent.restore(recursive: true)
683
+
684
+ assert_equal 1, polymorphic.class.count
685
+ end
686
+
687
+ # Ensure that we're checking parent_type when restoring
688
+ def test_missing_restore_recursive_on_polymorphic_has_one_association
689
+ parent = ParentModel.create
690
+ polymorphic = PolymorphicModel.create(parent_id: parent.id, parent_type: 'ParanoidModel')
691
+
692
+ parent.destroy
693
+ polymorphic.destroy
694
+
695
+ assert_equal 0, polymorphic.class.count
696
+
697
+ parent.restore(recursive: true)
698
+
699
+ assert_equal 0, polymorphic.class.count
700
+ end
701
+
657
702
  private
658
703
  def get_featureful_model
659
704
  FeaturefulModel.new(:name => "not empty")
@@ -706,6 +751,7 @@ class ParentModel < ActiveRecord::Base
706
751
  has_many :very_related_models, :class_name => 'RelatedModel', dependent: :destroy
707
752
  has_many :non_paranoid_models, dependent: :destroy
708
753
  has_many :asplode_models, dependent: :destroy
754
+ has_one :polymorphic_model, as: :parent, dependent: :destroy
709
755
  end
710
756
 
711
757
  class RelatedModel < ActiveRecord::Base
@@ -761,6 +807,7 @@ class ParanoidModelWithHasOne < ParanoidModel
761
807
  has_one :paranoid_model_with_belong, :dependent => :destroy
762
808
  has_one :class_name_belong, :dependent => :destroy, :class_name => "ParanoidModelWithAnthorClassNameBelong"
763
809
  has_one :paranoid_model_with_foreign_key_belong, :dependent => :destroy, :foreign_key => "has_one_foreign_key_id"
810
+ has_one :not_paranoid_model_with_belong, :dependent => :destroy
764
811
  end
765
812
 
766
813
  class ParanoidModelWithBelong < ActiveRecord::Base
@@ -778,6 +825,10 @@ class ParanoidModelWithForeignKeyBelong < ActiveRecord::Base
778
825
  belongs_to :paranoid_model_with_has_one
779
826
  end
780
827
 
828
+ class NotParanoidModelWithBelong < ActiveRecord::Base
829
+ belongs_to :paranoid_model_with_has_one
830
+ end
831
+
781
832
  class FlaggedModel < PlainModel
782
833
  acts_as_paranoid :flag_column => :is_deleted
783
834
  end
@@ -786,6 +837,8 @@ class FlaggedModelWithCustomIndex < PlainModel
786
837
  acts_as_paranoid :flag_column => :is_deleted, :indexed_column => :is_deleted
787
838
  end
788
839
 
840
+
841
+
789
842
  class AsplodeModel < ActiveRecord::Base
790
843
  acts_as_paranoid
791
844
  before_destroy do |r|
@@ -795,3 +848,8 @@ end
795
848
 
796
849
  class NoConnectionModel < ActiveRecord::Base
797
850
  end
851
+
852
+ class PolymorphicModel < ActiveRecord::Base
853
+ acts_as_paranoid
854
+ belongs_to :parent, polymorphic: true
855
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paranoia
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.4
4
+ version: 2.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - radarlistener@gmail.com
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-03 00:00:00.000000000 Z
11
+ date: 2015-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -94,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  version: 1.3.6
95
95
  requirements: []
96
96
  rubyforge_project: paranoia
97
- rubygems_version: 2.2.2
97
+ rubygems_version: 2.4.5
98
98
  signing_key:
99
99
  specification_version: 4
100
100
  summary: Paranoia is a re-implementation of acts_as_paranoid for Rails 3, using much,