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)


170
171
172
173
174
175
176
177
178
# File 'lib/acts_as_paranoid/core.rb', line 170

def deleted?
  return false if paranoid_value.nil?

  if self.class.column_type_with_deleted_value?
    paranoid_value == self.class.delete_now_value
  else
    return true
  end
end

#destroyObject



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

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]) if persisted?
        self.paranoid_value = self.class.delete_now_value
        self
      end
    end
  else
    destroy!
  end
end

#destroy!Object



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

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]) if persisted?
      self.paranoid_value = self.class.delete_now_value
      freeze
    end
  end
end

#destroy_dependent_associations!Object



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

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



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

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

#persisted?Boolean

Returns:

  • (Boolean)


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

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

#recover(options = {}) ⇒ Object



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

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



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

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