Permalink
Browse files

lazy load self.columns in default_scope

  • Loading branch information...
1 parent 11e1a01 commit 2eca9161df926b72ede0683734f3fcb38818449b @mvj3 committed Apr 11, 2013
Showing with 10 additions and 10 deletions.
  1. +9 −1 lib/acts_as_paranoid.rb
  2. +1 −9 lib/acts_as_paranoid/core.rb
@@ -35,7 +35,13 @@ def acts_as_paranoid(options = {})
include ActsAsParanoid::Core
# Magic!
- default_scope { where(paranoid_default_scope_sql) }
+ default_scope do
+ # lazy load self.columns
+ if (self.paranoid_configuration[:column_type] == "boolean") && (self.columns.detect {|column| column.name == self.paranoid_configuration[:column] }.default === false)
+ self.paranoid_configuration.merge!({:deleted_value => false})
+ end
+ where(paranoid_default_scope_sql)
+ end
# The paranoid column should not be mass-assignable
attr_protected paranoid_configuration[:column]
@@ -48,7 +54,9 @@ def acts_as_paranoid(options = {})
scope :deleted_after_time, lambda { |time| where("#{paranoid_column} > ?", time) }
scope :deleted_before_time, lambda { |time| where("#{paranoid_column} < ?", time) }
end
+
end
+
end
# Extend ActiveRecord's functionality
@@ -35,15 +35,6 @@ def delete_all(conditions = nil)
end
def paranoid_default_scope_sql
- # lazy load self.columns
- @@set_paranoid_configuration_deleted_value ||= {}
- @@set_paranoid_configuration_deleted_value[self.table_name.to_sym] ||= begin
- if (paranoid_configuration[:column_type] == "boolean") && (self.columns.detect {|column| column.name == self.paranoid_configuration[:column] }.default === false)
- self.paranoid_configuration.merge!({:deleted_value => false})
- end
- true
- end
-
if string_type_with_deleted_value?
self.scoped.table[paranoid_column].eq(nil).or(self.scoped.table[paranoid_column].not_eq(paranoid_configuration[:deleted_value])).to_sql
else
@@ -179,6 +170,7 @@ def deleted?
alias_method :destroyed?, :deleted?
+
private
def paranoid_value=(value)

0 comments on commit 2eca916

Please sign in to comment.