Skip to content

Commit

Permalink
Extend ActiveRecord::Base with paranoid associations.
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalossilva committed May 1, 2012
1 parent 7040c18 commit 6c9e955
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
8 changes: 3 additions & 5 deletions lib/acts_as_paranoid/associations.rb
Expand Up @@ -2,18 +2,16 @@ module ActsAsParanoid
module Associations
def self.included(base)
base.extend ClassMethods
base.class_eval do
class << self
alias_method_chain :belongs_to, :deleted
end
class << base
alias_method_chain :belongs_to, :deleted
end
end

module ClassMethods
def belongs_to_with_deleted(target, options = {})
with_deleted = options.delete(:with_deleted)
result = belongs_to_without_deleted(target, options)

if with_deleted
class_eval <<-RUBY, __FILE__, __LINE__
def #{target}_with_unscoped(*args)
Expand Down
1 change: 0 additions & 1 deletion lib/acts_as_paranoid/relation.rb
Expand Up @@ -2,7 +2,6 @@ module ActsAsParanoid
module Relation
def self.included(base)
base.class_eval do

def paranoid?
klass.try(:paranoid?) ? true : false
end
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_paranoid/validations.rb
Expand Up @@ -25,7 +25,7 @@ def validate_each(record, attribute, value)
relation = relation.and(table[scope_item].eq(scope_value))
end

# Re-add ActsAsParanoid default scope conditions manually.
# Re-add ActsAsParanoid default scope conditions manually.
if finder_class.unscoped.where(finder_class.paranoid_default_scope_sql).where(relation).exists?
record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
end
Expand Down
4 changes: 3 additions & 1 deletion lib/rails3_acts_as_paranoid.rb
Expand Up @@ -33,7 +33,6 @@ def acts_as_paranoid(options = {})
return if paranoid?

include ActsAsParanoid::Core
include ActsAsParanoid::Associations

# Magic!
default_scope { where(paranoid_default_scope_sql) }
Expand All @@ -52,6 +51,9 @@ def acts_as_paranoid(options = {})
# Extend ActiveRecord's functionality
ActiveRecord::Base.send :extend, ActsAsParanoid

# Extend ActiveRecord::Base with paranoid associations
ActiveRecord::Base.send :include, ActsAsParanoid::Associations

# Override ActiveRecord::Relation's behavior
ActiveRecord::Relation.send :include, ActsAsParanoid::Relation

Expand Down

0 comments on commit 6c9e955

Please sign in to comment.