Commit 15dc7e8
Changed files (4)
app
controllers
models
spec
app/controllers/application_controller.rb
@@ -8,10 +8,14 @@ class ApplicationController < ActionController::Base
Spank::IOC.resolve(key)
end
+ def current_user
+ @current_user ||= @current_session.user
+ end
+
private
def ensure_valid_session
- unless session[:session_id] && Session.find(session[:session_id])
+ unless session[:session_id] && @current_session = Session.find(session[:session_id])
render nothing: true, status: :unauthorized
end
rescue ActiveRecord::RecordNotFound
app/models/session.rb
@@ -1,2 +1,3 @@
class Session < ActiveRecord::Base
+ belongs_to :user
end
spec/controllers/application_controller_spec.rb
@@ -3,18 +3,24 @@ require "spec_helper"
describe ApplicationController do
controller do
def index
+ current_user
render text: 'hello'
end
end
context "when signed in" do
- let(:user_session) { Object.new }
+ let(:user) { User.create!(password: 'password', password_confirmation: 'password') }
+ let(:user_session) { Session.create!(user: user) }
+
+ before { get :index, {}, session_id: user_session.id }
it "lets you continue to do whatever the heck you were trying to do" do
- Session.stub(:find).with(1).and_return(user_session)
- get :index, {}, session_id: 1
response.status.should == 200
end
+
+ it "loads the current user" do
+ assigns(:current_user).should == user
+ end
end
context "when not signed in" do
spec/controllers/dashboard_controller_spec.rb
@@ -2,9 +2,5 @@ require "spec_helper"
describe DashboardController do
context "#index" do
- it "loads the current user" do
- get :index
- assigns(:current_user).should == user
- end
end
end