Skip to content

Commit d50b1dd

Browse files
author
David Davis
committedApr 23, 2013
Merge pull request #2030 from daviddavis/temp_1366666322
Moving before_destroy callbacks because of rails/rails#3458
2 parents 1f7af43 + bc7eb80 commit d50b1dd

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed
 

‎app/models/kt_environment.rb

+4-2
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

+2-4
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

+5-3
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
@@ -61,8 +65,6 @@ class User < ActiveRecord::Base
6165
after_validation :setup_remote_id
6266
before_save :hash_password, :setup_preferences
6367
after_save :create_or_update_default_system_registration_permission
64-
# THIS CHECK MUST BE THE FIRST before_destroy
65-
before_destroy :is_last_super_user?, :destroy_own_role
6668

6769
# hash the password before creating or updateing the record
6870
def hash_password
@@ -75,7 +77,7 @@ def setup_preferences
7577
self.preferences = HashWithIndifferentAccess.new unless self.preferences
7678
end
7779

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

‎test/models/user_test.rb

+14
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)
Please sign in to comment.