require 'setup' spec :Views do context :layouts do it 'inherits layout from superclass' do a = mock_controller { layout :master } b = mock_controller(a) { } assert(b.initialize_controller.layout) == :master end it 'overrides layout inherited from superclass' do a = mock_controller { layout :master } b = mock_controller(a) { layout :baster } assert(b.initialize_controller.layout) == :baster end it 'can use `inherit` to override layout inherited from superclass' do a = mock_controller { layout :master } b = mock_controller(a) { layout :baster } c = mock_controller(a) { import :layout, from: b } assert(c.initialize_controller.layout) == :baster end it 'renders without layout if no default layout set' do app mock_controller { def get; render; end } get assert(last_response).is_ok_with_body('/') end it 'renders without layout if default layout set to false' do app mock_controller { layout false def get; render; end } get assert(last_response).is_ok_with_body('/') end it 'renders without layout if layout option set to nil or false' do app mock_controller { layout :layout def var; @var end def get; render(layout: nil); end def post; @var = 'x'; render(layout: false); end def put; @var = 'y'; render; end } get assert(last_response).is_ok_with_body('/') post assert(last_response).is_ok_with_body('x') put assert(last_response).is_ok_with_body("=y=\n") end it 'renders with a defined layout' do app mock_controller { define_layout(:master) {'=<%= yield %>='} layout :master def get; render; end } get assert(last_response).is_ok_with_body('=/=') end it 'prefers defined layouts over files' do app mock_controller { define_layout(:layout) {'-<%= yield %>-'} layout :layout def get; render; end } get assert(last_response).is_ok_with_body('-/-') end it 'prefers explicit layouts over implicit ones' do app mock_controller { define_layout(:x) {'x<%= yield %>'} define_layout(:y) {'y<%= yield %>'} layout :x def get; render {''}; end def post; render(layout: :y) {''}; end } get assert(last_response).is_ok_with_body('x') post assert(last_response).is_ok_with_body('y') end it 'accepts layout as :template option' do app mock_controller { def get; render_layout(template: '+<%= yield %>+') {'='}; end } get assert(last_response).is_ok_with_body('+=+') end it 'raises ArgumentError if both layout name and :template option given' do app mock_controller { def get; render_layout(:x, template: '+<%= yield %>+') {'='}; end } assert {get}.raise ArgumentError, /both/i end end end