Skip to content

Instantly share code, notes, and snippets.

@waseem
Last active August 29, 2015 13:57
Show Gist options
  • Save waseem/9657432 to your computer and use it in GitHub Desktop.
Save waseem/9657432 to your computer and use it in GitHub Desktop.
Class User < ActiveRecord::Base
def update_password(params)
if authenticate(params['current_password'])
self.password = params['password']
self.password_confirmation = params['password_confirmation']
class << self
validates :password, length: { minimum: 6 }
end
!!save
else
self.errors[:current_password] << 'is wrong'
false
end
end
end
## In controller user.update_password(params)
class UserService
def self.update_password(user, params)
if user.authenticate(params['current_password'])
user.password = params['password']
user.password_confirmation = params['password_confirmation']
class << user
validates :password, length: { minimum: 6 }
end
!!user.save
else
user.errors[:current_password] << 'is wrong'
false
end
end
end
## In controller ::UserService.update_password(user, params)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment