I’m using Devise for authentication in Ruby on Rails and I’m overriding the registration update controller to not require the current password for updates to the User model. So, basically the code below says “if the user does not provide the password, update using update_without_password, otherwise update using update_attributes“.
if resource_params["password"].empty?
if resource.update_without_password(resource_params)
if is_navigational_format?
if resource.respond_to?(:pending_reconfirmation?) && resource.pending_reconfirmation?
flash_key = :update_needs_confirmation
end
set_flash_message :notice, flash_key || :updated
end
sign_in resource_name, resource, :bypass => true
respond_with resource, :location => after_update_path_for(resource)
else
clean_up_passwords resource
respond_with resource
end
else
if resource.update_attributes(resource_params)
if is_navigational_format?
if resource.respond_to?(:pending_reconfirmation?) && resource.pending_reconfirmation?
flash_key = :update_needs_confirmation
end
set_flash_message :notice, flash_key || :updated
end
sign_in resource_name, resource, :bypass => true
respond_with resource, :location => after_update_path_for(resource)
else
clean_up_passwords resource
respond_with resource
end
end
Clearly, there is room to reduce code redundancy here, but I’m still new to ruby and was wondering if anyone could suggest a clean way to write the same thing without duplicating all the code in the nested if.
Thanks!
If I am reading you right, and not missing anything, there is only a single line of difference there. You can write it like this: