diff options
| author | mo khan <mo@mokhan.ca> | 2013-05-19 17:21:41 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2013-05-19 17:21:41 -0600 |
| commit | 28d709192ed34f1f6759001fdfb02eba81b125ad (patch) | |
| tree | 0de912672c10767a84dcbd164c5adcf6e0d86cfa | |
| parent | d8893dabb2a4145cb9749008a692f7b32c7a0e35 (diff) | |
load user if logged in
| -rw-r--r-- | lib/commands/dashboard/index.html.erb | 2 | ||||
| -rw-r--r-- | lib/commands/dashboard/index_command.rb | 16 | ||||
| -rw-r--r-- | lib/commands/sessions/new.html.erb | 2 | ||||
| -rw-r--r-- | lib/web/responses/cookies.rb | 2 | ||||
| -rw-r--r-- | spec/specs/commands/dashboard/index_command_spec.rb | 17 | ||||
| -rw-r--r-- | spec/specs/web/asset_response_spec.rb | 2 |
6 files changed, 32 insertions, 9 deletions
diff --git a/lib/commands/dashboard/index.html.erb b/lib/commands/dashboard/index.html.erb index 9497ef8..391e032 100644 --- a/lib/commands/dashboard/index.html.erb +++ b/lib/commands/dashboard/index.html.erb @@ -24,7 +24,7 @@ </div><!--/span--> <div class="span9"> <div class="hero-unit"> - <h1>Hello, world!</h1> + <h1>Hello, <%= self.model.username %>!</h1> <p>This is a template for a simple marketing or informational website. It includes a large callout called the hero unit and three supporting pieces of content. Use it as a starting point to create something more unique.</p> <p><a href="#" class="btn btn-primary btn-large">Learn more »</a></p> </div> diff --git a/lib/commands/dashboard/index_command.rb b/lib/commands/dashboard/index_command.rb index dbc48fb..92d8342 100644 --- a/lib/commands/dashboard/index_command.rb +++ b/lib/commands/dashboard/index_command.rb @@ -6,13 +6,23 @@ module Booty class IndexCommand < Booty::RouteCommand handles :uri => /^\/$/, :verb => :GET - def initialize(view_engine) + def initialize(view_engine, users_repository) @view_engine = view_engine + @users = users_repository end def respond_to(request) - p request.cookies - HtmlResponse.new(:template => "/dashboard/index") + HtmlResponse.new(:template => "/dashboard/index", :model => lookup_user_from(request)) + end + + private + + def lookup_user_from(request) + if request.cookies.has_key?(Cookies::SESSION) + @users.find_by(request.cookies[Cookies::SESSION]) + else + OpenStruct.new(:username => 'world') + end end end end diff --git a/lib/commands/sessions/new.html.erb b/lib/commands/sessions/new.html.erb index 20908e9..a4d8b34 100644 --- a/lib/commands/sessions/new.html.erb +++ b/lib/commands/sessions/new.html.erb @@ -20,7 +20,7 @@ <div class="control-group"> <label class="control-label" for="register-email">Email</label> <div class="controls"> - <input type="text" id="register-email" name="email" placeholder="Email"> + <input type="text" id="register-email" name="username" placeholder="Email"> </div> </div> <div class="control-group"> diff --git a/lib/web/responses/cookies.rb b/lib/web/responses/cookies.rb index ca25613..266e154 100644 --- a/lib/web/responses/cookies.rb +++ b/lib/web/responses/cookies.rb @@ -1,3 +1,3 @@ module Cookies - SESSION=:s + SESSION='s' end diff --git a/spec/specs/commands/dashboard/index_command_spec.rb b/spec/specs/commands/dashboard/index_command_spec.rb index 9bce494..a95d99d 100644 --- a/spec/specs/commands/dashboard/index_command_spec.rb +++ b/spec/specs/commands/dashboard/index_command_spec.rb @@ -3,38 +3,51 @@ require 'spec_helper' module Booty module Dashboard describe IndexCommand do - let(:sut) { IndexCommand.new(view_engine) } + let(:sut) { IndexCommand.new(view_engine, users_repository) } let(:view_engine) { fake } + let(:users_repository) { fake } + context "when a request for the root of the website is received" do context "when it's for the root of the website" do let(:request) { { "REQUEST_PATH"=>"/", "REQUEST_METHOD" => "GET" } } + it "should indicate that it can process the request" do sut.matches?(request).should be_true end + it "should have a class method that matches the request" do IndexCommand.matches?(request).should be_true end end + context "when it is for another path" do let(:request) { { "REQUEST_PATH"=>"/hi/mo/1", "REQUEST_METHOD" => "GET" } } + it "should indicate that it cannot process the request" do sut.matches?(request).should be_false end + it "should have a class method that does not match the request" do IndexCommand.matches?(request).should be_false end end end + context "when rendering the home page" do let(:html) { "<html></html>" } + let(:request) { fake } + before :each do + request.stub(:cookies).and_return({}) view_engine.stub(:render).with(:template => "/dashboard/index").and_return(html) end - let(:result) { sut.run(nil) } + + let(:result) { sut.respond_to(request) } it "should return the proper status" do result.status.should == 200 end + it "should return the proper template" do result.template.should == "/dashboard/index" end diff --git a/spec/specs/web/asset_response_spec.rb b/spec/specs/web/asset_response_spec.rb index ea61ce4..8187a9f 100644 --- a/spec/specs/web/asset_response_spec.rb +++ b/spec/specs/web/asset_response_spec.rb @@ -8,7 +8,7 @@ describe Booty::AssetResponse do let(:result) { sut.run(view_engine) } it "should return the proper response" do - result.should == [200, {"Content-Type" => "text/css"}, [File.read('assets/css/bootstrap.css')]] + result.should == [200, {"Content-Type" => "text/css", "Cache-control" => "public, max-age=31536000", "Expires" => "Wed, 01 Jan 2014 22:00:00 GMT" }, [File.read('assets/css/bootstrap.css')]] end end end |
