Skip to content

Commit

Permalink
Rails 4.0.0 compatibility
Browse files Browse the repository at this point in the history
- Get rid of observers tests (I tried to use rails-observer gems but it was a mess)
- Update deprecated calls like #scope or #default_scope without block
- Get rid of attr_protected call and tests
- Get rid of AR#destroy! NoMethodError test

The last one cause issue because in AR 4 #destroy! bang is defined.
It's the same as #destroy but raise an AR::RecordNotFound if the record
is not present in database.

It may be smart to find another name for this method. But for now it just works.

Conflicts:
	Gemfile
	Gemfile.lock
	acts_as_paranoid.gemspec
	test/test_observers.rb
  • Loading branch information
byroot authored and Zachary Scott committed Aug 26, 2014
1 parent e796b9f commit ec9c311
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 49 deletions.
10 changes: 5 additions & 5 deletions lib/acts_as_paranoid/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ def delete_all!(conditions = nil)
end

def delete_all(conditions = nil)
update_all ["#{paranoid_configuration[:column]} = ?", delete_now_value], conditions
where(conditions).update_all(["#{paranoid_configuration[:column]} = ?", delete_now_value])
end

def paranoid_default_scope_sql
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])).
self.all.table[paranoid_column].eq(nil).
or(self.all.table[paranoid_column].not_eq(paranoid_configuration[:deleted_value])).
to_sql
else
self.scoped.table[paranoid_column].eq(nil).to_sql
self.all.table[paranoid_column].eq(nil).to_sql
end
end

Expand Down Expand Up @@ -74,7 +74,7 @@ def delete_now_value
protected

def without_paranoid_default_scope
scope = self.scoped.with_default_scope
scope = self.all.with_default_scope
scope.where_values.delete(paranoid_default_scope_sql)

scope
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_paranoid/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def delete_all!(conditions = nil)

def delete_all(conditions = nil)
if paranoid?
update_all(paranoid_deletion_attributes, conditions)
where(conditions).update_all(paranoid_deletion_attributes)
else
delete_all!(conditions)
end
Expand Down
3 changes: 1 addition & 2 deletions test/test_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class ParanoidTest < ParanoidBaseTest
def test_paranoid?
assert !NotParanoid.paranoid?
assert_raise(NoMethodError) { NotParanoid.delete_all! }
assert_raise(NoMethodError) { NotParanoid.first.destroy! }
assert_raise(NoMethodError) { NotParanoid.with_deleted }
assert_raise(NoMethodError) { NotParanoid.only_deleted }

Expand Down Expand Up @@ -60,7 +59,7 @@ def test_real_removal

ParanoidTime.delete_all!
assert_empty ParanoidTime.all
assert_empty ParanoidTime.with_deleted.all
assert_empty ParanoidTime.with_deleted
end

def test_non_persisted_destroy
Expand Down
27 changes: 2 additions & 25 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,25 +324,6 @@ class InheritedParanoid < SuperParanoid
acts_as_paranoid
end

class ParanoidObserver < ActiveRecord::Observer
observe :paranoid_with_callback

attr_accessor :called_before_recover, :called_after_recover

def before_recover(paranoid_object)
self.called_before_recover = paranoid_object
end

def after_recover(paranoid_object)
self.called_after_recover = paranoid_object
end

def reset
self.called_before_recover = nil
self.called_after_recover = nil
end
end


class ParanoidManyManyParentLeft < ActiveRecord::Base
has_many :paranoid_many_many_children
Expand All @@ -365,8 +346,6 @@ class ParanoidWithScopedValidation < ActiveRecord::Base
validates_uniqueness_of :name, :scope => :category
end

ParanoidWithCallback.add_observer(ParanoidObserver.instance)

class ParanoidBaseTest < ActiveSupport::TestCase
def setup
setup_db
Expand All @@ -379,8 +358,6 @@ def setup
ParanoidString.create! :name => "strings can be paranoid"
NotParanoid.create! :name => "no paranoid goals"
ParanoidWithCallback.create! :name => "paranoid with callbacks"

ParanoidObserver.instance.reset
end

def teardown
Expand Down Expand Up @@ -416,7 +393,7 @@ class ParanoidForest < ActiveRecord::Base
require "active_support/core_ext/logger.rb"
ActiveRecord::Base.logger = Logger.new(StringIO.new)

scope :rainforest, where(:rainforest => true)
scope :rainforest, lambda{ where(:rainforest => true) }

has_many :paranoid_trees, :dependent => :destroy
end
Expand All @@ -429,7 +406,7 @@ class ParanoidTree < ActiveRecord::Base

class ParanoidHuman < ActiveRecord::Base
acts_as_paranoid
default_scope where('gender = ?', 'male')
default_scope { where('gender = ?', 'male') }
end

class ParanoidAndroid < ActiveRecord::Base
Expand Down
16 changes: 0 additions & 16 deletions test/test_observers.rb

This file was deleted.

0 comments on commit ec9c311

Please sign in to comment.