permanent_records 3.1.6 → 3.2.0

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,4 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 78b0fb28d98b7b5a68bb591b23eaded91bba6f87
4
- data.tar.gz: 756981d277d14d61780c49250f6c9893e01f6a6b
5
2
  SHA512:
6
- metadata.gz: 915ccc96b2ad16475d21349ac09649227816947f66044eaf023a0dec6f561887cd5fb3295dcd73f979a61ae964239c3ff96336c3bfed58086e0deeb148233134
7
- data.tar.gz: 7e2723e413ceb80205bfa2256d50ff26383efa77db256f6013c3c7e40c34a1b2cda204ea0d0e363cbd9635513c4d84e474a5ac9622b8c3059f9ad81f79cd21a3
3
+ metadata.gz: be9cd9f51f282ac7959ef28b582ce21f6ea5fb324d08974bc290a6a3f826ee2fa8d753a31f8b66def452efaa612804e05f512ad966d431f44274af5f98291bbd
4
+ data.tar.gz: eaa35e4b1c6e95590e0ac67f4109464d8d5609cf7b8b024d74cfef90657fb34d57ed9aa26f37d93795a75c8faa458899cb348c8ee703915d29c4f19c86198b50
data/Gemfile CHANGED
@@ -8,9 +8,9 @@ gem 'activesupport', ver
8
8
 
9
9
  group :test do
10
10
  gem 'rake'
11
- gem 'pg'
11
+ gem 'sqlite3'
12
12
  gem 'pry'
13
13
  gem 'awesome_print'
14
14
  gem 'database_cleaner'
15
- gem 'rspec'
15
+ gem 'rspec', '~> 2.14.1'
16
16
  end
data/README.markdown CHANGED
@@ -1,83 +1,84 @@
1
- # PermanentRecords
1
+ # PermanentRecords (Rails 3+)
2
2
 
3
3
  [http://github.com/JackDanger/permanent_records/](http://github.com/JackDanger/permanent_records/)
4
4
 
5
5
  This gem prevents any of your ActiveRecord data from being destroyed.
6
6
  Any model that you've given a "deleted_at" datetime column will have that column set rather than let the record be deleted.
7
7
 
8
- ## Compatibility: This gem is for Rails 3+
9
-
10
- ## Does it make a lot of sense?
11
-
12
- Yes.
8
+ ## What methods does it give me?
13
9
 
14
10
  ```ruby
15
- User.find(3).destroy # sets the 'deleted_at' attribute to Time.now and returns a frozen record
16
- User.find(3).destroy(:force) # executes the real destroy method, the record will be removed from the database
17
- User.destroy_all # soft-deletes all User records
18
- User.delete_all # bye bye everything (no soft-deleting here)
11
+ User.find(3).destroy # Sets the 'deleted_at' attribute to Time.now
12
+ # and returns a frozen record.
13
+
14
+ User.find(3).destroy(:force) # Executes the real destroy method, the record
15
+ # will be removed from the database.
16
+
17
+ User.destroy_all # Soft-deletes all User records.
18
+
19
+ User.delete_all # bye bye everything (no soft-deleting here)
19
20
  ```
20
21
  There are also two scopes provided for easily searching deleted and not deleted records:
21
22
 
22
23
  ```ruby
23
- User.deleted.find(...) # only returns deleted records.
24
- User.not_deleted.find(...) # only returns non-deleted records.
25
- ```
24
+ User.deleted.find(...) # Only returns deleted records.
26
25
 
27
- Note: Your normal finds will, by default, _include_ deleted records. You'll have to manually use the 'not_deleted' scope to avoid this:
28
-
29
- ```ruby
30
- User.find(1) # will find record number 1, even if it's deleted
31
- User.not_deleted.find(1) # This is probably what you want, it doesn't find deleted records
26
+ User.not_deleted.find(...) # Only returns non-deleted records.
32
27
  ```
33
28
 
34
- ## Is Everything Automated?
35
-
29
+ Note: Your normal finds will, by default, _include_ deleted records. You'll have to manually use the ```not_deleted``` scope to avoid this:
36
30
 
37
- Yes. You don't have to change ANY of your code to get permanent archiving of all your data with this gem.
38
- When you call 'destroy' on any record (or 'destroy_all' on a class or association) your records will
39
- all have a deleted_at timestamp set on them.
31
+ ```ruby
32
+ User.find(1) # Will find record number 1, even if it's deleted.
40
33
 
34
+ User.not_deleted.find(1) # This is probably what you want, it doesn't find deleted records.
35
+ ```
41
36
 
42
37
  ## Can I easily undelete records?
43
38
 
44
39
  Yes. All you need to do is call the 'revive' method.
45
40
 
46
41
  ```ruby
47
- User.find(3).destroy
48
- # the user is now deleted
49
- User.find(3).revive
50
- # the user is back to it's original state
42
+ User.find(3).destroy # The user is now deleted.
43
+
44
+ User.find(3).revive # The user is back to it's original state.
51
45
  ```
52
46
 
53
47
  And if you had dependent records that were set to be destroyed along with the parent record:
54
48
 
55
49
  ```ruby
56
- class User < ActiveRecord::Base
57
- has_many :comments, :dependent => :destroy
58
- end
59
- User.find(3).destroy
60
- # all the comments are destroyed as well
61
- User.find(3).revive
62
- # all the comments that were just destroyed are now back in pristine condition
63
-
64
- # forcing deletion works the same way: fi you hard delete a record, its dependent records will also be hard deleted
50
+ class User < ActiveRecord::Base
51
+ has_many :comments, :dependent => :destroy
52
+ end
53
+
54
+ User.find(3).destroy # All the comments are destroyed as well.
55
+
56
+ User.find(3).revive # All the comments that were just destroyed
57
+ # are now back in pristine condition.
65
58
  ```
66
59
 
60
+ Forcing deletion works the same way: if you hard delete a record, its dependent records will also be hard deleted.
61
+
67
62
  ## Can I use default scopes?
68
63
 
69
64
  ```ruby
70
- default_scope where(:deleted_at => nil)
65
+ default_scope where(:deleted_at => nil)
71
66
  ```
72
67
 
73
68
  If you use such a default scope, you will need to simulate the `deleted` scope with a method
74
69
 
75
70
  ```ruby
76
- def self.deleted
77
- self.unscoped.where('deleted_at IS NOT NULL')
78
- end
71
+ def self.deleted
72
+ self.unscoped.where('deleted_at IS NOT NULL')
73
+ end
79
74
  ```
80
75
 
76
+ ## Is Everything Automated?
77
+
78
+ Yes. You don't have to change ANY of your code to get permanent archiving of all your data with this gem.
79
+ When you call `destroy` on any record (or `destroy_all` on a class or association) your records will
80
+ all have a deleted_at timestamp set on them.
81
+
81
82
  ## Productionizing
82
83
 
83
84
  If you operate a system where destroying or reviving a record takes more
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.1.6
1
+ 3.2.0
@@ -6,7 +6,7 @@ module PermanentRecords
6
6
  # * is_permanent? #=> true/false, depending if you have a deleted_at column
7
7
  # * deleted? #=> true/false, depending if you've called .destroy
8
8
  # * destroy #=> sets deleted_at, your record is now in the .destroyed scope
9
- # * revice #=> undo the destroy
9
+ # * revive #=> undo the destroy
10
10
  module ActiveRecord
11
11
  def self.included(base)
12
12
 
@@ -25,7 +25,11 @@ module PermanentRecords
25
25
  end
26
26
 
27
27
  def deleted?
28
- deleted_at if is_permanent?
28
+ if is_permanent?
29
+ !!deleted_at
30
+ else
31
+ destroyed?
32
+ end
29
33
  end
30
34
 
31
35
  def revive(validate = nil)
@@ -55,18 +59,22 @@ module PermanentRecords
55
59
  else
56
60
  record.save!
57
61
  end
58
- @attributes, @attributes_cache = record.attributes, record.attributes
59
- # workaround for active_record >= 3.2.0: re-wrap values of serialized attributes
60
- # (record.attributes returns the plain values but in the instance variables they are expected to be wrapped)
61
- if defined?(::ActiveRecord::AttributeMethods::Serialization::Attribute)
62
- serialized_attribute_class = ::ActiveRecord::AttributeMethods::Serialization::Attribute
63
- self.class.serialized_attributes.each do |key, coder|
64
- if @attributes.key?(key)
65
- attr = serialized_attribute_class.new(coder, @attributes[key], :unserialized)
66
- @attributes[key] = attr
67
- @attributes_cache[key] = attr
62
+ if ::Gem::Version.new(::ActiveRecord::VERSION::STRING) < ::Gem::Version.new('4.2.0')
63
+ @attributes, @attributes_cache = record.attributes, record.attributes
64
+ # workaround for active_record >= 3.2.0: re-wrap values of serialized attributes
65
+ # (record.attributes returns the plain values but in the instance variables they are expected to be wrapped)
66
+ if defined?(::ActiveRecord::AttributeMethods::Serialization::Attribute)
67
+ serialized_attribute_class = ::ActiveRecord::AttributeMethods::Serialization::Attribute
68
+ self.class.serialized_attributes.each do |key, coder|
69
+ if @attributes.key?(key)
70
+ attr = serialized_attribute_class.new(coder, @attributes[key], :unserialized)
71
+ @attributes[key] = attr
72
+ @attributes_cache[key] = attr
73
+ end
68
74
  end
69
75
  end
76
+ else # AR >= 4.2.0
77
+ @attributes = record.instance_variable_get('@attributes')
70
78
  end
71
79
  rescue Exception => e
72
80
  # trigger dependent record destruction (they were revived before this record,
@@ -89,9 +97,8 @@ module PermanentRecords
89
97
  end.each do |name, reflection|
90
98
  cardinality = reflection.macro.to_s.gsub('has_', '')
91
99
  if cardinality == 'many'
92
- records = send(name).unscoped.find(
93
- :all,
94
- :conditions => [
100
+ records = send(name).unscoped.where(
101
+ [
95
102
  "#{reflection.quoted_table_name}.deleted_at > ?" +
96
103
  " AND " +
97
104
  "#{reflection.quoted_table_name}.deleted_at < ?",
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.license = 'MIT'
12
12
 
13
13
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
14
- s.authors = ["Jack Danger Canty", "David Sulc", "Joe Nelson", "Trond Arve Nordheim"]
14
+ s.authors = ["Jack Danger Canty", "David Sulc", "Joe Nelson", "Trond Arve Nordheim", "Josh Teneycke"]
15
15
  s.date = Date.today.to_s
16
16
  s.description = "Never Lose Data. Rather than deleting rows this sets Record#deleted_at and gives you all the scopes you need to work with your data."
17
17
  s.email = "gems@6brand.com"
@@ -84,6 +84,10 @@ describe PermanentRecords do
84
84
  it 'really removes the record' do
85
85
  expect { subject }.to change { record.class.count }.by(-1)
86
86
  end
87
+
88
+ it 'makes deleted? return true' do
89
+ subject.should be_deleted
90
+ end
87
91
  end
88
92
 
89
93
  context 'with dependent records' do
@@ -120,7 +124,7 @@ describe PermanentRecords do
120
124
  end
121
125
  end
122
126
  context 'as default scope' do
123
- let(:load_comments) { Comment.unscoped.find_all_by_hole_id(subject.id) }
127
+ let(:load_comments) { Comment.unscoped.where(:hole_id => subject.id) }
124
128
  context 'with :has_many cardinality' do
125
129
  before {
126
130
  load_comments.size.should == 2
data/spec/spec_helper.rb CHANGED
@@ -17,6 +17,10 @@ module Rails
17
17
  def self.env; 'test'end
18
18
  end
19
19
 
20
+ if I18n.config.respond_to?(:enforce_available_locales)
21
+ I18n.config.enforce_available_locales = true
22
+ end
23
+
20
24
  require 'logger'
21
25
  ActiveRecord::Base.logger = Logger.new support.join("debug.log")
22
26
  ActiveRecord::Base.configurations = YAML::load_file support.join('database.yml')
@@ -1,5 +1,5 @@
1
1
  class Comment < ActiveRecord::Base
2
2
  belongs_to :hole
3
3
 
4
- default_scope where(:deleted_at => nil)
4
+ default_scope { where(:deleted_at => nil) }
5
5
  end
@@ -1,6 +1,6 @@
1
1
  test:
2
- :adapter: postgresql
3
- :database: permanent_records
2
+ :adapter: sqlite3
3
+ :database: ":memory:"
4
4
  :min_messages: ERROR
5
5
  # sqlite:
6
6
  # :adapter: sqlite
@@ -1,5 +1,9 @@
1
1
  class Difficulty < ActiveRecord::Base
2
2
  belongs_to :hole
3
3
 
4
- default_scope where(:deleted_at => nil)
5
- end
4
+ if ActiveRecord::VERSION::STRING == '3.0.0'
5
+ default_scope where(:deleted_at => nil)
6
+ else
7
+ default_scope { where(:deleted_at => nil) }
8
+ end
9
+ end
metadata CHANGED
@@ -1,44 +1,45 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: permanent_records
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.6
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Danger Canty
8
8
  - David Sulc
9
9
  - Joe Nelson
10
10
  - Trond Arve Nordheim
11
+ - Josh Teneycke
11
12
  autorequire:
12
13
  bindir: bin
13
14
  cert_chain: []
14
- date: 2013-08-19 00:00:00.000000000 Z
15
+ date: 2015-01-17 00:00:00.000000000 Z
15
16
  dependencies:
16
17
  - !ruby/object:Gem::Dependency
17
18
  name: activerecord
18
19
  requirement: !ruby/object:Gem::Requirement
19
20
  requirements:
20
- - - '>='
21
+ - - ">="
21
22
  - !ruby/object:Gem::Version
22
23
  version: 3.0.0
23
24
  type: :runtime
24
25
  prerelease: false
25
26
  version_requirements: !ruby/object:Gem::Requirement
26
27
  requirements:
27
- - - '>='
28
+ - - ">="
28
29
  - !ruby/object:Gem::Version
29
30
  version: 3.0.0
30
31
  - !ruby/object:Gem::Dependency
31
32
  name: rake
32
33
  requirement: !ruby/object:Gem::Requirement
33
34
  requirements:
34
- - - '>='
35
+ - - ">="
35
36
  - !ruby/object:Gem::Version
36
37
  version: '0'
37
38
  type: :development
38
39
  prerelease: false
39
40
  version_requirements: !ruby/object:Gem::Requirement
40
41
  requirements:
41
- - - '>='
42
+ - - ">="
42
43
  - !ruby/object:Gem::Version
43
44
  version: '0'
44
45
  description: Never Lose Data. Rather than deleting rows this sets Record#deleted_at
@@ -50,7 +51,7 @@ extra_rdoc_files:
50
51
  - LICENSE
51
52
  - README.markdown
52
53
  files:
53
- - .document
54
+ - ".document"
54
55
  - Gemfile
55
56
  - LICENSE
56
57
  - README.markdown
@@ -58,8 +59,8 @@ files:
58
59
  - VERSION
59
60
  - lib/permanent_records.rb
60
61
  - permanent_records.gemspec
61
- - spec/spec_helper.rb
62
62
  - spec/permanent_records_spec.rb
63
+ - spec/spec_helper.rb
63
64
  - spec/support/comment.rb
64
65
  - spec/support/database.yml
65
66
  - spec/support/difficulty.rb
@@ -82,18 +83,19 @@ require_paths:
82
83
  - lib
83
84
  required_ruby_version: !ruby/object:Gem::Requirement
84
85
  requirements:
85
- - - '>='
86
+ - - ">="
86
87
  - !ruby/object:Gem::Version
87
88
  version: '0'
88
89
  required_rubygems_version: !ruby/object:Gem::Requirement
89
90
  requirements:
90
- - - '>='
91
+ - - ">="
91
92
  - !ruby/object:Gem::Version
92
93
  version: '0'
93
94
  requirements: []
94
95
  rubyforge_project:
95
- rubygems_version: 2.0.3
96
+ rubygems_version: 2.4.3
96
97
  signing_key:
97
98
  specification_version: 3
98
99
  summary: Soft-delete your ActiveRecord records
99
100
  test_files: []
101
+ has_rdoc: