Module: ActsAsParanoidDag::ModelAdditions

Defined in:
lib/acts_as_paranoid_dag/model_additions.rb

Defined Under Namespace

Modules: DagLinkInstanceMethods

Instance Method Summary collapse

Instance Method Details

This allows you to make your dag paranoid, meaning that connections are not destroyed, but instead a ‘deleted_at’ attribute is set.

In your DagLink model replace

class DagLink < ActiveRecord::Base
  acts_as_dag_links options
end

by

class DagLink < ActiveRecord::Base
  acts_as_dag_links options, paranoid: true
end


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/acts_as_paranoid_dag/model_additions.rb', line 20

def acts_as_dag_links( params )

  # Find out whether the dag link should have the paranoid extension.
  paranoid = params[ :paranoid ]
  params.delete( :paranoid )

  # Call the original acts_as_dag_links method from the acts-as-dag gem.
  super params

  # If the dag links should be paranoid, load the corresponding extensions.
  if paranoid
    acts_as_paranoid
    include DagLinkInstanceMethods

    scope :now, where( "#{table_name}.deleted_at IS NULL" )

    def now_and_in_the_past
      without_paranoid_default_scope
    end

    def in_the_past
      without_paranoid_default_scope.only_deleted
    end

    def at_time( time )
      links = without_paranoid_default_scope
        .where( "created_at <= ?", time )
      links = links.where( :deleted_at => nil ) + links.where( "deleted_at >= ?", time )
      links
    end

  end
  
end