Module: Paranoia

Defined in:
lib/paranoia.rb,
lib/paranoia/version.rb

Defined Under Namespace

Modules: Callbacks, Query

Constant Summary collapse

VERSION =
"2.1.3"
@@default_sentinel_value =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_sentinel_valueObject



11
12
13
# File 'lib/paranoia.rb', line 11

def self.default_sentinel_value
  @@default_sentinel_value
end

.default_sentinel_value=(val) ⇒ Object

Change default_sentinel_value in a rails initilizer



7
8
9
# File 'lib/paranoia.rb', line 7

def self.default_sentinel_value=(val)
  @@default_sentinel_value = val
end

.included(klazz) ⇒ Object



15
16
17
18
# File 'lib/paranoia.rb', line 15

def self.included(klazz)
  klazz.extend Query
  klazz.extend Callbacks
end

Instance Method Details

#deleteObject



85
86
87
# File 'lib/paranoia.rb', line 85

def delete
  touch_paranoia_column
end

#destroyObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/paranoia.rb', line 66

def destroy
  transaction do
    run_callbacks(:destroy) do
      result = touch_paranoia_column
      if result && ActiveRecord::VERSION::STRING >= '4.2'
        each_counter_cached_associations do |association|
          foreign_key = association.reflection.foreign_key.to_sym
          unless destroyed_by_association && destroyed_by_association.foreign_key.to_sym == foreign_key
            if send(association.reflection.name)
              association.decrement_counters
            end
          end
        end
      end
      result
    end
  end
end

#paranoia_destroyed?Boolean Also known as: deleted?

Returns:

  • (Boolean)


107
108
109
# File 'lib/paranoia.rb', line 107

def paranoia_destroyed?
  send(paranoia_column) != paranoia_sentinel_value
end

#restore!(opts = {}) ⇒ Object Also known as: restore



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/paranoia.rb', line 89

def restore!(opts = {})
  self.class.transaction do
    run_callbacks(:restore) do
      # Fixes a bug where the build would error because attributes were frozen.
      # This only happened on Rails versions earlier than 4.1.
      noop_if_frozen = ActiveRecord.version < Gem::Version.new("4.1")
      if (noop_if_frozen && !@attributes.frozen?) || !noop_if_frozen
        write_attribute paranoia_column, paranoia_sentinel_value
        update_column paranoia_column, paranoia_sentinel_value
      end
      restore_associated_records if opts[:recursive]
    end
  end

  self
end