Commit f0c61dca
Changed files (5)
app
config
locales
spec
controllers
app/controllers/passwords_controller.rb
@@ -1,7 +1,16 @@
class PasswordsController < ApplicationController
before_filter :authenticate_user!
+ def show
+ end
+
def update
-
+ user = User.find(params[:id])
+ if user.change_password(params[:user][:password], params[:user][:password_confirmation])
+ render :show
+ else
+ flash[:alert] = t(:passwords_do_not_match)
+ render :show
+ end
end
end
app/models/user.rb
@@ -28,6 +28,12 @@ class User < ActiveRecord::Base
Comment.create_for(self, creation, comment)
end
+ def change_password(password, confirmation)
+ return false unless password == confirmation
+ self.password = password
+ self.save!
+ end
+
def to_param
"#{id}-#{name.gsub(/[^a-z0-9]+/i, '-')}"
end
app/views/passwords/show.html.erb
config/locales/en.yml
@@ -3,4 +3,6 @@
en:
hello: "Hello world"
- :share_something: "You have not uploaded anything!"
+ share_something: "You have not uploaded anything!"
+ passwords_do_not_match: 'Oops... the passwords should match.'
+
spec/controllers/passwords_controller_spec.rb
@@ -10,5 +10,35 @@ describe PasswordsController do
response.should redirect_to(new_user_session_path)
end
end
+
+ context "when logged in" do
+ let(:user) { FactoryGirl.create(:user) }
+
+ before { http_login(user) }
+
+ context "when the new password and the confirmation password does not match" do
+ before { put :update, :id => user.id, :user => { :password => 'foobar', :password_confirmation => 'barfoo' } }
+
+ it "should display an error on the page" do
+ flash[:alert].should == I18n.t(:passwords_do_not_match)
+ end
+
+ it "should render the show template" do
+ response.should render_template(:show)
+ end
+ end
+
+ context "when the password and confirmation match" do
+ let(:new_password) { 'booyakasham' }
+
+ before :each do
+ put :update, :id => user.id, :user => { :password => new_password, :password_confirmation => new_password }
+ end
+
+ it "should update the users password" do
+ user.reload.valid_password?(new_password).should be_true
+ end
+ end
+ end
end
end