rails3_acts_as_paranoid 0.0.9 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  A simple plugin which hides records instead of deleting them, being able to recover them.
4
4
 
5
- This branch targets Rails 3.0.X. If you're working with another version, switch to the corresponding branch.
5
+ This branch targets Rails 3.1.X. If you're working with another version, switch to the corresponding branch.
6
6
 
7
7
  ## Credits
8
8
 
@@ -1,6 +1,58 @@
1
1
  require 'active_record'
2
2
  require 'validations/uniqueness_without_deleted'
3
3
 
4
+
5
+ module ActiveRecord
6
+ class Relation
7
+ def paranoid?
8
+ klass.try(:paranoid?) ? true : false
9
+ end
10
+
11
+ def paranoid_deletion_attributes
12
+ { klass.paranoid_column => klass.delete_now_value }
13
+ end
14
+
15
+ alias_method :destroy!, :destroy
16
+ def destroy(id)
17
+ if paranoid?
18
+ update_all(paranoid_deletion_attributes, {:id => id})
19
+ else
20
+ destroy!(id)
21
+ end
22
+ end
23
+
24
+ alias_method :really_delete_all!, :delete_all
25
+
26
+ def delete_all!(conditions = nil)
27
+ if conditions
28
+ # This idea comes out of Rails 3.1 ActiveRecord::Record.delete_all
29
+ where(conditions).delete_all!
30
+ else
31
+ really_delete_all!
32
+ end
33
+ end
34
+
35
+ def delete_all(conditions = nil)
36
+ if paranoid?
37
+ update_all(paranoid_deletion_attributes, conditions)
38
+ else
39
+ delete_all!(conditions)
40
+ end
41
+ end
42
+
43
+ def arel=(a)
44
+ @arel = a
45
+ end
46
+
47
+ def with_deleted
48
+ wd = self.clone
49
+ wd.default_scoped = false
50
+ wd.arel = self.build_arel
51
+ wd
52
+ end
53
+ end
54
+ end
55
+
4
56
  module ActsAsParanoid
5
57
 
6
58
  def paranoid?
@@ -25,16 +77,7 @@ module ActsAsParanoid
25
77
  self.paranoid_column_reference = "#{self.table_name}.#{paranoid_configuration[:column]}"
26
78
 
27
79
  return if paranoid?
28
-
29
- ActiveRecord::Relation.class_eval do
30
- alias_method :delete_all!, :delete_all
31
- alias_method :destroy!, :destroy
32
- end
33
80
 
34
- ActiveRecord::Reflection::AssociationReflection.class_eval do
35
- alias_method :foreign_key, :primary_key_name unless respond_to?(:foreign_key)
36
- end
37
-
38
81
  # Magic!
39
82
  default_scope where("#{paranoid_column_reference} IS ?", nil)
40
83
 
@@ -72,6 +115,18 @@ module ActsAsParanoid
72
115
  def only_deleted
73
116
  self.unscoped.where("#{paranoid_column_reference} IS NOT ?", nil)
74
117
  end
118
+
119
+ def deletion_conditions(id_or_array)
120
+ ["id in (?)", [id_or_array].flatten]
121
+ end
122
+
123
+ def delete!(id_or_array)
124
+ delete_all!(deletion_conditions(id_or_array))
125
+ end
126
+
127
+ def delete(id_or_array)
128
+ delete_all(deletion_conditions(id_or_array))
129
+ end
75
130
 
76
131
  def delete_all!(conditions = nil)
77
132
  self.unscoped.delete_all!(conditions)
@@ -133,6 +188,27 @@ module ActsAsParanoid
133
188
  end
134
189
  end
135
190
 
191
+ def delete!
192
+ with_transaction_returning_status do
193
+ act_on_dependent_destroy_associations
194
+ self.class.delete_all!(self.class.primary_key.to_sym => self.id)
195
+ self.paranoid_value = self.class.delete_now_value
196
+ freeze
197
+ end
198
+ end
199
+
200
+ def delete
201
+ if paranoid_value.nil?
202
+ with_transaction_returning_status do
203
+ self.class.delete_all(self.class.primary_key.to_sym => self.id)
204
+ self.paranoid_value = self.class.delete_now_value
205
+ self
206
+ end
207
+ else
208
+ delete!
209
+ end
210
+ end
211
+
136
212
  def recover(options={})
137
213
  options = {
138
214
  :recursive => self.class.paranoid_configuration[:recover_dependent_associations],
@@ -196,6 +272,7 @@ module ActsAsParanoid
196
272
 
197
273
  end
198
274
 
275
+
199
276
  # Extend ActiveRecord's functionality
200
277
  ActiveRecord::Base.send :extend, ActsAsParanoid
201
278
 
@@ -9,7 +9,8 @@ module ParanoidValidations
9
9
  value = YAML.dump value
10
10
  end
11
11
 
12
- sql, params = mount_sql_and_params(finder_class, record.class.quoted_table_name, attribute, value)
12
+ table = Arel::Table.new(record.class.table_name)
13
+ sql, params = build_relation(finder_class, table, attribute, value)
13
14
 
14
15
  # This is the only changed line from the base class version - it does finder_class.unscoped
15
16
  relation = finder_class.where(sql, *params)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails3_acts_as_paranoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,16 +13,16 @@ date: 2012-03-06 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70156244254600 !ruby/object:Gem::Requirement
16
+ requirement: &70234162146040 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '3.0'
21
+ version: '3.1'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70156244254600
25
- description: Active Record (~>3.0) plugin which allows you to hide and restore records
24
+ version_requirements: *70234162146040
25
+ description: Active Record (~>3.1) plugin which allows you to hide and restore records
26
26
  without actually deleting them. Check its GitHub page for more in-depth information.
27
27
  email:
28
28
  - goncalossilva@gmail.com
@@ -34,7 +34,7 @@ files:
34
34
  - lib/validations/uniqueness_without_deleted.rb
35
35
  - LICENSE
36
36
  - README.markdown
37
- homepage: http://github.com/goncalossilva/rails3_acts_as_paranoid
37
+ homepage: https://github.com/softcraft-development/rails3_acts_as_paranoid
38
38
  licenses: []
39
39
  post_install_message:
40
40
  rdoc_options: []
@@ -48,7 +48,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
48
48
  version: '0'
49
49
  segments:
50
50
  - 0
51
- hash: -960527913778585172
51
+ hash: -2074595129237887781
52
52
  required_rubygems_version: !ruby/object:Gem::Requirement
53
53
  none: false
54
54
  requirements:
@@ -60,6 +60,6 @@ rubyforge_project: rails3_acts_as_paranoid
60
60
  rubygems_version: 1.8.10
61
61
  signing_key:
62
62
  specification_version: 3
63
- summary: Active Record (~>3.0) plugin which allows you to hide and restore records
63
+ summary: Active Record (~>3.1) plugin which allows you to hide and restore records
64
64
  without actually deleting them.
65
65
  test_files: []