File: active_record_5_2.rb

package info (click to toggle)
ruby-paranoia 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 164 kB
  • sloc: ruby: 399; makefile: 6
file content (41 lines) | stat: -rw-r--r-- 1,229 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module HandleParanoiaDestroyedInBelongsToAssociation
  def handle_dependency
    return unless load_target

    case options[:dependent]
    when :destroy
      target.destroy
      if target.respond_to?(:paranoia_destroyed?)
        raise ActiveRecord::Rollback unless target.paranoia_destroyed?
      else
        raise ActiveRecord::Rollback unless target.destroyed?
      end
    else
      target.send(options[:dependent])
    end
  end
end

module HandleParanoiaDestroyedInHasOneAssociation
  def delete(method = options[:dependent])
    if load_target
      case method
      when :delete
        target.delete
      when :destroy
        target.destroyed_by_association = reflection
        target.destroy
        if target.respond_to?(:paranoia_destroyed?)
          throw(:abort) unless target.paranoia_destroyed?
        else
          throw(:abort) unless target.destroyed?
        end
      when :nullify
        target.update_columns(reflection.foreign_key => nil) if target.persisted?
      end
    end
  end
end

ActiveRecord::Associations::BelongsToAssociation.prepend HandleParanoiaDestroyedInBelongsToAssociation
ActiveRecord::Associations::HasOneAssociation.prepend HandleParanoiaDestroyedInHasOneAssociation