rails3_acts_as_paranoid 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -33,7 +33,7 @@ The values shown are the defaults. While *column* can be anything (as long as it
33
33
  - `time` or
34
34
  - `string`
35
35
 
36
- If your column type is a `string`, you can also specify which value to use when marking an object as deleted by passing `:deleted_value` (default is "deleted").
36
+ If your column type is a `string`, you can also specify which value to use when marking an object as deleted by passing `:deleted_value` (default is "deleted"). Any records with a non-matching value in this column will be treated normally (ie: not deleted).
37
37
 
38
38
  ### Filtering
39
39
 
@@ -14,15 +14,17 @@ module ActsAsParanoid
14
14
 
15
15
  if with_deleted
16
16
  result.options[:with_deleted] = with_deleted
17
- class_eval <<-RUBY, __FILE__, __LINE__
18
- def #{target}_with_unscoped(*args)
19
- association = association(:#{target})
20
- return nil if association.options[:polymorphic] && association.klass.nil?
21
- return #{target}_without_unscoped(*args) unless association.klass.paranoid?
22
- association.klass.with_deleted.scoping { #{target}_without_unscoped(*args) }
23
- end
24
- alias_method_chain :#{target}, :unscoped
25
- RUBY
17
+ unless method_defined? "#{target}_with_unscoped"
18
+ class_eval <<-RUBY, __FILE__, __LINE__
19
+ def #{target}_with_unscoped(*args)
20
+ association = association(:#{target})
21
+ return nil if association.options[:polymorphic] && association.klass.nil?
22
+ return #{target}_without_unscoped(*args) unless association.klass.paranoid?
23
+ association.klass.with_deleted.scoping { #{target}_without_unscoped(*args) }
24
+ end
25
+ alias_method_chain :#{target}, :unscoped
26
+ RUBY
27
+ end
26
28
  end
27
29
 
28
30
  result
@@ -22,7 +22,11 @@ module ActsAsParanoid
22
22
  end
23
23
 
24
24
  def only_deleted
25
- without_paranoid_default_scope.where("#{paranoid_column_reference} IS NOT ?", nil)
25
+ if string_type_with_deleted_value?
26
+ without_paranoid_default_scope.where("#{paranoid_column_reference} IS ?", paranoid_configuration[:deleted_value])
27
+ else
28
+ without_paranoid_default_scope.where("#{paranoid_column_reference} IS NOT ?", nil)
29
+ end
26
30
  end
27
31
 
28
32
  def delete_all!(conditions = nil)
@@ -34,7 +38,17 @@ module ActsAsParanoid
34
38
  end
35
39
 
36
40
  def paranoid_default_scope_sql
37
- self.scoped.table[paranoid_column].eq(nil).to_sql
41
+ if string_type_with_deleted_value?
42
+ self.scoped.table[paranoid_column].eq(nil).
43
+ or(self.scoped.table[paranoid_column].not_eq(paranoid_configuration[:deleted_value])).
44
+ to_sql
45
+ else
46
+ self.scoped.table[paranoid_column].eq(nil).to_sql
47
+ end
48
+ end
49
+
50
+ def string_type_with_deleted_value?
51
+ paranoid_column_type == :string && !paranoid_configuration[:deleted_value].nil?
38
52
  end
39
53
 
40
54
  def paranoid_column
@@ -67,6 +81,10 @@ module ActsAsParanoid
67
81
  end
68
82
  end
69
83
 
84
+ def persisted?
85
+ !(new_record? || @destroyed)
86
+ end
87
+
70
88
  def paranoid_value
71
89
  self.send(self.class.paranoid_column)
72
90
  end
@@ -75,7 +93,8 @@ module ActsAsParanoid
75
93
  with_transaction_returning_status do
76
94
  run_callbacks :destroy do
77
95
  destroy_dependent_associations!
78
- self.class.delete_all!(self.class.primary_key.to_sym => self.id)
96
+ # Handle composite keys, otherwise we would just use `self.class.primary_key.to_sym => self.id`.
97
+ self.class.delete_all!(Hash[[Array(self.class.primary_key), Array(self.id)].transpose])
79
98
  self.paranoid_value = self.class.delete_now_value
80
99
  freeze
81
100
  end
@@ -83,10 +102,11 @@ module ActsAsParanoid
83
102
  end
84
103
 
85
104
  def destroy
86
- if paranoid_value.nil?
105
+ if !deleted?
87
106
  with_transaction_returning_status do
88
107
  run_callbacks :destroy do
89
- self.class.delete_all(self.class.primary_key.to_sym => self.id)
108
+ # Handle composite keys, otherwise we would just use `self.class.primary_key.to_sym => self.id`.
109
+ self.class.delete_all(Hash[[Array(self.class.primary_key), Array(self.id)].transpose])
90
110
  self.paranoid_value = self.class.delete_now_value
91
111
  self
92
112
  end
@@ -117,7 +137,7 @@ module ActsAsParanoid
117
137
  next unless reflection.klass.paranoid?
118
138
 
119
139
  scope = reflection.klass.only_deleted
120
-
140
+
121
141
  # Merge in the association's scope
122
142
  scope = scope.merge(association(reflection.name).association_scope)
123
143
 
@@ -132,13 +152,13 @@ module ActsAsParanoid
132
152
  end
133
153
  end
134
154
  end
135
-
155
+
136
156
  def destroy_dependent_associations!
137
157
  self.class.dependent_associations.each do |reflection|
138
158
  next unless reflection.klass.paranoid?
139
159
 
140
160
  scope = reflection.klass.only_deleted
141
-
161
+
142
162
  # Merge in the association's scope
143
163
  scope = scope.merge(association(reflection.name).association_scope)
144
164
 
@@ -149,8 +169,10 @@ module ActsAsParanoid
149
169
  end
150
170
 
151
171
  def deleted?
152
- !paranoid_value.nil?
172
+ !(paranoid_value.nil? ||
173
+ (self.class.string_type_with_deleted_value? && paranoid_value != self.class.delete_now_value))
153
174
  end
175
+
154
176
  alias_method :destroyed?, :deleted?
155
177
 
156
178
  private
@@ -18,7 +18,9 @@ module ActsAsParanoid
18
18
  end
19
19
 
20
20
  relation = build_relation(finder_class, table, attribute, value)
21
- relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted?
21
+ [Array(finder_class.primary_key), Array(record.send(:id))].transpose.each do |pk_key, pk_value|
22
+ relation = relation.and(table[pk_key.to_sym].not_eq(pk_value))
23
+ end if record.persisted?
22
24
 
23
25
  Array.wrap(options[:scope]).each do |scope_item|
24
26
  scope_value = record.send(scope_item)
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.2.4
4
+ version: 0.2.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-16 00:00:00.000000000Z
12
+ date: 2012-12-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70118109527820 !ruby/object:Gem::Requirement
16
+ requirement: &70338444648540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '3.2'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70118109527820
24
+ version_requirements: *70338444648540
25
25
  description: Active Record (~>3.2) 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:
@@ -39,7 +39,8 @@ files:
39
39
  - README.markdown
40
40
  homepage: https://github.com/goncalossilva/rails3_acts_as_paranoid
41
41
  licenses: []
42
- post_install_message:
42
+ post_install_message: ! "\n=======================================\n\n This gem has
43
+ moved!\n\n To stay up to date, run:\n\n $ gem install 'acts_as_paranoid'\"\n\n=======================================\n\n"
43
44
  rdoc_options: []
44
45
  require_paths:
45
46
  - lib
@@ -51,7 +52,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
51
52
  version: '0'
52
53
  segments:
53
54
  - 0
54
- hash: -893378719868361513
55
+ hash: 375772570073154179
55
56
  required_rubygems_version: !ruby/object:Gem::Requirement
56
57
  none: false
57
58
  requirements: