Skip to content

Commit

Permalink
#280 layout() method in controller does not override getLayout() in c…
Browse files Browse the repository at this point in the history
…ontrollers
  • Loading branch information
Igor Polevoy committed May 13, 2016
1 parent 0cdc1c4 commit bb3e090
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 2 deletions.
@@ -0,0 +1,21 @@
package app.controllers;

import org.javalite.activeweb.AppController;

/**
* @author Igor Polevoy on 5/13/16.
*/
public class OverrideLayoutController extends AppController {

public void index(){}

public void action(){
render().layout("/layouts/default_layout");
}


@Override
protected String getLayout() {
return "/layouts/override_layout";
}
}
@@ -0,0 +1,46 @@
/*
Copyright 2009-2014 Igor Polevoy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package app.controllers;

import org.javalite.activeweb.ControllerSpec;
import org.junit.Before;
import org.junit.Test;

/**
* @author Igor Polevoy
*/
public class OverrideLayoutControllerSpec extends ControllerSpec {

@Before
public void before(){
setTemplateLocation("src/test/views");
}

@Test
public void shouldUseControllerLayout(){
request().get("index");
a(responseContent()).shouldBeEqual("Override layout top Index Override layout bottom");
a(layout()).shouldBeEqual("/layouts/override_layout");
}

@Test
public void shouldUseDefaultLayoutFromAction(){
request().get("action");
a(responseContent()).shouldBeEqual("override");
a(layout()).shouldBeEqual("/layouts/default_layout");
}
}
@@ -0,0 +1 @@
Override layout top ${page_content} Override layout bottom
@@ -0,0 +1 @@
override
1 change: 1 addition & 0 deletions activeweb-testing/src/test/views/override_layout/index.ftl
@@ -0,0 +1 @@
Index
Expand Up @@ -151,8 +151,7 @@ private void renderResponse(Route route, boolean integrateViews) throws Instant
//this is configuration of explicit response. If render() method was called in controller, we already have instance of
// response on current thread.
private void configureExplicitResponse(Route route, String controllerLayout, RenderTemplateResponse resp) throws InstantiationException, IllegalAccessException {
String responseLayout = resp.getLayout();
if(!Configuration.getDefaultLayout().equals(controllerLayout) && Configuration.getDefaultLayout().equals(responseLayout)){
if(!Configuration.getDefaultLayout().equals(controllerLayout) && resp.hasDefaultLayout()){
resp.setLayout(controllerLayout);
}
if(resp.getContentType() == null){
Expand Down
Expand Up @@ -26,6 +26,7 @@ class RenderTemplateResponse extends ControllerResponse{
private String template, format;
private String layout = Configuration.getDefaultLayout();
private TemplateManager templateManager;
private boolean defaultLayout = true;

/**
* Constructs a response object for rendering pages. This can be used for regular responses.
Expand Down Expand Up @@ -70,6 +71,11 @@ public Map values(){

public void setLayout(String layout) {
this.layout = layout;
this.defaultLayout = false; // in some bizarre cases, when you need default_layout set manually inside action!
}

public boolean hasDefaultLayout(){
return defaultLayout;
}

protected void setTemplateManager(TemplateManager templateManager){
Expand Down

0 comments on commit bb3e090

Please sign in to comment.