is_paranoid_ext 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/is_paranoid_ext.gemspec +1 -1
- data/lib/is_paranoid_ext.rb +21 -2
- metadata +2 -2
data/is_paranoid_ext.gemspec
CHANGED
data/lib/is_paranoid_ext.rb
CHANGED
@@ -15,10 +15,29 @@ module IsParanoidExt
|
|
15
15
|
private
|
16
16
|
|
17
17
|
def merge_paranoid_options!(association_id, options)
|
18
|
-
|
19
|
-
|
18
|
+
through_id = options[:through]
|
19
|
+
@paranoid_conditions, @paranoid_values = nil, nil
|
20
|
+
apply_paranoid_conditions_for(options[:class_name] || association_id)
|
21
|
+
apply_paranoid_conditions_for through_id if through_id
|
22
|
+
options.merge!(:conditions => merged_paranoid_conditions) unless @paranoid_conditions.nil?
|
23
|
+
end
|
24
|
+
|
25
|
+
def apply_paranoid_conditions_for(association_id)
|
26
|
+
reflection = reflections[association_id.to_sym]
|
27
|
+
association_klass = ( reflection.try(:klass) || association_id.to_s.classify.constantize )
|
28
|
+
if association_klass && association_klass.respond_to?(:restore)
|
29
|
+
@paranoid_conditions ||= []
|
30
|
+
@paranoid_conditions << "#{association_klass.quoted_table_name}.#{association_klass.destroyed_field} IS ?"
|
31
|
+
@paranoid_values ||= []
|
32
|
+
@paranoid_values << association_klass.field_not_destroyed
|
20
33
|
end
|
21
34
|
end
|
35
|
+
|
36
|
+
def merged_paranoid_conditions
|
37
|
+
@merged_paranoid_conditions ||= if @paranoid_conditions.any? && @paranoid_values.any?
|
38
|
+
[@paranoid_conditions.map{|c|"(#{c})"}.join(' AND ')] + @paranoid_values
|
39
|
+
else nil end
|
40
|
+
end
|
22
41
|
end
|
23
42
|
|
24
43
|
ActiveRecord::Base.send(:extend, IsParanoidExt)
|