Skip to content

Commit

Permalink
Fix response override from internal middleware. Put action instances …
Browse files Browse the repository at this point in the history
…in env.
  • Loading branch information
jodosha committed Dec 1, 2014
1 parent 350e391 commit bea967f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/lotus/application.rb
Expand Up @@ -153,7 +153,7 @@ def name
# @see Lotus::Application#middleware
def call(env)
middleware.call(env).tap do |response|
@rendering_policy.render(response)
@rendering_policy.render(env, response)
end
end

Expand Down
7 changes: 5 additions & 2 deletions lib/lotus/frameworks.rb
Expand Up @@ -6,9 +6,12 @@ module Lotus
module Frameworks
module Action
module Rack
ENV_KEY = 'lotus.action'.freeze

protected
def response
super << self
def finish
super
@_env[ENV_KEY] = self
end
end
end
Expand Down
14 changes: 6 additions & 8 deletions lib/lotus/rendering_policy.rb
Expand Up @@ -12,7 +12,7 @@ class RenderingPolicy
HEADERS = 1
BODY = 2

RACK_RESPONSE_SIZE = 3
LOTUS_ACTION = 'lotus.action'.freeze

SUCCESSFUL_STATUSES = (200..201).freeze
STATUSES_WITHOUT_BODY = Set.new((100..199).to_a << 204 << 205 << 301 << 302 << 304).freeze
Expand All @@ -26,17 +26,15 @@ def initialize(configuration)
@templates = configuration.templates
end

def render(response)
if renderable?(response)
action = response.pop

def render(env, response)
if action = renderable?(env)
body = if successful?(response)
view_for(response, action).render(
action.to_rendering
)
else
if render_status_page?(response, action)
Lotus::Views::Default.render(@templates, response[STATUS],response: response, format: :html)
Lotus::Views::Default.render(@templates, response[STATUS], response: response, format: :html)
end
end

Expand All @@ -45,8 +43,8 @@ def render(response)
end

private
def renderable?(response)
response.size > RACK_RESPONSE_SIZE
def renderable?(env)
env.delete(LOTUS_ACTION)
end

def successful?(response)
Expand Down
7 changes: 3 additions & 4 deletions test/fixtures/sessions/application.rb
@@ -1,11 +1,10 @@
module SessionsApp
class Application < Lotus::Application
configure do
# Activate sessions
sessions :cookie, secret: '1234567890'

routes do
post '/set_session' , to: 'sessions#new'
post '/set_session' , to: 'sessions#create'
get '/get_session' , to: 'sessions#show'
delete '/clear_session', to: 'sessions#destroy'
end
Expand All @@ -16,7 +15,7 @@ class Application < Lotus::Application


module Controllers::Sessions
class New
class Create
include SessionsApp::Action

def call(params)
Expand All @@ -29,7 +28,7 @@ class Show
include SessionsApp::Action

def call(params)
self.body = session[:name]
self.body = session[:name] || '[empty]'
end
end

Expand Down
7 changes: 3 additions & 4 deletions test/integration/sessions_test.rb
Expand Up @@ -31,7 +31,7 @@ def request
it 'has empty session by default' do
get '/get_session'

response.body.must_equal ''
response.body.must_equal '[empty]'
end

it 'allows to set session' do
Expand All @@ -49,12 +49,11 @@ def request

it 'allows to clear session' do
post '/set_session', { name: 'Lotus' }
delete '/clear_session', nil, { 'HTTP_COOKIE' => response.headers['Set-Cookie'] }

delete '/clear_session', nil, { 'HTTP_COOKIE' => response.headers['Set-Cookie'] }
response.body.must_equal 'Session cleared for: Lotus'

get '/get_session', nil, { 'HTTP_COOKIE' => response.headers['Set-Cookie'] }

response.body.must_equal ''
response.body.must_equal '[empty]'
end
end

0 comments on commit bea967f

Please sign in to comment.