Skip to content

Commit d391ad5

Browse files
David Davisjlsherrill
David Davis
authored andcommitted
Moving before_destroy callbacks because of rails/rails#3458 (cherry picked from commit 5630b13)
1 parent 84e83cd commit d391ad5

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

app/models/kt_environment.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class KTEnvironment < ActiveRecord::Base
2121
include Ext::PermissionTagCleanup
2222
acts_as_reportable
2323

24+
# RAILS3458: before_destroys before associations. see http://tinyurl.com/rails3458
25+
before_destroy :confirm_last_env
26+
before_destroy :delete_default_view_version
27+
2428
belongs_to :organization, :inverse_of => :environments
2529
has_many :activation_keys, :dependent => :destroy, :foreign_key => :environment_id
2630
has_and_belongs_to_many :priors, {:class_name => "KTEnvironment", :foreign_key => :environment_id,
@@ -68,8 +72,6 @@ def <<(*items)
6872
validates_with Validators::PathDescendentsValidator
6973

7074
after_create :create_default_content_view_version
71-
before_destroy :confirm_last_env
72-
before_destroy :delete_default_view_version
7375

7476
after_destroy :unset_users_with_default
7577
ERROR_CLASS_NAME = "Environment"

app/models/permission.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
1212

1313
class Permission < ActiveRecord::Base
14+
before_destroy :check_locked # RAILS3458: must be before dependent associations http://tinyurl.com/rails3458
15+
1416
belongs_to :resource_type
1517
belongs_to :organization
1618
belongs_to :role, :inverse_of => :permissions
@@ -24,11 +26,7 @@ class Permission < ActiveRecord::Base
2426
validates :name, :presence => true
2527
validates_with Validators::NonHtmlNameValidator, :attributes => :name
2628
validates_with Validators::KatelloDescriptionFormatValidator, :attributes => :description
27-
2829
validates_uniqueness_of :name, :scope => [:organization_id, :role_id], :message => N_("Label has already been taken")
29-
30-
before_destroy :check_locked
31-
3230
validates_with Validators::PermissionValidator
3331
validates_presence_of :resource_type
3432

app/models/user.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class User < ActiveRecord::Base
2929
scope :hidden, where(:hidden => true)
3030
scope :visible, where(:hidden => false)
3131

32+
# RAILS3458: THIS CHECK MUST BE THE FIRST before_destroy AND
33+
# PROCEED DEPENDENT ASSOCIATIONS tinyurl.com/rails3458
34+
before_destroy :not_last_super_user?, :destroy_own_role
35+
3236
has_many :roles_users
3337
has_many :roles, :through => :roles_users, :before_remove => :super_admin_check, :uniq => true, :extend => RolesPermissions::UserOwnRole
3438
validates_with Validators::OwnRolePresenceValidator, :attributes => :roles
@@ -60,8 +64,6 @@ class User < ActiveRecord::Base
6064
after_validation :setup_remote_id
6165
before_save :hash_password, :setup_preferences
6266
after_save :create_or_update_default_system_registration_permission
63-
# THIS CHECK MUST BE THE FIRST before_destroy
64-
before_destroy :is_last_super_user?, :destroy_own_role
6567

6668
# hash the password before creating or updateing the record
6769
def hash_password
@@ -74,7 +76,7 @@ def setup_preferences
7476
self.preferences = HashWithIndifferentAccess.new unless self.preferences
7577
end
7678

77-
def is_last_super_user?
79+
def not_last_super_user?
7880
if !User.current.nil?
7981
if self.id == User.current.id
8082
self.errors.add(:base, _("Cannot delete currently logged user"))

test/models/user_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,18 @@ def test_default_env_removed
418418
assert_nil @user.default_environment
419419
end
420420

421+
def test_before_destroy
422+
# RAILS3458: Check that before_destroy callback is executed first http://tinyurl.com/rails3458
423+
User.stubs(:current).returns(@user)
424+
SearchFavorite.create!(:params => "abc",
425+
:path => "/garlic_naan",
426+
:user_id => @user.id
427+
)
428+
@user.search_favorites.reload
429+
refute_empty @user.search_favorites
430+
refute @user.destroy
431+
432+
refute_empty @user.search_favorites
433+
end
434+
421435
end

0 commit comments

Comments
 (0)