Skip to content

Commit

Permalink
fix: render template according to request format
Browse files Browse the repository at this point in the history
  • Loading branch information
alovak committed Jan 31, 2013
1 parent c7c26ae commit f9658cf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/grape-rabl/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def call(object, env)

if rablable?
rabl do |template|
engine = ::Tilt.new(view_path(template))
engine = ::Tilt.new(view_path(template), {format: env['api.format']})
engine.render endpoint, {}
end
else
Expand Down
44 changes: 44 additions & 0 deletions spec/grape_rabl_xml_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'spec_helper'

describe Grape::Rabl do
subject do
Class.new(Grape::API)
end

before do
subject.format :xml
subject.formatter :xml, Grape::Formatter::Rabl
end

def app
subject
end

context "with xml format" do
before do
subject.before {
env["api.tilt.root"] = "#{File.dirname(__FILE__)}/views"
env["api.format"] = :xml
}
end

it "should respond with proper content-type" do
subject.get("/home", :rabl => "user"){}
get("/home")
last_response.headers["Content-Type"].should == "application/xml"
end

["user", "user.rabl"].each do |rabl_option|
it "should render rabl template (#{rabl_option})" do
subject.get("/home", :rabl => rabl_option) do
@user = OpenStruct.new(:name => "LTe", :email => "email@example.com")
@project = OpenStruct.new(:name => "First")
end

get "/home"

last_response.body.should == %Q{<?xml version="1.0" encoding="UTF-8"?>\n<user>\n <name>LTe</name>\n <email>email@example.com</email>\n <project>\n <name>First</name>\n </project>\n</user>\n}
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'bundler'
Bundler.setup :default, :test

require "active_support/core_ext/hash/conversions.rb"
require 'grape/rabl'
require 'rspec'
require 'rack/test'
Expand Down

0 comments on commit f9658cf

Please sign in to comment.