Skip to content

Commit

Permalink
Move user association changes to before_update callback also
Browse files Browse the repository at this point in the history
  • Loading branch information
nimmolo committed Feb 19, 2024
1 parent 6279e29 commit 9b22706
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
19 changes: 0 additions & 19 deletions app/controllers/account/profile_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,36 +63,17 @@ def upload_image_if_present
end

def deal_with_possible_profile_changes
# compute legal name change now because @user.save will overwrite it
legal_name_change = @user.legal_name_change
# ditto for login_changed?
login_changed = @user.login_changed?

if !@user.changed
flash_notice(:runtime_no_changes.t)
redirect_to(user_path(@user.id))
elsif !@user.save
flash_object_errors(@user)
render(:edit) and return
else
update_copyright_holder(legal_name_change)
expire_caches_of_associated_observations if login_changed
maybe_update_location_and_finish
end
end

def update_copyright_holder(legal_name_change = nil)
return unless legal_name_change

Image.update_copyright_holder(*legal_name_change, @user)
end

# EXPIRE CACHES OF OBS WITH CHANGED USER LOGINS
# "touch" the updated_at column of all observations with the changed login
def expire_caches_of_associated_observations
Observation.where(user_id: @user.id).touch_all
end

def maybe_update_location_and_finish
if @need_location
flash_notice(:runtime_profile_must_define.t)
Expand Down
17 changes: 17 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ class User < AbstractModel # rubocop:disable Metrics/ClassLength
# go through +change_password+.)
before_create :crypt_password

before_update :update_image_copyright_holder
before_update :expire_caches_of_associated_observations

# Ensure that certain default values are symbols (rather than strings)
# might only be an issue for test environment?
# Probably better to instead use after_create and after_update,
Expand Down Expand Up @@ -484,6 +487,20 @@ def legal_name_change
[old_legal_name, new_legal_name]
end

def update_image_copyright_holder
return unless legal_name_change

Image.update_copyright_holder(*legal_name_change, self)
end

# EXPIRE CACHES OF OBS WITH CHANGED USER LOGINS
# "touch" the updated_at column of all observations with the changed login
def expire_caches_of_associated_observations
return unless name_changed? || login_changed?

Observation.where(user_id: id).touch_all
end

##############################################################################
#
# :section: Authentication
Expand Down

0 comments on commit 9b22706

Please sign in to comment.