Commit 6cf750a9 by Diego Aguir Selzlein

Using guard clauses.

parent 265a22f9
...@@ -22,16 +22,15 @@ module Paranoia ...@@ -22,16 +22,15 @@ module Paranoia
def with_deleted def with_deleted
if ActiveRecord::VERSION::STRING >= "4.1" if ActiveRecord::VERSION::STRING >= "4.1"
unscope where: paranoia_column return unscope where: paranoia_column
else
all.tap { |x| x.default_scoped = false }
end end
all.tap { |x| x.default_scoped = false }
end end
def only_deleted def only_deleted
if paranoia_sentinel_value.nil? if paranoia_sentinel_value.nil?
with_deleted.where.not(paranoia_column => paranoia_sentinel_value) return with_deleted.where.not(paranoia_column => paranoia_sentinel_value)
else end
# if paranoia_sentinel_value is not null, then it is possible that # if paranoia_sentinel_value is not null, then it is possible that
# some deleted rows will hold a null value in the paranoia column # some deleted rows will hold a null value in the paranoia column
# these will not match != sentinel value because "NULL != value" is # these will not match != sentinel value because "NULL != value" is
...@@ -39,7 +38,6 @@ module Paranoia ...@@ -39,7 +38,6 @@ module Paranoia
quoted_paranoia_column = connection.quote_column_name(paranoia_column) quoted_paranoia_column = connection.quote_column_name(paranoia_column)
with_deleted.where("#{quoted_paranoia_column} IS NULL OR #{quoted_paranoia_column} != ?", paranoia_sentinel_value) with_deleted.where("#{quoted_paranoia_column} IS NULL OR #{quoted_paranoia_column} != ?", paranoia_sentinel_value)
end end
end
alias :deleted :only_deleted alias :deleted :only_deleted
def restore(id_or_ids, opts = {}) def restore(id_or_ids, opts = {})
...@@ -78,16 +76,13 @@ module Paranoia ...@@ -78,16 +76,13 @@ module Paranoia
transaction do transaction do
run_callbacks(:destroy) do run_callbacks(:destroy) do
result = delete result = delete
if result && ActiveRecord::VERSION::STRING >= '4.2' next result unless result && ActiveRecord::VERSION::STRING >= '4.2'
each_counter_cached_associations do |association| each_counter_cached_associations do |association|
foreign_key = association.reflection.foreign_key.to_sym foreign_key = association.reflection.foreign_key.to_sym
unless destroyed_by_association && destroyed_by_association.foreign_key.to_sym == foreign_key next if destroyed_by_association && destroyed_by_association.foreign_key.to_sym == foreign_key
if send(association.reflection.name) next unless send(association.reflection.name)
association.decrement_counters association.decrement_counters
end end
end
end
end
result result
end end
end end
...@@ -202,13 +197,11 @@ class ActiveRecord::Base ...@@ -202,13 +197,11 @@ class ActiveRecord::Base
association_data = self.send(name) association_data = self.send(name)
# has_one association can return nil # has_one association can return nil
# .paranoid? will work for both instances and classes # .paranoid? will work for both instances and classes
if association_data && association_data.paranoid? next unless association_data && association_data.paranoid?
if reflection.collection? if reflection.collection?
association_data.with_deleted.each(&:really_destroy!) next association_data.with_deleted.each(&:really_destroy!)
else
association_data.really_destroy!
end
end end
association_data.really_destroy!
end end
end end
write_attribute(paranoia_column, current_time_from_proper_timezone) write_attribute(paranoia_column, current_time_from_proper_timezone)
...@@ -268,11 +261,8 @@ module ActiveRecord ...@@ -268,11 +261,8 @@ module ActiveRecord
protected protected
def build_relation_with_paranoia(klass, table, attribute, value) def build_relation_with_paranoia(klass, table, attribute, value)
relation = build_relation_without_paranoia(klass, table, attribute, value) relation = build_relation_without_paranoia(klass, table, attribute, value)
if klass.respond_to?(:paranoia_column) return relation unless klass.respond_to?(:paranoia_column)
relation.and(klass.arel_table[klass.paranoia_column].eq(klass.paranoia_sentinel_value)) relation.and(klass.arel_table[klass.paranoia_column].eq(klass.paranoia_sentinel_value))
else
relation
end
end end
alias_method_chain :build_relation, :paranoia alias_method_chain :build_relation, :paranoia
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment