Module: ActsAsParanoid::Core

Defined in:
lib/acts_as_paranoid/core.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/acts_as_paranoid/core.rb', line 3

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#deleted?Boolean Also known as: destroyed?

Returns:

  • (Boolean)


171
172
173
174
# File 'lib/acts_as_paranoid/core.rb', line 171

def deleted?
  !(paranoid_value.nil? ||
    (self.class.string_type_with_deleted_value? && paranoid_value != self.class.delete_now_value))
end

#destroyObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/acts_as_paranoid/core.rb', line 104

def destroy
  if !deleted?
    with_transaction_returning_status do
      run_callbacks :destroy do
        # Handle composite keys, otherwise we would just use `self.class.primary_key.to_sym => self.id`.
        self.class.delete_all(Hash[[Array(self.class.primary_key), Array(self.id)].transpose])
        self.paranoid_value = self.class.delete_now_value
        self
      end
    end
  else
    destroy!
  end
end

#destroy!Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/acts_as_paranoid/core.rb', line 92

def destroy!
  with_transaction_returning_status do
    run_callbacks :destroy do
      destroy_dependent_associations!
      # Handle composite keys, otherwise we would just use `self.class.primary_key.to_sym => self.id`.
      self.class.delete_all!(Hash[[Array(self.class.primary_key), Array(self.id)].transpose])
      self.paranoid_value = self.class.delete_now_value
      freeze
    end
  end
end

#destroy_dependent_associations!Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/acts_as_paranoid/core.rb', line 156

def destroy_dependent_associations!
  self.class.dependent_associations.each do |reflection|
    next unless reflection.klass.paranoid?

    scope = reflection.klass.only_deleted

    # Merge in the association's scope
    scope = scope.merge(association(reflection.name).association_scope)

    scope.each do |object|
      object.destroy!
    end
  end
end

#paranoid_valueObject



88
89
90
# File 'lib/acts_as_paranoid/core.rb', line 88

def paranoid_value
  self.send(self.class.paranoid_column)
end

#persisted?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/acts_as_paranoid/core.rb', line 84

def persisted?
  !(new_record? || @destroyed)
end

#recover(options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/acts_as_paranoid/core.rb', line 119

def recover(options={})
  options = {
    :recursive => self.class.paranoid_configuration[:recover_dependent_associations],
    :recovery_window => self.class.paranoid_configuration[:dependent_recovery_window]
  }.merge(options)

  self.class.transaction do
    run_callbacks :recover do
      recover_dependent_associations(options[:recovery_window], options) if options[:recursive]

      self.paranoid_value = nil
      self.save
    end
  end
end

#recover_dependent_associations(window, options) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/acts_as_paranoid/core.rb', line 135

def recover_dependent_associations(window, options)
  self.class.dependent_associations.each do |reflection|
    next unless reflection.klass.paranoid?

    scope = reflection.klass.only_deleted

    # Merge in the association's scope
    scope = scope.merge(association(reflection.name).association_scope)

    # We can only recover by window if both parent and dependant have a
    # paranoid column type of :time.
    if self.class.paranoid_column_type == :time && reflection.klass.paranoid_column_type == :time
      scope = scope.merge(reflection.klass.deleted_inside_time_window(paranoid_value, window))
    end

    scope.each do |object|
      object.recover(options)
    end
  end
end