destroyed_at 0.1.0 → 0.1.1

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: 27eab1991579adb70e518846349f055c1b3987eb
4
- data.tar.gz: 0dfd9c729a38a9b6e8a39111cfb1e3ea25e0051c
3
+ metadata.gz: d0cbd15c416464ff4ef00e2af5b0a037bf72691e
4
+ data.tar.gz: 7f0cdd43d097e1f28b8c557ba444fa1f0befc6f6
5
5
  SHA512:
6
- metadata.gz: bca0bb1c670c15093a97181d81fc4437382abebb78bcfb6fb46aeafe8605df76dc1c52c875e8b6fa091a9db20d16945e3e759b19e40a0f9db929be7955ecb6d8
7
- data.tar.gz: f691ed1739ba388af682df654ddfd9261c8efaddfcda9e805628f14e32619eaaab7f665efc630bda8814d62650164305ed2e77b1f2fb106e3f89d695bb60163e
6
+ metadata.gz: bb3d75a9b8ab6488912479cc5623556f4f23e84d2235ecd6b668e1a2417037abf43ee377aa27b26504fb32a8878e0cb8c92540f3a6188d231dc917c2e5b9f2aa
7
+ data.tar.gz: ca2b3311fcec370efa0d5ab14d0fa6c5b11b7a36e8282f931f54501f399c173c2c0d0d18af81f124e49afb66351510ac268631eb89fbf239f990712133c32079
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  *.rbc
3
+ *.swp
3
4
  .bundle
4
5
  .config
5
6
  .yardoc
@@ -15,3 +16,4 @@ spec/reports
15
16
  test/tmp
16
17
  test/version_tmp
17
18
  tmp
19
+ bin/
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,24 @@
1
+ # Contribution Guidelines #
2
+
3
+ ## Submitting a new issue ##
4
+
5
+ If you want to ensure that your issue gets fixed *fast* you should
6
+ attempt to reproduce the issue in an isolated example application that
7
+ you can share.
8
+
9
+ ## Making a pull request ##
10
+
11
+ If you'd like to submit a pull request please adhere to the following:
12
+
13
+ 1. Your code *must* be tested. Please TDD your code!
14
+ 2. No single-character variables
15
+ 3. Two-spaces instead of tabs
16
+ 4. Single-quotes instead of double-quotes unless you are using string
17
+ interpolation or escapes.
18
+ 5. General Rails/Ruby naming conventions for files and classes
19
+
20
+ Please note that you must adhere to each of the aforementioned rules.
21
+ Failure to do so will result in an immediate closing of the pull
22
+ request. If you update and rebase the pull request to follow the
23
+ guidelines your pull request will be re-opened and considered for
24
+ inclusion.
data/README.md CHANGED
@@ -32,6 +32,8 @@ class ActiveRecord::Base
32
32
  end
33
33
  ```
34
34
 
35
+ **Please note you will need to make a migration**
36
+
35
37
  Each model's table that is expected to have this behavior **must** have
36
38
  a `destroyed_at` column of type `DateTime`.
37
39
 
@@ -40,50 +42,80 @@ Allows you to "destroy" an object without deleting the record or
40
42
  associated records.
41
43
 
42
44
  ### Destroying ###
43
- Overides the `destroy` method to set `destroyed_at` on an object. The
45
+ Overides `#destroy` to set `#destroyed_at` to the current time on an object. The
44
46
  default scope of the class is then set to return objects that have not
45
47
  been destroyed (i.e., have `nil` for their destroyed_at value).
46
48
 
47
49
  `#destroyed?` will be `true` when your model is destroyed; it will be
48
- `false` when your model has been undestroyed.
50
+ `false` when your model has been restored.
51
+
52
+ ```ruby
53
+ class User < ActiveRecord::Base
54
+ include DestroyedAt
55
+ end
49
56
 
50
- ## Undestroying ###
51
- When you'd like to "undestroy" a record, call the `undestroy` method on
52
- the instance. This will set its `destroyed_at` value to `nil`, thereby
57
+ user = User.create
58
+ user.destroy
59
+ # => true
60
+ user.destroyed_at
61
+ # => <DateTime>
62
+ ```
63
+
64
+ ### Restoring ####
65
+ When you'd like to "restore" a record, call the `#restore` method on
66
+ the instance. This will set its `#destroyed_at` value to `nil`, thereby
53
67
  including it in the default scope of the class again.
54
68
 
55
69
  To include this functionality on `has_many through` relationships,
56
70
  be sure to `include DestroyedAt` on the through model, as well as the
57
71
  parent model.
58
72
 
59
- #### Callbacks ####
60
- `before_undestroy` and `after_undestroy` callbacks are added to your
61
- model. They work similarly to the `before_destroy` and `after_destroy`
62
- callbacks.
63
-
64
- ### Destroying ###
65
73
  ```ruby
66
74
  class User < ActiveRecord::Base
67
75
  include DestroyedAt
68
76
  end
69
77
 
70
78
  user = User.create
71
- user.destroy
79
+ user.destroy
80
+ user.restore
72
81
  # => true
82
+ user.destroyed_at
83
+ # => nil
73
84
  ```
74
85
 
75
- ### Undestroying ###
86
+ ### Callbacks ###
87
+ `before_restore` and `after_restore` callbacks are added to your
88
+ model. They work similarly to the `before_destroy` and `after_destroy`
89
+ callbacks.
90
+
76
91
  ```ruby
77
92
  class User < ActiveRecord::Base
78
- include DestroyedAt
93
+ before_restore :before_restore_action
94
+ after_restore :after_restore_action
95
+
96
+ private
97
+
98
+ def before_restore_action
99
+ ...
100
+ end
101
+
102
+ def after_restore_action
103
+ ...
104
+ end
79
105
  end
106
+ ```
80
107
 
81
- user = User.create
82
- user.destroy
83
- user.undestroy
84
- # => true
108
+ ### Validations ###
109
+
110
+ If you are using the `uniqueness` validator you will need to run it as:
111
+
112
+ ```ruby
113
+ validates :email, uniqueness: { conditions: -> { where(destroyed_at: nil) } }
85
114
  ```
86
115
 
116
+ Rails will by default not include default_scopes when querying for uniqueness. Rather than monkey
117
+ patching the validator we believe this is the best solution.
118
+
87
119
  ## Authors ##
88
120
 
89
121
  * [Michael Dupuis](http://twitter.com/michaeldupuisjr)
data/lib/destroyed_at.rb CHANGED
@@ -5,7 +5,7 @@ module DestroyedAt
5
5
  klass.instance_eval do
6
6
  default_scope { where(destroyed_at: nil) }
7
7
  after_initialize :_set_destruction_state
8
- define_model_callbacks :undestroy
8
+ define_model_callbacks :restore
9
9
  end
10
10
  end
11
11
 
@@ -18,13 +18,13 @@ module DestroyedAt
18
18
  end
19
19
  end
20
20
 
21
- # Set an object's destroyed at time to nil.
22
- def undestroy
21
+ # Set an object's destroyed_at time to nil.
22
+ def restore
23
23
  state = nil
24
- run_callbacks(:undestroy) do
24
+ run_callbacks(:restore) do
25
25
  if state = self.update_attribute(:destroyed_at, nil)
26
26
  @destroyed = false
27
- _undestroy_associations
27
+ _restore_associations
28
28
  end
29
29
  end
30
30
  state
@@ -33,16 +33,18 @@ module DestroyedAt
33
33
  private
34
34
 
35
35
  def _set_destruction_state
36
- @destroyed = destroyed_at.present?
36
+ @destroyed = destroyed_at.present? if respond_to?(:destroyed_at)
37
+ # Don't stop the other callbacks from running
38
+ true
37
39
  end
38
40
 
39
- def _undestroy_associations
41
+ def _restore_associations
40
42
  reflections.select { |key, value| value.options[:dependent] == :destroy }.keys.each do |key|
41
43
  assoc = association(key)
42
44
  if assoc.options[:through] && assoc.options[:dependent] == :destroy
43
45
  assoc = association(assoc.options[:through])
44
46
  end
45
- assoc.scoped.unscoped.each { |r| r.undestroy if r.respond_to? :undestroy }
47
+ assoc.scoped.unscoped.each { |r| r.restore if r.respond_to? :restore }
46
48
  end
47
49
  end
48
50
  end
@@ -1,3 +1,3 @@
1
1
  module DestroyedAt
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -17,11 +17,11 @@ describe 'Destroying AR models' do
17
17
  end
18
18
  end
19
19
 
20
- it 'can undestroy records' do
20
+ it 'can restore records' do
21
21
  user = User.create(:destroyed_at => DateTime.current)
22
22
  User.all.must_be_empty
23
23
  user.reload
24
- user.undestroy
24
+ user.restore
25
25
  User.all.wont_be_empty
26
26
  end
27
27
 
@@ -34,11 +34,11 @@ describe 'Destroying AR models' do
34
34
  person.after_flag.must_equal true
35
35
  end
36
36
 
37
- it 'will run undestroy callbacks' do
37
+ it 'will run restore callbacks' do
38
38
  person = Person.create(:destroyed_at => DateTime.current)
39
39
  person.before_flag.wont_equal true
40
40
  person.after_flag.wont_equal true
41
- person.undestroy
41
+ person.restore
42
42
  person.before_flag.must_equal true
43
43
  person.after_flag.must_equal true
44
44
  end
@@ -55,18 +55,18 @@ describe 'Destroying AR models' do
55
55
  Car.unscoped.count.must_equal 0
56
56
  end
57
57
 
58
- it 'can undestroy relationships' do
58
+ it 'can restore relationships' do
59
59
  user = User.create(:destroyed_at => DateTime.current)
60
60
  Profile.create(:destroyed_at => DateTime.current, :user => user)
61
61
  Profile.count.must_equal 0
62
- user.undestroy
62
+ user.restore
63
63
  Profile.count.must_equal 1
64
64
  end
65
65
 
66
- it 'will not undestroy relationships that have no destroy dependency' do
66
+ it 'will not restore relationships that have no destroy dependency' do
67
67
  user = User.create(:destroyed_at => DateTime.current, :show => Show.new(:destroyed_at => DateTime.current))
68
68
  Show.count.must_equal 0
69
- user.undestroy
69
+ user.restore
70
70
  Show.count.must_equal 0
71
71
  end
72
72
 
@@ -74,7 +74,7 @@ describe 'Destroying AR models' do
74
74
  user = User.create(:destroyed_at => DateTime.current)
75
75
  Dinner.create(:destroyed_at => DateTime.current, :user => user)
76
76
  Dinner.count.must_equal 0
77
- user.undestroy
77
+ user.restore
78
78
  Dinner.count.must_equal 1
79
79
  end
80
80
 
@@ -85,7 +85,7 @@ describe 'Destroying AR models' do
85
85
  fleet = Fleet.create(:destroyed_at => DateTime.current, :car => car)
86
86
  user.fleets = [fleet]
87
87
  user.cars.count.must_equal 0
88
- user.undestroy
88
+ user.restore
89
89
  user.cars.count.must_equal 1
90
90
  end
91
91
 
@@ -95,7 +95,12 @@ describe 'Destroying AR models' do
95
95
  user.destroyed?.must_equal true
96
96
  user = User.unscoped.last
97
97
  user.destroyed?.must_equal true
98
- user.undestroy
98
+ user.restore
99
99
  user.destroyed?.must_equal false
100
100
  end
101
+
102
+ it 'properly selects columns' do
103
+ User.create
104
+ User.select(:id).must_be_kind_of ActiveRecord::Relation
105
+ end
101
106
  end
data/test/test_helper.rb CHANGED
@@ -48,8 +48,8 @@ class Person < User
48
48
  before_destroy :set_before_flag
49
49
  after_destroy :set_after_flag
50
50
 
51
- before_undestroy :set_before_flag
52
- after_undestroy :set_after_flag
51
+ before_restore :set_before_flag
52
+ after_restore :set_after_flag
53
53
 
54
54
  attr_accessor :before_flag, :after_flag
55
55
 
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: 0.1.0
4
+ version: 0.1.1
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: 2013-06-28 00:00:00.000000000 Z
11
+ date: 2013-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -145,6 +145,7 @@ extra_rdoc_files: []
145
145
  files:
146
146
  - .gitignore
147
147
  - .travis.yml
148
+ - CONTRIBUTING.md
148
149
  - Gemfile
149
150
  - README.md
150
151
  - Rakefile
@@ -173,7 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
174
  version: '0'
174
175
  requirements: []
175
176
  rubyforge_project:
176
- rubygems_version: 2.0.0
177
+ rubygems_version: 2.0.3
177
178
  signing_key:
178
179
  specification_version: 4
179
180
  summary: Safe destroy for ActiveRecord.