Commit 9b2a5520 by Ryan Bigg

Merge pull request #178 from bluej100/require-primary-key

raise error for acts_as_paranoid on models without primary keys
parents ba0e7b92 6881e750
...@@ -154,6 +154,7 @@ end ...@@ -154,6 +154,7 @@ end
class ActiveRecord::Base class ActiveRecord::Base
def self.acts_as_paranoid(options={}) def self.acts_as_paranoid(options={})
raise "primary key required for "+self.name unless self.primary_key
alias :destroy! :destroy alias :destroy! :destroy
alias :delete! :delete alias :delete! :delete
def really_destroy! def really_destroy!
......
...@@ -28,6 +28,7 @@ def connect! ...@@ -28,6 +28,7 @@ def connect!
ActiveRecord::Base.connection.execute 'CREATE TABLE custom_column_models (id INTEGER NOT NULL PRIMARY KEY, destroyed_at DATETIME)' ActiveRecord::Base.connection.execute 'CREATE TABLE custom_column_models (id INTEGER NOT NULL PRIMARY KEY, destroyed_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE custom_sentinel_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME NOT NULL)' ActiveRecord::Base.connection.execute 'CREATE TABLE custom_sentinel_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME NOT NULL)'
ActiveRecord::Base.connection.execute 'CREATE TABLE non_paranoid_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER)' ActiveRecord::Base.connection.execute 'CREATE TABLE non_paranoid_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER)'
ActiveRecord::Base.connection.execute 'CREATE TABLE idless_models (deleted_at DATETIME)'
end end
class WithDifferentConnection < ActiveRecord::Base class WithDifferentConnection < ActiveRecord::Base
...@@ -620,6 +621,12 @@ class ParanoiaTest < test_framework ...@@ -620,6 +621,12 @@ class ParanoiaTest < test_framework
assert_equal 3, parent.very_related_models.size assert_equal 3, parent.very_related_models.size
end end
def test_model_without_primary_key
assert_raises(RuntimeError) do
IdlessModel.class_eval{ acts_as_paranoid }
end
end
private private
def get_featureful_model def get_featureful_model
FeaturefulModel.new(:name => "not empty") FeaturefulModel.new(:name => "not empty")
...@@ -758,3 +765,6 @@ class AsplodeModel < ActiveRecord::Base ...@@ -758,3 +765,6 @@ class AsplodeModel < ActiveRecord::Base
raise StandardError, 'ASPLODE!' raise StandardError, 'ASPLODE!'
end end
end end
class IdlessModel < ActiveRecord::Base
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