destroyed_at 2.0.0 → 2.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: 7f228171fc8186eacb4a2b03091ac1da77801e98
4
- data.tar.gz: 0e4ab1aadcc01078dd0b1decd2efaa2087131ee8
3
+ metadata.gz: e3fb7f279e758ccac81977388fb7231371b6dbe4
4
+ data.tar.gz: 4eae1c95bc510f1ba3bafce736b5741b9f5ed27e
5
5
  SHA512:
6
- metadata.gz: b3c50322b6247e7baee90999128c42a8731812c4d37ba346e183f46c3dc54d6208740e5eacbf2c937a840f1e561d4ee5841cd5af7cae5824a7e5bf880e4a531f
7
- data.tar.gz: 86f3b2fe4a8c15d9c5a1edb217161fdbe9377bfdcc970560d787dc6b63a7740e14330eee37a20f00b819d514253be378fd5a0dc7fce0652286b7aacf6e33ab6a
6
+ metadata.gz: 182864cb375de9bfaa99a612483d441f3b84c5d865447dcee45d06ef0fb7d7ba0c93d0bf7e0da40ba69d73db9f04e5721f564b8eb7d2c1807bdc0c8e54cff90e
7
+ data.tar.gz: f4c85b295b4a463a4ddcd80ac15e4ba375de63e2203b3a0e1879fa9cecbb75750858c837974a81449a178544d53cfb0daa1041234ab6667ce2e9300c265fee09
@@ -1,3 +1,11 @@
1
+ ## 2.1.0
2
+ * Allows `#mark_for_destruction` to be passed a timestamp which will be
3
+ used for future calls to `destroy`. Thanks
4
+ [anarchocurious](https://github.com/anarchocurious)!
5
+ * Fixes polymorphic associations for dependencies that don't have the
6
+ same `destroyed_at` timestamp. Thanks
7
+ [nashby](https://github.com/nashby)!
8
+
1
9
  ## 2.0.0
2
10
  * Associated models that do not include `DestroyedAt` will now be
3
11
  **deleted** via `dependent: :destroy`. Previously, this was only the
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # DestroyedAt #
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/dockyard/destroyed_at.png?branch=master)](http://travis-ci.org/dockyard/destroyed_at)
4
- [![Dependency Status](https://gemnasium.com/dockyard/destroyed_at.png?travis)](https://gemnasium.com/dockyard/destroyed_at)
5
- [![Code Climate](https://codeclimate.com/github/dockyard/destroyed_at.png)](https://codeclimate.com/github/dockyard/destroyed_at)
3
+ [![Build Status](https://travis-ci.org/DockYard/ruby-destroyed_at.svg?branch=master)](http://travis-ci.org/dockyard/ruby-destroyed_at)
4
+ [![Dependency Status](https://gemnasium.com/dockyard/ruby-destroyed_at.png?travis)](https://gemnasium.com/dockyard/ruby-destroyed_at)
5
+ [![Code Climate](https://codeclimate.com/github/dockyard/ruby-destroyed_at/badges/gpa.svg)](https://codeclimate.com/github/dockyard/ruby-destroyed_at)
6
6
 
7
7
  ## Looking for help? ##
8
8
 
@@ -39,7 +39,7 @@ module DestroyedAt
39
39
 
40
40
  # Set an object's destroyed_at time.
41
41
  def destroy(timestamp = nil)
42
- timestamp ||= current_time_from_proper_timezone
42
+ timestamp ||= @marked_for_destruction_at || current_time_from_proper_timezone
43
43
  raw_write_attribute(:destroyed_at, timestamp)
44
44
  run_callbacks(:destroy) do
45
45
  destroy_associations
@@ -48,6 +48,12 @@ module DestroyedAt
48
48
  end
49
49
  end
50
50
 
51
+ def mark_for_destruction(timestamp = nil)
52
+ @marked_for_destruction_at = timestamp
53
+
54
+ super()
55
+ end
56
+
51
57
  # Set an object's destroyed_at time to nil.
52
58
  def restore
53
59
  state = nil
@@ -1,7 +1,7 @@
1
1
  module DestroyedAt
2
2
  module HasOneAssociation
3
3
  def delete(method = options[:dependent])
4
- if DestroyedAt.has_destroy_at?(target) && load_target && method == :destroy
4
+ if load_target && DestroyedAt.has_destroy_at?(target) && method == :destroy
5
5
  DestroyedAt.destroy_target_of_association(owner, target)
6
6
  else
7
7
  super
@@ -1,3 +1,3 @@
1
1
  module DestroyedAt
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -81,6 +81,21 @@ describe 'destroying an activerecord instance' do
81
81
  Author.count.must_equal 0
82
82
  Avatar.count.must_equal 0
83
83
  end
84
+
85
+ it 'destroys child with the correct datetime through an an autosaving association' do
86
+ datetime = 10.minutes.ago
87
+
88
+ commenter = Commenter.create
89
+
90
+ comment = commenter.comments.build
91
+ commenter.save
92
+
93
+ comment.mark_for_destruction(datetime)
94
+ commenter.save
95
+
96
+ comment.destroyed_at.must_equal datetime
97
+ end
98
+
84
99
  end
85
100
 
86
101
  describe 'restoring an activerecord instance' do
@@ -108,6 +123,34 @@ describe 'restoring an activerecord instance' do
108
123
  initial_count.must_equal post.validation_count
109
124
  end
110
125
 
126
+ it 'restores polymorphic has_many relation with DestroyedAt' do
127
+ comment = Comment.create
128
+ Like.create(likeable: comment)
129
+ comment.destroy
130
+
131
+ Comment.count.must_equal 0
132
+ Like.count.must_equal 0
133
+
134
+ comment.reload
135
+ comment.restore
136
+ Comment.count.must_equal 1
137
+ Like.count.must_equal 1
138
+ end
139
+
140
+ it 'restores polymorphic has_one relation with DestroyedAt' do
141
+ post = Post.create
142
+ Like.create(likeable: post)
143
+ post.destroy
144
+
145
+ Post.count.must_equal 0
146
+ Like.count.must_equal 0
147
+
148
+ post.reload
149
+ post.restore
150
+ Post.count.must_equal 1
151
+ Like.count.must_equal 1
152
+ end
153
+
111
154
  it 'restores a dependent has_many relation with DestroyedAt' do
112
155
  Comment.create(:destroyed_at => timestamp, :post => post)
113
156
  Comment.count.must_equal 0
@@ -184,7 +227,7 @@ describe 'creating a destroyed record' do
184
227
  end
185
228
 
186
229
  describe 'non destroyed-at models' do
187
- it 'can destroy has_on dependants' do
230
+ it 'can destroy has_one dependants' do
188
231
  person = Person.create!
189
232
  person.create_pet!
190
233
 
@@ -38,6 +38,7 @@ ActiveRecord::Base.connection.execute(%{CREATE TABLE images (id INTEGER PRIMARY
38
38
  ActiveRecord::Base.connection.execute(%{CREATE TABLE posts (id INTEGER PRIMARY KEY, author_id INTEGER, destroyed_at DATETIME);})
39
39
  ActiveRecord::Base.connection.execute(%{CREATE TABLE people (id INTEGER PRIMARY KEY);})
40
40
  ActiveRecord::Base.connection.execute(%{CREATE TABLE pets (id INTEGER PRIMARY KEY, person_id INTEGER);})
41
+ ActiveRecord::Base.connection.execute(%{CREATE TABLE likes (id INTEGER PRIMARY KEY, likeable_id INTEGER, likeable_type TEXT, destroyed_at DATETIME);})
41
42
 
42
43
  class Author < ActiveRecord::Base
43
44
  has_many :posts
@@ -68,15 +69,22 @@ class Categorization < ActiveRecord::Base
68
69
  belongs_to :post
69
70
  end
70
71
 
72
+ class Like < ActiveRecord::Base
73
+ include DestroyedAt
74
+ belongs_to :likeable, polymorphic: true
75
+ end
76
+
71
77
  class Comment < ActiveRecord::Base
72
78
  include DestroyedAt
73
79
  belongs_to :post
74
80
  belongs_to :commenter
81
+
82
+ has_many :likes, as: :likeable, dependent: :destroy
75
83
  end
76
84
 
77
85
  class Commenter < ActiveRecord::Base
78
86
  include DestroyedAt
79
- has_many :comments, dependent: :destroy
87
+ has_many :comments, dependent: :destroy, autosave: true
80
88
  has_many :posts, through: :comments
81
89
  end
82
90
 
@@ -92,6 +100,7 @@ class Post < ActiveRecord::Base
92
100
  has_many :categorizations, dependent: :destroy
93
101
  has_many :comments, dependent: :destroy
94
102
  has_many :commenters, through: :comments
103
+ has_one :like, as: :likeable, dependent: :destroy
95
104
 
96
105
  before_destroy :increment_destroy_callback_counter
97
106
  before_restore :increment_restore_callback_counter
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: destroyed_at
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Dupuis Jr.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-13 00:00:00.000000000 Z
11
+ date: 2016-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord