Commit 2f0c90d4

mo khan <mo@mokhan.ca>
2014-05-14 04:36:16
extract change password command.
1 parent 71f7cfe
Changed files (2)
app
app/controllers/passwords_controller.rb
@@ -6,14 +6,19 @@ class PasswordsController < ApplicationController
   end
 
   def update
-    @user = current_user
-    if @user.change_password(params[:user][:password], params[:user][:password_confirmation])
-      sign_in(@user, :bypass => true) unless Rails.env.test?
-      flash[:notice] = t('passwords.updated')
-      render :index
-    else
-      flash[:error] = t(:passwords_do_not_match)
-      render :index
-    end
+    ChangePassword.new(self).run(params[:user][:password], params[:user][:password_confirmation])
+  end
+
+  def password_changed(user)
+    @user = user
+    sign_in(@user, bypass: true) unless Rails.env.test?
+    flash[:notice] = t('passwords.updated')
+    render :index
+  end
+
+  def password_changed_failed(user)
+    @user = user
+    flash[:error] = t(:passwords_do_not_match)
+    render :index
   end
 end
app/services/commands/change_password.rb
@@ -0,0 +1,14 @@
+class ChangePassword
+  def initialize(context, current_user = context.current_user)
+    @context = context
+    @current_user = current_user
+  end
+
+  def run(password, password_confirmation)
+    if @current_user.change_password(password, password_confirmation)
+      @context.password_changed(@current_user)
+    else
+      @context.password_changed_failed(@current_user)
+    end
+  end
+end