require "controller/spec_helper" describe "Controller render" do with_environment with_abstract_controller_spec with_view_path "#{spec_dir}/views" before :all do AbstractController = Rad::AbstractController end after :all do remove_constants %w( LayoutFiltersSpec LayoutSpec AnotherLayout ExplicitRenderSpec RenderInsideOfControllerSpec ForbidPartialAsActionSpec FormatSpec AlreadyRenderedSpec SpecialFormatSpec AnotherActionSpec InlineRenderSpec AbstractController InlineWithLayoutSpec MultipleActions ) end describe 'layout' do it "should use :except and :only in layout" do class ::LayoutFiltersSpec inherit AbstractController layout '/layouts/app', only: :action_with_layout def action_with_layout; end def action_without_layout; end end ccall(LayoutFiltersSpec, :action_with_layout, format: 'html').should == "Layout html, content" ccall(LayoutFiltersSpec, :action_without_layout).should == "content" end it "should apply formats to layout" do class LayoutSpec inherit AbstractController layout '/layouts/app' def action; end end ccall(LayoutSpec, :action, format: 'html').should == "Layout html, content" ccall(LayoutSpec, :action, format: 'js').should == "Layout js, content" end it "should take layout: false or layout: '/another_layout'" do class AnotherLayout inherit AbstractController layout '/layout/app' def action; end def without_layout render action: :action, layout: false end def another_layout render action: :action, layout: '/layouts/admin' end end ccall(AnotherLayout, :without_layout).should == "action" ccall(AnotherLayout, :another_layout).should == "Admin layout, action" end it "should take into account :layout when rendering template" do class ::ExplicitRenderSpec inherit AbstractController layout '/layouts/app' def with_layout render '/some_template' end def without_layout render '/some_template', layout: false end end ccall(ExplicitRenderSpec, :with_layout, format: 'html').should == "Layout html, some template" ccall(ExplicitRenderSpec, :without_layout, format: 'html').should == "some template" end end it "should render template inside of controller" do class ::RenderInsideOfControllerSpec inherit AbstractController def some_action render '/some_template' end end ccall(RenderInsideOfControllerSpec, 'some_action').should == "some template" end it "should not allow to render partials as actions" do class ::ForbidPartialAsActionSpec inherit AbstractController def action; end end lambda{ccall ForbidPartialAsActionSpec, :action}.should raise_error(/No template/) end it "should render view with right format" do class FormatSpec inherit AbstractController def action; end end ccall(FormatSpec, :action, format: 'html').should == "html format" ccall(FormatSpec, :action, format: 'js').should == "js format" end it "should be able to use Template.render for different purposes (mail for example)" do Rad::Template.render("/standalone", locals: {a: 'a'}).should == 'standalone usage, a' end it "should not rener if already rendered in controller" do class ::AlreadyRenderedSpec inherit AbstractController def action render '/AlreadyRenderedSpec/custom_template' end end ccall(AlreadyRenderedSpec, :action).should == 'custom content' end it "should handle serialization obj ('xml', 'json')" do class ::SpecialFormatSpec inherit AbstractController def json_action render json: {a: "b"} end def xml_action render xml: {a: "b"} end end ccall(SpecialFormatSpec, :json_action, format: 'json').should == %({"a":"b"}) ccall(SpecialFormatSpec, :xml_action, format: 'xml').should =~ /b<\/a>/ -> { ccall(SpecialFormatSpec, :json_action, format: 'xml') }.should raise_error(/responing with 'json' to the 'xml'/) end it "should render another action via action: :action_name" do class ::AnotherActionSpec inherit AbstractController def another_action; end def action render action: :another_action end end ccall(AnotherActionSpec, :action).should == "another action (action_name: another_action)" end it "should take :inline option" do class ::InlineRenderSpec inherit AbstractController def action render inline: "content" end end ccall(InlineRenderSpec, :action).should == "content" end it ":inline option should render without layout" do class ::InlineWithLayoutSpec inherit AbstractController layout '/layouts/app' def action render inline: "content" end end ccall(InlineWithLayoutSpec, :action).should == "content" end it "should render correct action inside of special 'actions.xxx' template file" do class ::MultipleActions inherit AbstractController def increase; end def decrease; end def action_without_template; end end ccall(MultipleActions, :increase).should == "plus\n" ccall(MultipleActions, :decrease).should == "minus\n" ccall(MultipleActions, :action_without_template).should == "" end end