is_paranoid_ext 0.0.1 → 0.0.2

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.
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{is_paranoid_ext}
3
- s.version = "0.0.1"
3
+ s.version = "0.0.2"
4
4
 
5
5
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
6
  s.authors = ["Alex Neill"]
@@ -15,10 +15,29 @@ module IsParanoidExt
15
15
  private
16
16
 
17
17
  def merge_paranoid_options!(association_id, options)
18
- if (klass = association_id.to_s.classify.constantize) && klass.respond_to?(:restore)
19
- options.merge!(:conditions => ["#{klass.quoted_table_name}.#{klass.destroyed_field} IS ?", klass.field_not_destroyed])
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)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Alex Neill